From 2cf83ede6e8ce01ee0c1884e3cb69dfdaad6a8e0 Mon Sep 17 00:00:00 2001 From: Grzegorz Bokota Date: Tue, 6 Aug 2024 22:36:27 +0200 Subject: [PATCH] fix: Fix checking sublcasses of Future (#665) * fix: Fix checking sublcasses of Future * skip test on python 3.8 * add negative test --- src/magicgui/_util.py | 9 +++++---- tests/test_util.py | 8 ++++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/magicgui/_util.py b/src/magicgui/_util.py index 4bfa65a9a..8f113acaf 100644 --- a/src/magicgui/_util.py +++ b/src/magicgui/_util.py @@ -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 diff --git a/tests/test_util.py b/tests/test_util.py index de00bf8f8..d3ce2325f 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -1,6 +1,7 @@ import sys import typing from collections.abc import Mapping, Sequence +from concurrent.futures import Future import pytest @@ -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]])