From d994830ed8e3bb73906783129460f225ae8e2875 Mon Sep 17 00:00:00 2001 From: DanCardin Date: Tue, 24 Sep 2024 20:46:59 -0400 Subject: [PATCH] fix: Implement is_subtype_of in terms of is_subclass_of. --- tests/test_type_view.py | 3 ++- type_lens/type_view.py | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/test_type_view.py b/tests/test_type_view.py index 770fae5..41bb116 100644 --- a/tests/test_type_view.py +++ b/tests/test_type_view.py @@ -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: @@ -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 diff --git a/type_lens/type_view.py b/type_lens/type_view.py index 9779c94..eeb74a3 100644 --- a/type_lens/type_view.py +++ b/type_lens/type_view.py @@ -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.