File tree Expand file tree Collapse file tree 1 file changed +20
-0
lines changed
crates/ty_python_semantic/resources/mdtest Expand file tree Collapse file tree 1 file changed +20
-0
lines changed Original file line number Diff line number Diff line change @@ -1502,6 +1502,26 @@ class Foo:
1502
1502
static_assert(not is_assignable_to(Foo, Iterable[Any]))
1503
1503
```
1504
1504
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
+
1505
1525
## Equivalence of protocols with method members
1506
1526
1507
1527
Two protocols ` P1 ` and ` P2 ` , both with a method member ` x ` , are considered equivalent if the
You can’t perform that action at this time.
0 commit comments