Skip to content

Type inference problem when tying a class with a type var  #12447

@wrobell

Description

@wrobell

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions