Skip to content

Commit 5960688

Browse files
committed
another test
1 parent fbd12a6 commit 5960688

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

crates/ty_python_semantic/resources/mdtest/protocols.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,6 +1502,26 @@ class Foo:
15021502
static_assert(not is_assignable_to(Foo, Iterable[Any]))
15031503
```
15041504

1505+
Because method members must always be available on the class, it is safe to access a method on
1506+
`type[P]`, where `P` is a protocol class, just like it is generally safe to access a method on
1507+
`type[C]` where `C` is a nominal class:
1508+
1509+
```py
1510+
from typing import Protocol
1511+
1512+
class Foo(Protocol):
1513+
def method(self) -> str: ...
1514+
1515+
def f(x: Foo):
1516+
reveal_type(type(x).method) # revealed: def method(self) -> str
1517+
1518+
class Bar:
1519+
def __init__(self):
1520+
self.method = lambda: "foo"
1521+
1522+
f(Bar()) # error: [invalid-argument-type]
1523+
```
1524+
15051525
## Equivalence of protocols with method members
15061526

15071527
Two protocols `P1` and `P2`, both with a method member `x`, are considered equivalent if the

0 commit comments

Comments
 (0)