-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved the protocol matching logic so it honors partially-solved ty…
…pe variables whose values are provided by other argument types in a call. This addresses #5243. (#5273) Co-authored-by: Eric Traut <[email protected]>
- Loading branch information
1 parent
783633a
commit 7ffe61a
Showing
3 changed files
with
95 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# This sample verifies that a generic protocol that is specialized with | ||
# a type variable can be matched if that type variable's type is | ||
# supplied by another argument in a call. | ||
|
||
from typing import Protocol, TypeVar | ||
|
||
_T_co = TypeVar("_T_co", covariant=True) | ||
_T_contra = TypeVar("_T_contra", contravariant=True) | ||
|
||
|
||
class MyStr: | ||
... | ||
|
||
|
||
class MyBytes: | ||
def __buffer__(self, __flags: int) -> memoryview: | ||
... | ||
|
||
|
||
MyAnyStr = TypeVar("MyAnyStr", MyStr, MyBytes) | ||
|
||
|
||
class Buffer(Protocol): | ||
def __buffer__(self, __flags: int) -> memoryview: | ||
... | ||
|
||
|
||
class SupportsRead(Protocol[_T_co]): | ||
def read(self, __length: int = ...) -> _T_co: | ||
... | ||
|
||
|
||
class SupportsWrite(Protocol[_T_contra]): | ||
def write(self, __s: _T_contra) -> object: | ||
... | ||
|
||
|
||
class BufferedWriter: | ||
def write(self, __buffer: Buffer) -> int: | ||
raise NotImplementedError | ||
|
||
|
||
def f(s: SupportsRead[MyAnyStr], t: SupportsWrite[MyAnyStr]) -> None: | ||
... | ||
|
||
|
||
def h(src: SupportsRead[MyBytes], tgt: BufferedWriter) -> None: | ||
f(src, tgt) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters