Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
GlenNicholls opened this issue Sep 24, 2022 · 1 comment
Closed
Labels
bug mypy got something wrong

Comments

@GlenNicholls
Copy link

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
@GlenNicholls GlenNicholls added the bug mypy got something wrong label Sep 24, 2022
@GlenNicholls
Copy link
Author

Bleh, I can't reproduce this as it looks like there's something else going on. I'll re-open once I have a better MWE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

1 participant