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
fix: Allow inheriting dataclasses with defaults #7184
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2500,3 +2500,50 @@ class StdLibDataclass: | |
|
||
assert is_pydantic_dataclass(PydanticDataclass) is True | ||
assert is_pydantic_dataclass(StdLibDataclass) is False | ||
|
||
|
||
def test_can_inherit_stdlib_dataclasses_with_defaults(): | ||
@dataclasses.dataclass | ||
class Base: | ||
a: None = None | ||
|
||
class Model(BaseModel, Base): | ||
pass | ||
|
||
assert Model().a is None | ||
|
||
|
||
@pytest.mark.xfail(raises=ValidationError, require=True) | ||
def test_can_inherit_stdlib_dataclasses_default_factories_and_use_them(): | ||
"""This test documents that default factories are not supported""" | ||
|
||
@dataclasses.dataclass | ||
class Base: | ||
a: str = dataclasses.field(default_factory=lambda: 'TEST') | ||
|
||
class Model(BaseModel, Base): | ||
pass | ||
|
||
assert Model().a == 'TEST' | ||
|
||
|
||
def test_can_inherit_stdlib_dataclasses_default_factories_and_provide_a_value(): | ||
@dataclasses.dataclass | ||
class Base: | ||
a: str = dataclasses.field(default_factory=lambda: 'TEST') | ||
|
||
class Model(BaseModel, Base): | ||
pass | ||
|
||
assert Model(a='NOT_THE_SAME').a == 'NOT_THE_SAME' | ||
|
||
|
||
def test_can_inherit_stdlib_dataclasses_with_dataclass_fields(): | ||
@dataclasses.dataclass | ||
class Base: | ||
a: int = dataclasses.field(default=5) | ||
|
||
class Model(BaseModel, Base): | ||
pass | ||
|
||
assert Model().a == 5 | ||
Comment on lines
+2541
to
+2549
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we also add a test for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @adriangb I added a test that xfails. The internals of stdlib dataclasses does not assign default factory attributes to the class instance (meaning the warning isnt triggered in this case). It also means that you are right, the default factory is not propagated to the resulting @dataclasses.dataclass
class Base:
a: int = dataclasses.field(default_factory=lambda: 5)
assert hasattr(Base, 'a') # this fails There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The inheritance works fine if you provide a value, so for my usecase this isn't an important feature to support. I added a test for what we need to work, though. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather see
with pytest.raises(...)
since that's what we use elsewhere