Skip to content

Commit

Permalink
Fix is_injectable() error with GenericAlias on Python < 3.11.
Browse files Browse the repository at this point in the history
  • Loading branch information
wRAR committed Feb 10, 2025
1 parent 91c6dd4 commit 5e2444b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
8 changes: 8 additions & 0 deletions tests/test_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,16 @@ def to_item(self) -> dict: # type: ignore
"foo": "bar",
}

from collections.abc import Set as CollectionsSet
from typing import Set as TypingSet

assert is_injectable(None) is False
assert is_injectable(type(None)) is False
assert is_injectable(set) is False
assert is_injectable(set[str]) is False
assert is_injectable(TypingSet[str]) is False
assert is_injectable(CollectionsSet[str]) is False
assert is_injectable(Optional[str]) is False
assert is_injectable(MyClass) is False
assert is_injectable(MyClass()) is False
assert is_injectable(MyItemPage) is True
Expand Down
7 changes: 6 additions & 1 deletion web_poet/pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import inspect
from contextlib import suppress
from functools import wraps
from types import GenericAlias
from typing import Any, Generic, Optional, TypeVar, overload

import attr
Expand Down Expand Up @@ -38,7 +39,11 @@ class Injectable(abc.ABC, FieldsMixin):
def is_injectable(cls: Any) -> bool:
"""Return True if ``cls`` is a class which inherits
from :class:`~.Injectable`."""
return isinstance(cls, type) and issubclass(cls, Injectable)
return (
not isinstance(cls, GenericAlias)
and isinstance(cls, type)
and issubclass(cls, Injectable)
)


ItemT = TypeVar("ItemT")
Expand Down

0 comments on commit 5e2444b

Please sign in to comment.