Skip to content

Cannot infer type argument from overloaded function with explicit self types #14943

Closed
@JukkaL

Description

@JukkaL

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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions