TypeVar declared at class scope with generic bound specialized on an in-scope generic parameter TypeVar doesn't give an error
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
I don't think that TypeVars at class scope have any particular meaning, but if they reject bound=T they ought to also reject bound=G[T] in the same way.
To Reproduce
from typing import TypeVar, Generic
T = TypeVar("T")
class A:
pass
class B(A):
pass
class G(Generic[T]):
pass
anA = TypeVar("anA", bound=A)
class X(Generic[anA]):
# error, as expected
# AnInnerA = TypeVar("AnInnerA", bound=anA)
AnInnerG = TypeVar("AnInnerG", bound=G[anA])
def method(self, val: AnInnerG) -> None:
pass
x: X[A] = X()
g: G[A] = G()
x.method(g)
Expected Behavior
I would have expected to see an error on the line where AnInnerG was defined.
Actual Behavior
No error on AnInnerG's definition, but I cannot find any type I can pass to X.method without provoking an error.
main.py:27: error: Value of type variable "AnInnerG" of "method" of "X" cannot be "G[A]" [type-var]
Found 1 error in 1 file (checked 1 source file)
Your Environment
- Mypy version used: 1.4.1
- Mypy command-line flags:
--strict - Mypy configuration options from
mypy.ini(and other config files): n/a - 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 supplied Python reproduction with mypy 1.4.1 and --strict, then compare it with the linked playground. Investigate how class-scope TypeVars and generic bounds are validated; done means the invalid AnInnerG declaration produces the expected diagnostic rather than only failing at the method call.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100