Skip to content

Mypy doesn't recognize dataclass attributes defined in __post_init__ #13726

Closed
@GlenNicholls

Description

@GlenNicholls

Bug Report

I think this is somewhat related to #11923, not positive though.

Mypy doesn't seem to recognize dataclass attributes defined in __post_init__ and I'm not sure if this is expected/intentional:

import dataclasses
@dataclasses.dataclass 
class A:
  def __post_init__(self):
    self.a = dict(foo=["a"])

  def blah(self):
    self.a["foo"].append("a")

Expected Behavior

I would expect that this should be fine as the __post_init__ should have enough information for mypy to recognize that the attribute is defined.

Actual Behavior

In member "foo" of class "A":
error: "object" has no attribute "append"  [attr-defined]
            self.a["foo"].append("a")

Your Environment

  • Mypy version used: 0.971
  • Mypy configuration options from pyproject.toml:
    [tool.mypy]
    warn_unused_configs = true
    warn_unused_ignores = true
    install_types = true
    non_interactive = true
    check_untyped_defs = true
    show_error_codes = true
    show_error_context = true
    pretty = true
    color_output = true
  • Python version used: 3.9
  • Operating system and version: Ubuntu 20.04

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions