Skip to content

Commit 8196b76

Browse files
committed
Fix subclass check on union metatype.
Issue: #144
1 parent 9978dad commit 8196b76

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

multimethod/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ def __hash__(self) -> int:
8888

8989
def __subclasscheck__(self, subclass):
9090
args = get_args(subclass)
91-
match origin := get_origin(subclass) or subclass:
91+
match origin := get_origin(subclass):
92+
case None:
93+
origin = subclass
9294
case typing.Literal:
9395
return all(isinstance(arg, self) for arg in args)
9496
case typing.Union | types.UnionType:

tests/test_subscripts.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def test_union():
3939
assert issubclass(int, subtype(int | float))
4040
assert issubclass(subtype(int | float), subtype(int | float | None))
4141
assert subtype(Iterable | Mapping | Sequence) is Iterable
42+
assert not issubclass(Union, subtype(type[int]))
4243

4344
# Test nested subtype with UnionType base
4445
tp = subtype(int | float)
@@ -77,10 +78,15 @@ def func(_: cls[int]): ...
7778

7879
@func.register
7980
def _(_: type[cls[int]]):
81+
return int
82+
83+
@func.register
84+
def _(_: type[cls]):
8085
return type
8186

8287
assert func(cls[int]()) is None
83-
assert func(cls[int]) is type
88+
assert func(cls[int]) is int
89+
assert func(cls) is type
8490

8591

8692
def test_tuple():

0 commit comments

Comments
 (0)