Skip to content

Commit

Permalink
fix: Implement is_subtype_of in terms of is_subclass_of.
Browse files Browse the repository at this point in the history
  • Loading branch information
DanCardin committed Sep 25, 2024
1 parent 99237d2 commit d994830
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
3 changes: 2 additions & 1 deletion tests/test_type_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ class Bar(Foo):

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


def test_is_subclass_of() -> None:
Expand All @@ -293,7 +294,7 @@ class Bar(Foo):
assert TypeView(Optional[int]).is_subclass_of(int) is False
assert TypeView(None).is_subclass_of(int) is False
assert TypeView(Literal[1]).is_subclass_of(int) is False
assert TypeView(Union[bool, int]).is_subclass_of(int) is False
assert TypeView(Union[float, str]).is_subclass_of(int) is False

assert TypeView(bool).is_subclass_of(bool) is True
assert TypeView(List[int]).is_subclass_of(list) is True
Expand Down
4 changes: 2 additions & 2 deletions type_lens/type_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ def is_subtype_of(self, typ: Any | tuple[Any, ...], /) -> bool:
return issubclass(self.origin, typ)

if self.annotation is AnyStr:
return issubclass(str, typ) or issubclass(bytes, typ)
return self.annotation is not Any and not self.is_type_var and issubclass(self.annotation, typ)
return TypeView(Union[str, bytes]).is_subtype_of(typ)
return self.annotation is not Any and not self.is_type_var and self.is_subclass_of(typ)

def is_subclass_of(self, typ: Any | tuple[Any, ...], /) -> bool:
"""Whether the annotation is a subclass of the given type.
Expand Down

0 comments on commit d994830

Please sign in to comment.