diff --git a/src/magicgui/_util.py b/src/magicgui/_util.py index 465b70ae8..a6054d1dd 100644 --- a/src/magicgui/_util.py +++ b/src/magicgui/_util.py @@ -187,6 +187,8 @@ def _safe_isinstance_tuple(obj: object, superclass: object) -> bool: def safe_issubclass(obj: object, superclass: object) -> bool: """Safely check if obj is a subclass of superclass.""" + if obj is superclass: + return True if isinstance(superclass, tuple): return any(safe_issubclass(obj, s) for s in superclass) obj_origin = get_origin(obj) diff --git a/tests/test_util.py b/tests/test_util.py index ea051add2..61d5cf096 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -76,7 +76,16 @@ def test_tuple_check(self): assert not safe_issubclass(typing.Tuple[int, int], typing.Tuple[int, int, int]) def test_subclass_future(self): - assert safe_issubclass(Future[typing.List[int]], Future[list[int]]) - assert safe_issubclass(Future[typing.List[int]], Future[list]) - assert safe_issubclass(Future[list[int]], Future[typing.List[int]]) - assert not safe_issubclass(Future[list[int]], Future[typing.List[str]]) + assert safe_issubclass(Future[list[int]], Future[list[int]]) + assert safe_issubclass(Future[list[int]], Future[list]) + assert safe_issubclass(Future[list[int]], Future[list[int]]) + assert not safe_issubclass(Future[list[int]], Future[list[str]]) + + def test_subclass_new_type(self): + new_int = typing.NewType("new_int", int) + + assert safe_issubclass(new_int, new_int) + assert safe_issubclass(list[new_int], typing.List[new_int]) + assert safe_issubclass(typing.List[new_int], list[new_int]) + assert safe_issubclass(list[new_int], typing.Sequence[new_int]) + assert safe_issubclass(list[new_int], list[new_int])