Skip to content

Commit

Permalink
fix: Fix checking sublcasses of Future (#665)
Browse files Browse the repository at this point in the history
* fix: Fix checking sublcasses of Future

* skip test on python 3.8

* add negative test
  • Loading branch information
Czaki authored Aug 6, 2024
1 parent 9776992 commit 2cf83ed
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/magicgui/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,11 @@ def safe_issubclass(obj: object, superclass: object) -> bool:
if obj_origin is tuple and obj_args:
return _safe_isinstance_tuple(obj, superclass)

return (
issubclass(obj_origin, superclass_origin) and # type: ignore
(obj_args == superclass_args or not superclass_args)
)
if not superclass_args:
return True
if len(obj_args) != len(superclass_args):
return False
return all(safe_issubclass(o, s) for o, s in zip(obj_args, superclass_args))

except Exception:
return False
Expand Down
8 changes: 8 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sys
import typing
from collections.abc import Mapping, Sequence
from concurrent.futures import Future

import pytest

Expand Down Expand Up @@ -78,3 +79,10 @@ def test_tuple_check(self):
assert safe_issubclass(typing.Tuple[int, int], typing.Tuple[int, ...])
assert not safe_issubclass(typing.Tuple[int, int], typing.Tuple[int, str])
assert not safe_issubclass(typing.Tuple[int, int], typing.Tuple[int, int, int])

@pytest.mark.skipif(sys.version_info < (3, 9), reason="Future[] is supported in 3.9+")
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]])

0 comments on commit 2cf83ed

Please sign in to comment.