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

pyanalyze doesn't support assignment in __post_init__ #748

Open
hauntsaninja opened this issue Mar 11, 2024 · 1 comment
Open

pyanalyze doesn't support assignment in __post_init__ #748

hauntsaninja opened this issue Mar 11, 2024 · 1 comment

Comments

@hauntsaninja
Copy link
Contributor

from dataclasses import dataclass

@dataclass
class T:
    x: str

    def __post_init__(self):
        self.s = 1
        print(self.s)

@dataclass
class TT(T):
    def foo(self):
        print(self.s)

will give:

b.py.T has no attribute 's' (code: undefined_attribute)
In b.py at line 9
   6: 
   7:     def __post_init__(self):
   8:         self.s = 1
   9:         print(self.s)
                    ^
  10: 
  11: @dataclass
  12: class TT(T):

b.py.TT has no attribute 's' (code: undefined_attribute)
In b.py at line 14
  11: @dataclass
  12: class TT(T):
  13:     def foo(self):
  14:         print(self.s)
                    ^

If the subclass is not a dataclass, you don't get the second complaint

@JelleZijlstra
Copy link
Contributor

Do you think __post_init__ should be special here? Pyanalyze generally assumes dataclasses have only those attributes defined as fields, because in general it's currently very hard to detect undefined attributes. So I might not want to change this until I have a more solid way to detect undefined attributes in general. That's something I've been thinking about, but it would be a big change.

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

No branches or pull requests

2 participants