From 777029dbb62a8b9a3a27939a2c0de875f58930c5 Mon Sep 17 00:00:00 2001 From: Grzegorz Bokota Date: Fri, 22 Nov 2024 22:19:34 +0100 Subject: [PATCH 1/5] fix: fix safe_issubclass for NewType --- src/magicgui/_util.py | 2 ++ tests/test_magicgui.py | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/magicgui/_util.py b/src/magicgui/_util.py index 465b70ae8..daf9015b2 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 == 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_magicgui.py b/tests/test_magicgui.py index f113e7494..7d7b342fc 100644 --- a/tests/test_magicgui.py +++ b/tests/test_magicgui.py @@ -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() From 8008cd36b740331eb42429364472a0b504ae1971 Mon Sep 17 00:00:00 2001 From: Grzegorz Bokota Date: Fri, 22 Nov 2024 22:52:00 +0100 Subject: [PATCH 2/5] move tests --- tests/test_magicgui.py | 15 --------------- tests/test_util.py | 9 +++++++++ 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/tests/test_magicgui.py b/tests/test_magicgui.py index 7d7b342fc..f113e7494 100644 --- a/tests/test_magicgui.py +++ b/tests/test_magicgui.py @@ -940,18 +940,3 @@ 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() diff --git a/tests/test_util.py b/tests/test_util.py index 2973ea84b..c3bbf8f32 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -80,3 +80,12 @@ def test_subclass_future(self): 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], list[new_int]) + assert safe_issubclass(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]) From e25ddeedf23c05815c190a4fd008cd97658a2341 Mon Sep 17 00:00:00 2001 From: Grzegorz Bokota Date: Fri, 22 Nov 2024 23:18:24 +0100 Subject: [PATCH 3/5] fix test, disable UP006 for test file --- pyproject.toml | 1 + tests/test_util.py | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index c30a4f6ad..5af218142 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -166,6 +166,7 @@ ignore = [ [tool.ruff.lint.per-file-ignores] "tests/*.py" = ["D", "S", "E501"] +"tests/test_util.py" = ["D", "S", "E501", "UP006"] "docs/*.py" = ["B"] "docs/examples/*.py" = ["D", "B", "E501"] "src/magicgui/widgets/_image/*.py" = ["D"] diff --git a/tests/test_util.py b/tests/test_util.py index c3bbf8f32..30cf901bc 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -85,7 +85,7 @@ 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], list[new_int]) - assert safe_issubclass(list[new_int], list[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]) From dbdbd073e60a459c74cf5e7265a6fb55a578326d Mon Sep 17 00:00:00 2001 From: Grzegorz Bokota Date: Sat, 23 Nov 2024 22:08:33 +0100 Subject: [PATCH 4/5] Update src/magicgui/_util.py --- src/magicgui/_util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/magicgui/_util.py b/src/magicgui/_util.py index daf9015b2..a6054d1dd 100644 --- a/src/magicgui/_util.py +++ b/src/magicgui/_util.py @@ -187,7 +187,7 @@ 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: + if obj is superclass: return True if isinstance(superclass, tuple): return any(safe_issubclass(obj, s) for s in superclass) From fbe381bc4b101d76974c5f508748db52f9b58b73 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 23 Nov 2024 21:29:41 +0000 Subject: [PATCH 5/5] style(pre-commit.ci): auto fixes [...] --- tests/test_util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_util.py b/tests/test_util.py index d4cec9a01..61d5cf096 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -88,4 +88,4 @@ def test_subclass_new_type(self): 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]) \ No newline at end of file + assert safe_issubclass(list[new_int], list[new_int])