python / python/mypy

"Overloaded function implementation does not accept all possible arguments"

Open
#11,391 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-overloads
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

False positive; mypy doesn't believe that Foo[T] is a subtype of T.

from typing import Union, Generic, TypeVar, overload

T = TypeVar("T")

class Foo(Generic[T]):
    pass

@overload
def expect(pattern: Foo[T]) -> None: ...

@overload
def expect(pattern: T) -> None: ...

def expect(pattern: Union[Foo[T], T]) -> None: return None
$ mypy xxx.py
xxx.py:14: error: Overloaded function implementation does not accept all possible arguments of signature 1

Note that the second overload referencing the type variable used in the first overload is necessary; if pattern: int is used instead of pattern: T in the second overload, mypy type-checks the file correctly.

Environment

  • Mypy version used: 0.910
  • Python version used: 3.10 and 3.7
  • Operating system and version: Fedora 34

Related issues

  • #11004: mypy thinks **kwargs: Union[None, int, str] overload isn't accepted by **kwargs: Any
  • #9420: mypy thinks Type[T1] isn't accepted by Union[Type[T1], Type[T2]]
  • #9023: same bug with two different type variables

I think that #10390 fixes this issue, but I'm not sure.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the diagnostic in xxx.py with mypy using the overloads shown, then read related issues #10390, #11004, #9420, and #9023 to understand existing handling. Done means the valid overload implementation is accepted without the reported error and the regression is covered by the project's tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.