Skip to content
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

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

Open
JukkaL opened this issue Mar 23, 2023 · 0 comments · May be fixed by #14975
Open

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

JukkaL opened this issue Mar 23, 2023 · 0 comments · May be fixed by #14975
Labels

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Mar 23, 2023

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.

@JukkaL JukkaL mentioned this issue Mar 23, 2023
2 tasks
tyralla added a commit to tyralla/mypy that referenced this issue Mar 29, 2023
…lf types (Fixes python#14943).

When finding no compatible callable right away, try to let method `ConstraintBuilderVisitor.find_matching_overload_item` return the first overloaded function with the suitable self type.

Checked by test case `TestOverloadedMethodWithExplictSelfTypes`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
1 participant