Skip to content

Commit c57b884

Browse files
committed
Allow matching on type of annotation in Annotated
1 parent b300a19 commit c57b884

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

src/ovld/mro.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@ def typeorder(t1, t2):
134134
return Order.NONE
135135

136136

137+
def _find_ann(main, others):
138+
if main in others:
139+
return True
140+
elif isinstance(main, type):
141+
return any(isinstance(x, main) for x in others)
142+
else:
143+
return False
144+
145+
137146
def subclasscheck(t1, t2):
138147
"""Check whether t1 is a "subclass" of t2."""
139148
if t1 == t2 or t2 is Any:
@@ -165,7 +174,7 @@ def subclasscheck(t1, t2):
165174
if o1 is Annotated and o2 is Annotated:
166175
t1, *a1 = get_args(t1)
167176
t2, *a2 = get_args(t2)
168-
return subclasscheck(t1, t2) and any(ann in a2 for ann in a1)
177+
return subclasscheck(t1, t2) and any(_find_ann(main, a1) for main in a2)
169178

170179
if o1 is Annotated:
171180
return t2 is Annotated

tests/test_mro.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ def test_subclasscheck_anntype():
150150
assert not subclasscheck(type[int], type[Annotated[int, "hello"]])
151151

152152

153+
def test_subclasscheck_anntype_type():
154+
assert subclasscheck(type[Annotated[int, "hello"]], type[Annotated[int, str]])
155+
assert subclasscheck(type[Annotated[int, str]], type[Annotated[object, str]])
156+
assert not subclasscheck(type[Annotated[int, "hello"]], type[Annotated[int, int]])
157+
158+
153159
@pytest.mark.skipif(
154160
sys.version_info < (3, 12), reason="Python 3.11 is more strict on Annotated"
155161
)

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)