1111from multimethod import DispatchError , multimethod , parametric , subtype
1212
1313
14+ def matches (instance , cls ):
15+ origins = tuple (subtype .origins (cls )) or cls
16+ return isinstance (instance , cls ) and issubclass (type (instance ), origins )
17+
18+
1419def test_literals ():
1520 assert issubclass (subtype (Literal ["a" , "b" ]), str )
1621 assert not issubclass (subtype (Literal ["a" ]), subtype (list [int ]))
1722 assert issubclass (Literal [[0 ]], subtype (Iterable [int ]))
1823 tp = subtype (Literal ["a" , 0 ])
19- assert isinstance ("a" , tp )
20- assert isinstance (0 , tp )
24+ assert matches ("a" , tp )
25+ assert matches (0 , tp )
2126 assert not issubclass (Literal ["a" , 0.0 ], tp )
2227 assert not issubclass (tuple [str , int ], tp )
2328 assert issubclass (tp , subtype (str | int ))
@@ -40,7 +45,8 @@ def test_union():
4045 assert issubclass (subtype (int | float ), subtype (int | float | None ))
4146 assert subtype (Iterable | Mapping | Sequence ) is Iterable
4247 assert not issubclass (Union , subtype (type [int ]))
43- assert isinstance (bool , subtype (type [int ] | type [float ]))
48+ assert matches (bool , subtype (type [int ] | type [float ]))
49+ assert matches (bool | float , subtype (type [int | float ]))
4450
4551 # Test nested subtype with UnionType base
4652 tp = subtype (int | float )
@@ -51,7 +57,7 @@ def test_union():
5157@pytest .mark .skipif (sys .version_info < (3 , 12 ), reason = "Type aliases added in 3.12" )
5258def test_type_alias ():
5359 Point = typing .TypeAliasType (name = "Point" , value = tuple [int , int ])
54- assert isinstance ((0 , 0 ), subtype (Point ))
60+ assert matches ((0 , 0 ), subtype (Point ))
5561
5662
5763def test_type ():
@@ -64,7 +70,7 @@ def _(arg: type[list[str]]):
6470
6571 assert func (list ) is func (list [int ]) is None
6672 assert func (list [str ]) is str
67- assert not isinstance ([], subtype (type [list ]))
73+ assert not matches ([], subtype (type [list ]))
6874 with pytest .raises (DispatchError ):
6975 func (tuple )
7076 with pytest .raises (DispatchError ):
@@ -76,11 +82,11 @@ def test_new():
7682 assert subtype (Str ) is str
7783 tp = subtype (type [Str ])
7884 assert typing .NewType in subtype .origins (tp )
79- assert not isinstance (str , tp )
80- assert isinstance (Str , tp )
81- assert isinstance (typing .NewType ("" , Str ), tp )
82- assert not isinstance (typing .NewType ("" , str ), tp )
83- assert isinstance (Str , subtype (Literal [Str ]))
85+ assert not matches (str , tp )
86+ assert matches (Str , tp )
87+ assert matches (typing .NewType ("" , Str ), tp )
88+ assert not matches (typing .NewType ("" , str ), tp )
89+ assert matches (Str , subtype (Literal [Str ]))
8490
8591
8692def test_generic ():
@@ -110,12 +116,12 @@ def _(_: type[cls]):
110116def test_tuple ():
111117 assert subtype (tuple ) is tuple
112118 assert not issubclass (tuple [int ], subtype (tuple [()]))
113- assert not isinstance (tuple [int ], subtype (type [tuple [()]]))
114- assert isinstance ((), subtype (tuple [()]))
115- assert not isinstance ((0 ,), subtype (tuple [()]))
119+ assert not matches (tuple [int ], subtype (type [tuple [()]]))
120+ assert matches ((), subtype (tuple [()]))
121+ assert not matches ((0 ,), subtype (tuple [()]))
116122 assert issubclass (tuple [int ], subtype (tuple [int , ...]))
117123 assert issubclass (tuple [bool , ...], subtype (tuple [int , ...]))
118- assert isinstance (tuple [int ], subtype (type [tuple [int , ...]]))
124+ assert matches (tuple [int ], subtype (type [tuple [int , ...]]))
119125 assert not issubclass (tuple [int , float ], subtype (tuple [int , ...]))
120126
121127
@@ -168,8 +174,8 @@ def _(arg: Sequence[Callable[[bool], bool]]):
168174def test_final ():
169175 tp = subtype (Iterable [str ])
170176 d = {"" : 0 }
171- assert isinstance (d , subtype (Mapping [str , int ]))
172- assert isinstance (d .keys (), tp )
177+ assert matches (d , subtype (Mapping [str , int ]))
178+ assert matches (d .keys (), tp )
173179
174180
175181def test_args ():
@@ -185,8 +191,8 @@ def test_parametric():
185191 assert issubclass (coro , Callable )
186192 assert not issubclass (Callable , coro )
187193 assert not issubclass (parametric (object , inspect .iscoroutinefunction ), coro )
188- assert isinstance (asyncio .sleep , coro )
189- assert not isinstance (lambda : None , coro )
194+ assert matches (asyncio .sleep , coro )
195+ assert not matches (lambda : None , coro )
190196 assert list (subtype .origins (coro )) == [Callable ]
191197
192198 ints = parametric (array , typecode = "i" )
@@ -196,6 +202,6 @@ def test_parametric():
196202 assert issubclass (sized & ints , ints )
197203 assert not issubclass (ints , sized & ints )
198204 assert not issubclass (parametric (object , typecode = "i" ), array )
199- assert isinstance (array ("i" ), ints )
200- assert not isinstance (array ("l" ), ints )
205+ assert matches (array ("i" ), ints )
206+ assert not matches (array ("l" ), ints )
201207 assert list (subtype .origins (ints )) == [array ]
0 commit comments