Type inference problem when tying a class with a type var
Open
Nobody has claimed this yet.
bug
topic-type-variables
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Please consider
import dataclasses as dtc
import typing as tp
T = tp.TypeVar('T', int, str, covariant=True)
@dtc.dataclass(frozen=True)
class C(tp.Generic[T]):
c: type[T]
c_decision: dict[bool, C] = {
True: C(int),
False: C(str),
}
reveal_type(c_decision[True])
reveal_type(c_decision[False])
then
$ mypy -V
mypy 0.942
$ mypy issues-mypy/t9.py
issues-mypy/t9c.py:15: note: Revealed type is "t9c.C*[Any]"
issues-mypy/t9c.py:16: note: Revealed type is "t9c.C*[Any]"
If I remove type annotation for c_decision
c_decision = {
True: C(int),
False: C(str),
}
then the output is
issues-mypy/t9c.py:15: note: Revealed type is "builtins.object*"
issues-mypy/t9c.py:16: note: Revealed type is "builtins.object*"
EDIT: rewriting the description to try to simplify the problem
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 mypy on the shown issues-mypy/t9.py reproducer, or the referenced t9c.py variant, and compare revealed types with and without c_decision's annotation. Trace inference for C(int) and C(str); done when the intended behavior is captured by a regression test.
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
- Mostly clear
- Newbie friendliness
- 35/100