Generic type variable not unified with specific bound type.
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
In g below, argument types of different versions of x.f from different bounds of T cross-contaminate. When T is A, x.f can only accept A = T. Likewise, when T is B, x.f can only accept B = T. Hence the code is correct. However, mypy seems to believe that T can somehow be both A and B at simultaneously.
To Reproduce
https://mypy-play.net/?mypy=latest&python=3.11&gist=a92693b3d619e1a11011b86a7cee1ce2
from typing import TypeVar, Generic
class A:
def f(self, other: A) -> None:
...
class B:
def f(self, other: B) -> None:
...
T = TypeVar('T', bound = A | B)
def g(x: T) -> None:
x.f(x)
Expected Behavior
g typechecked successfully.
Actual Behavior
g fails typechecking:
main.py:14: error: Argument 1 to "f" of "A" has incompatible type "T"; expected "A" [arg-type]
main.py:14: error: Argument 1 to "f" of "B" has incompatible type "T"; expected "B" [arg-type]
Your Environment
- Mypy version used: 1.5.0
- Mypy command-line flags:
- Mypy configuration options from
mypy.ini(and other config files): - Python version used: 3.11
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the linked mypy-play reproduction with mypy 1.5.0 and default flags, then compare the reported errors with the expected successful type check for g. Trace the type-variable handling behind the call x.f(x); done means this example typechecks without the incompatible A and B argument errors, with regression coverage added.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100