-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Issue with constrained type var and Protocol #18024
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
Comments
Looks potentially related to python/typeshed#12286 (which landed in v1.12.0 with #17586) |
|
Cannot infer type argument
for open & gzipCannot infer type argument
for open & gzip
Here's an attempt to create a simplified, self-contained reproducer: from typing import AnyStr, Protocol, TypeVar, Generic, overload
class Buffer(Protocol):
def __buffer__(self, flags: int, /) -> memoryview: ...
class IO(Generic[AnyStr]):
@overload
def write(self: IO[bytes], s: Buffer, /) -> int: ...
@overload
def write(self, s: AnyStr, /) -> int: ...
def write(self, s):
return 0
class TextIO(IO[str]):
pass
_T_contra = TypeVar("_T_contra", contravariant=True)
class SupportsWrite(Protocol[_T_contra]):
def write(self, s: _T_contra, /) -> object: ...
def foo(
f: SupportsWrite[AnyStr],
) -> None: ...
def bar(d: TextIO) -> None:
foo(d) # Value of type variable "AnyStr" of "foo" cannot be "Buffer" Interestingly, this fails on 1.11 as well, and doesn't depend on the changes in #17586. I looked at this a bit, it seems that constraints inference from protocol generates invalid constraints ( @ilevkivskyi I remember that you mentioned that you've looked into inconsistencies between |
Yes, and it may not be trivial to fix. Ultimately, we need to refactor this, so that everything uses |
It may still be possible to fix this specific regression by a more ad-hoc fix, by adding some functionality to |
As of at least version 1.15.0, this issue seems to be resolved...
|
Hm yeah, looks like it got fixed in a more recent typeshed sync: #17971 Jukka's self contained repro still reproduces, so we should keep this issue open It could also maybe be worth adding a test case to typeshed |
Cannot infer type argument
for open & gzip
Bug Report
Seems like a regression from 11.2, consider the following
I see errors even though this should be fine?
no errors in 1.11.2:
Your Environment
mypy.ini
(and other config files): noneThe text was updated successfully, but these errors were encountered: