-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BufferedWriter
is not compatible with SupportsWrite[AnyStr]
#5243
Comments
Actually, this is probably a duplicate of #5027. |
I don't think this is related to #5027. It doesn't involve a protocol implementation with a method-local TypeVar. This looks like a problem with recent changes in typeshed. Let's look more closely at your example. Function I presume this typeshed change was prompted by PEP 688. I'm tempted to revert the typeshed update until we can determine what's going on here. This is likely to break a lot of code. I just confirmed that it breaks the @JelleZijlstra, perhaps you can provide some insights here? |
We in fact added |
Ah, I missed that. OK, then this might be a type analysis bug. I'll dig deeper. Thanks for the quick response. |
I've reverted the typeshed changes for the time being and published a new version of pyright (1.1.313) that addresses this regression. I wanted to do this prior to this week's build of pylance (which is done on Wednesday morning's). This will give me time to investigate the underlying cause of this problem prior to next week's build. |
After looking at this more closely, I don't think this is a bug in pyright. Here's a self-contained, minimal code sample that demonstrates the problem: from typing import AnyStr, Protocol, TypeVar
_T_co = TypeVar("_T_co", covariant=True)
_T_contra = TypeVar("_T_contra", contravariant=True)
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[AnyStr], t: SupportsWrite[AnyStr]) -> None:
...
def h(src: SupportsRead[bytes], tgt: BufferedWriter) -> None:
f(src, tgt) The issue is that @JelleZijlstra, I don't fully understand the recent typeshed changes, but they appear to be creating a lot of new errors in existing typed code bases. I presume that you ran mypy_primer with the changes. I'm curious whether that indicated any significant problems for existing code bases? I'm hesitant to upgrade the bundled stubs for pyright until we've worked through some of these issues. Another issue that seems to be related to this one is in the import io
from typing import Any, Protocol, TypeVar
AnyStr_contra = TypeVar("AnyStr_contra", str, bytes, contravariant=True)
class BaseBuffer(Protocol):
@property
def mode(self) -> str: ...
def seek(self, __offset: int, __whence: int = ...) -> int: ...
def seekable(self) -> bool: ...
def tell(self) -> int: ...
class WriteBuffer(BaseBuffer, Protocol[AnyStr_contra]):
def write(self, __b: AnyStr_contra) -> Any: ...
def flush(self) -> Any: ...
def test(b: io.BytesIO):
x: WriteBuffer[bytes] | WriteBuffer[str] = b # Type error with new typeshed Let me know if you'd like me to open individual issues in the typeshed project to work through these issues (and others I'm seeing). |
I think this should pass: in the call to Mypy emits an error here because in its bundled stubs, Pyright fails on that sample, which I think is a bug. I'll need some more time to wrap my head around the pandas example, but in general please feel free to open bugs on typeshed. |
BufferedWriter
is not compatible with SupportsWrite[AnyStr]
…pe variables whose values are provided by other argument types in a call. This addresses #5243.
…pe variables whose values are provided by other argument types in a call. This addresses #5243.
…required to fix #5243. (#5272) Co-authored-by: Eric Traut <[email protected]>
…pe variables whose values are provided by other argument types in a call. This addresses #5243.
…pe variables whose values are provided by other argument types in a call. This addresses #5243. (#5273) Co-authored-by: Eric Traut <[email protected]>
* Improved the protocol matching logic so it honors partially-solved type variables whose values are provided by other argument types in a call. This addresses #5243. * Updated typeshed to the latest version. --------- Co-authored-by: Eric Traut <[email protected]>
This will be fixed in the next release. |
This is addressed in pyright 1.1.314, which I just published. It will also be included in this week's Pylance prerelease build. |
Thanks, the original code snippet works now, but if I set the type of from __future__ import annotations
from io import BufferedWriter
import shutil
from typing import TYPE_CHECKING, Any, AnyStr
if TYPE_CHECKING:
from _typeshed import SupportsRead, SupportsWrite
def f(s: SupportsRead[AnyStr], t: SupportsWrite[AnyStr]) -> None:
shutil.copyfileobj(s, t)
def g(s: SupportsRead[bytes], t: SupportsWrite[bytes]) -> None:
shutil.copyfileobj(s, t)
def h(src: Any, tgt: BufferedWriter) -> None: # changed: `src` now has type `Any`
f(src, tgt) # same error as before
g(src, tgt) |
…e case where a partially-solved type variable with a solution of `Any` are provided by other argument types in a call. This addresses #5243.
…e case where a partially-solved type variable with a solution of `Any` are provided by other argument types in a call. This addresses #5243. (#5296) Co-authored-by: Eric Traut <[email protected]>
Thanks for the bug report. This will be addressed in the next release. |
This comment was marked as spam.
This comment was marked as spam.
This is included in pyright 1.1.315, which I just published. It will also be included in this week's insiders release of pylance. |
Describe the bug
Sorry for the rambling title – I couldn't think of a good description of this:
Expected behavior
If I can pass
tgt
succesfully tog
, then I don't see why it would fail withf
. Clearly,BufferedWriter
is compatible withSupportsWrite[bytes]
.VS Code extension or command-line
command-line
Additional context
This happened after updating to the most recent version. I believe there were typeshed changes around this code.
The text was updated successfully, but these errors were encountered: