Open Redoubts opened 1 week ago
Looks potentially related to https://github.com/python/typeshed/pull/12286 (which landed in v1.12.0 with #17586)
mypy_primer -p test.py --bisect --debug --old 'v1.11.0'
confirms it's the sync PR brianschubert mentions
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 (infer_constraints_from_protocol_members
). The root of the problem seems to be in mypy.subtypes.find_member
.
@ilevkivskyi I remember that you mentioned that you've looked into inconsistencies between find_member
and analyze_member_access
. This may be an example where they deviate.
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): none