This generates a false positive:
from typing import IO, Generic, AnyStr, overload from _typeshed import SupportsRead, SupportsWrite class C(Generic[AnyStr]): @overload def write(self: C[str], s: str) -> int: ... @overload def write(self: C[bytes], s: bytes) -> int: ... def write(self, x): return 0 def cp(src: SupportsRead[AnyStr], dst: SupportsWrite[AnyStr]) -> None: ... def g(a: IO[bytes], b: C[bytes]) -> None: # error: Cannot infer type argument 1 of "cp" cp(a, b)
Here's a real-world example which generates the false positive on master (but doesn't generate one when using mypy 1.1.1):
import tempfile import shutil from typing import IO def f(model_fileobj: IO[bytes]) -> None: with tempfile.NamedTemporaryFile() as tmp_file: # error: Cannot infer type argument 1 of "copyfileobj" shutil.copyfileobj(model_fileobj, tmp_file)
This started on failing on master at 85dac76. In particular, there were changes to tempfile stubs that trigger this issue.
If the write method is not overloaded, the calls are type checked correctly.
This doesn't seem to cause many issues, so I don't consider this a release blocker, but it would still be nice to fix this.
Fix type argument inference for overloaded functions with explicit se…
This particular example was fixed in #17873
Remove now unnecessary mypy ignore
Activity
Fix type argument inference for overloaded functions with explicit se…
hauntsaninja commented on Nov 3, 2024
This particular example was fixed in #17873
Remove now unnecessary mypy ignore