Skip to content

Commit

Permalink
fix: fix safe_issubclass for NewType
Browse files Browse the repository at this point in the history
  • Loading branch information
Czaki committed Nov 22, 2024
1 parent 59f1244 commit 777029d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/magicgui/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 == superclass:
return True
if isinstance(superclass, tuple):
return any(safe_issubclass(obj, s) for s in superclass)
obj_origin = get_origin(obj)
Expand Down
15 changes: 15 additions & 0 deletions tests/test_magicgui.py
Original file line number Diff line number Diff line change
Expand Up @@ -940,3 +940,18 @@ def func() -> Union[None, int]:

func()
mock.assert_called_once()


def test_list_builtin_and_typing():
mock = Mock()

new_int = NewType("new_int", int)

register_type(list[new_int], return_callback=mock)

@magicgui
def func() -> list[new_int]:
return [1]

func()
mock.assert_called_once()

0 comments on commit 777029d

Please sign in to comment.