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
dataclass init=False field with default works but default_factory does not #89529
Comments
Simple example @dataclass(init=False)
class TestObject(object):
m: str = field(default='hi')
k: list = field(default_factory=list)
def test(self):
print(f'm is {self.m} ')
print(f'k is {self.k}')
if __name__ == '__main__':
myobject = TestObject()
myobject.test() Produces: Traceback (most recent call last):
File "H:\unit_test\tests_dataclass.py", line 81, in <module>
myobject.test()
File "H:\unit_test\tests_dataclass.py", line 76, in test
print(f'k is {self.k}')
AttributeError: 'TestObject' object has no attribute 'k'
m is hi So m is initialized to hi but k just disappears But wait there's more! If i do from dataclasses import dataclass, field
@dataclass(init=False)
class TestObject(object):
m: str = field(default='hi')
k: list = field(default_factory=list)
def test(self):
print(f'm is {self.m} ')
print(f'k is {self.k}')
@dataclass
class InheritedTestObject(TestObject):
def __post_init__(self):
super().__init__()
print(f'Inherited m is {self.m} ')
print(f'Inherited k is {self.k}')
print(f'Inherited g is {self.k}')
if __name__ == '__main__':
myobject = InheritedTestObject()
myobject.test() It produces: Inherited m is hi Process finished with exit code 0 NO ERRORS! It seems like a bug to me, but what is the expected behavior in this case? I would expect the first case to not error out and should have an empty list. I've only tested this on Python 3.9 so far. |
Also wanted to add that I did try another variation of the first example where I set the default_factory field's init value to False and I got the same error. from dataclasses import dataclass, field
@dataclass(init=False)
class TestObject(object):
m: str = field(default='hi')
k: list = field(init=False, default_factory=list)
def test(self):
print(f'm is {self.m} ')
print(f'k is {self.k}')
if __name__ == '__main__':
myobject = TestObject()
myobject.test() Also produces Traceback (most recent call last):
File "H:\unit_test\tests_dataclass.py", line 81, in <module>
myobject.test()
File "H:\unit_test\tests_dataclass.py", line 76, in test
print(f'k is {self.k}')
AttributeError: 'TestObject' object has no attribute 'k'
m is hi |
Right now
This is why: imagine, that we execute
Another, more complex solution is to track fields with |
I think a good possible solution is to raise an error if The error would be "Error: default_factory argument needs |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: