Skip to content

Commit 21be94a

Browse files
authored
[ty] Explicitly test assignability/subtyping between unions of nominal types and protocols with method members (#20557)
1 parent b7d5dc9 commit 21be94a

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

crates/ty_python_semantic/resources/mdtest/protocols.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1769,6 +1769,9 @@ class P(Protocol):
17691769
class NominalSubtype:
17701770
def m(self, y: int) -> None: ...
17711771

1772+
class NominalSubtype2:
1773+
def m(self, *args: object) -> None: ...
1774+
17721775
class NotSubtype:
17731776
def m(self, x: int) -> int:
17741777
return 42
@@ -1785,18 +1788,24 @@ class DefinitelyNotSubtype:
17851788
m = None
17861789

17871790
static_assert(is_subtype_of(NominalSubtype, P))
1791+
static_assert(is_subtype_of(NominalSubtype2, P))
1792+
static_assert(is_subtype_of(NominalSubtype | NominalSubtype2, P))
17881793
static_assert(not is_assignable_to(DefinitelyNotSubtype, P))
17891794
static_assert(not is_assignable_to(NotSubtype, P))
1795+
static_assert(not is_assignable_to(NominalSubtype | NotSubtype, P))
1796+
static_assert(not is_assignable_to(NominalSubtype2 | DefinitelyNotSubtype, P))
17901797

17911798
# `m` has the correct signature when accessed on instances of `NominalWithClassMethod`,
17921799
# but not when accessed on the class object `NominalWithClassMethod` itself
17931800
#
1794-
# TODO: this should pass
1801+
# TODO: these should pass
17951802
static_assert(not is_assignable_to(NominalWithClassMethod, P)) # error: [static-assert-error]
1803+
static_assert(not is_assignable_to(NominalSubtype | NominalWithClassMethod, P)) # error: [static-assert-error]
17961804

17971805
# Conversely, `m` has the correct signature when accessed on the class object
17981806
# `NominalWithStaticMethod`, but not when accessed on instances of `NominalWithStaticMethod`
17991807
static_assert(not is_assignable_to(NominalWithStaticMethod, P))
1808+
static_assert(not is_assignable_to(NominalSubtype | NominalWithStaticMethod, P))
18001809
```
18011810

18021811
A callable instance attribute is not sufficient for a type to satisfy a protocol with a method
@@ -2112,6 +2121,7 @@ static_assert(is_subtype_of(NClassMethodGood, PClassMethod)) # error: [static-a
21122121
static_assert(is_subtype_of(NClassMethodGood, PStaticMethod)) # error: [static-assert-error]
21132122
static_assert(not is_assignable_to(NClassMethodBad, PClassMethod)) # error: [static-assert-error]
21142123
static_assert(not is_assignable_to(NClassMethodBad, PStaticMethod)) # error: [static-assert-error]
2124+
static_assert(not is_assignable_to(NClassMethodGood | NClassMethodBad, PClassMethod)) # error: [static-assert-error]
21152125

21162126
static_assert(is_assignable_to(NStaticMethodGood, PClassMethod))
21172127
static_assert(is_assignable_to(NStaticMethodGood, PStaticMethod))
@@ -2120,6 +2130,7 @@ static_assert(is_subtype_of(NStaticMethodGood, PClassMethod)) # error: [static-
21202130
static_assert(is_subtype_of(NStaticMethodGood, PStaticMethod)) # error: [static-assert-error]
21212131
static_assert(not is_assignable_to(NStaticMethodBad, PClassMethod)) # error: [static-assert-error]
21222132
static_assert(not is_assignable_to(NStaticMethodBad, PStaticMethod)) # error: [static-assert-error]
2133+
static_assert(not is_assignable_to(NStaticMethodGood | NStaticMethodBad, PStaticMethod)) # error: [static-assert-error]
21232134
```
21242135

21252136
## Equivalence of protocols with method or property members

0 commit comments

Comments
 (0)