Skip to content

Commit

Permalink
fix: Handle literal[something] is_subclass_of checks.
Browse files Browse the repository at this point in the history
  • Loading branch information
DanCardin committed Sep 27, 2024
1 parent e2df15d commit 7882bf8
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ classifiers = [
"Intended Audience :: Developers",
]
name = "type-lens"
version = "0.2.1"
version = "0.2.2"
description = "type-lens is a Python template project designed to simplify the setup of a new project."
readme = "README.md"
license = { text = "MIT" }
Expand Down
3 changes: 2 additions & 1 deletion tests/test_type_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,9 @@ class Bar(Foo):
assert TypeView(List[int]).is_subtype_of(list) is True
assert TypeView(List[int]).is_subtype_of(int) is False
assert TypeView(Optional[int]).is_subtype_of(int) is False
assert TypeView(Union[bool, int]).is_subtype_of(int) is True
assert TypeView(Literal['one']).is_subtype_of(set) is False

assert TypeView(Union[bool, int]).is_subtype_of(int) is True
assert TypeView(Foo).is_subtype_of(Foo) is True
assert TypeView(Bar).is_subtype_of(Foo) is True
assert TypeView(Union[bool, int]).is_subtype_of(int) is True
Expand Down
10 changes: 5 additions & 5 deletions type_lens/type_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from collections.abc import Collection, Mapping
from typing import Any, AnyStr, Final, ForwardRef, Generic, Literal, TypeVar, Union, _SpecialForm

from typing_extensions import Annotated, NotRequired, Required, get_args, get_origin
from typing_extensions import Annotated, NotRequired, Required, get_args, get_origin, Self
from typing_extensions import Literal as ExtensionsLiteral

from type_lens.types.builtins import UNION_TYPES, NoneType
Expand Down Expand Up @@ -53,7 +53,7 @@ def __init__(self, annotation: T) -> None:
self.args: Final[tuple[Any, ...]] = args
self.metadata: Final = metadata
self._wrappers: Final = wrappers
self.inner_types: Final = tuple(TypeView(arg) for arg in args)
self.inner_types: Final = tuple(self.__class__(arg) for arg in args)

def __eq__(self, other: object) -> bool:
if not isinstance(other, TypeView):
Expand Down Expand Up @@ -232,7 +232,7 @@ def is_subtype_of(self, typ: Any | tuple[Any, ...], /) -> bool:
if self.is_union:
return all(t.is_subtype_of(typ) for t in self.inner_types)

return issubclass(self.origin, typ)
return self.is_subclass_of(typ)

if self.annotation is AnyStr:
return TypeView(Union[str, bytes]).is_subtype_of(typ)
Expand All @@ -249,7 +249,7 @@ def is_subclass_of(self, typ: Any | tuple[Any, ...], /) -> bool:
"""
return isinstance(self.fallback_origin, type) and issubclass(self.fallback_origin, typ)

def strip_optional(self) -> TypeView:
def strip_optional(self) -> Self:
if not self.is_optional:
return self

Expand All @@ -258,4 +258,4 @@ def strip_optional(self) -> TypeView:

args = tuple(a for a in self.args if a is not NoneType)
non_optional = Union[args] # type: ignore[valid-type]
return TypeView(non_optional)
return self.__class__(non_optional)

0 comments on commit 7882bf8

Please sign in to comment.