Skip to content

static type checkers doesn't infer the type correctly under dataclass  #118118

Closed as not planned
@gkirchou

Description

@gkirchou

Bug report

Bug description:

This should be reproducible for all Python versions that support dataclass. A relevant bug is reported to typeshed: python/typeshed#11793

Static type checkers seems not smart enough to infer the type correctly in the below case:

from collections import OrderedDict
from typing import reveal_type

od: OrderedDict[int, int] = OrderedDict()

class object:
  d: dict[int, int]

o = object()
o.d = od
reveal_type(o.d)
# `OrderedDict` for runtime
# `collections.OrderedDict[builtins.int, builtins.int]` for mypy
# `OrderedDict[int, int]` for pytype
from collections import OrderedDict
import dataclasses
from typing import reveal_type

@dataclasses.dataclass
class dataclassObject(object):
  d: dict[int, int]                                                                                                        

o = dataclassObject(d=od)
reveal_type(o.d)
# `OrderedDict` for runtime
# `builtins.dict[builtins.int, builtins.int]` for mypy
# `Dict[int, int]` for pytype

CPython versions tested on:

3.9, 3.11

Operating systems tested on:

macOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions