Missing error when an attribute is a variant generic
Open
Nobody has claimed this yet.
bug
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
The following produces no errors but I expected it to be treated the same as if B.a was a get/set property (which causes a variance error)
class A:
pass
class A2(A):
something: str = "hello"
T_co = TypeVar('T_co', covariant=True)
class B(Generic[T_co]):
def __init__(self, a: T_co) -> None:
self.a: T_co = a # EXPECTED: typing error here like "public attributes must have invariant type"
def set_a(b: B[A]) -> None:
b.a = A()
b2: B[A2] = B(A2())
set_a(b2)
print(b2.a.something) # ACTUAL: runtime error here
Python 3.10, mypy 0.961 with --strict
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 3.10 example with mypy --strict and tracing the variance check for self.a in the generic class B. Compare it with the existing get/set-property handling; done means the invalid covariant mutable attribute is diagnosed and the regression is covered.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers, devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 42/100