-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Fix dataclass ignoring default_factory passed in Annotated #9971
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 dataclass ignoring default_factory passed in Annotated #9971
Conversation
CodSpeed Performance ReportMerging #9971 will not alter performanceComparing Summary
|
tests/test_dataclasses.py
Outdated
|
|
||
| @pytest.mark.parametrize("field_fn_unset", [Field, dataclasses.field]) | ||
| @pytest.mark.parametrize("field_fn2", [Field, dataclasses.field]) | ||
| def test_annotated_with_field_default_factory( field_fn_unset, field_fn2) -> None: |
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.
Without entering into the details of the fix, perhaps you might want to call make format to let ruff fix formatting for you...
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 did run the make format command, but weirdly I only got the following output:
PDM, version 2.17.1
pdm run ruff check --fix pydantic tests docs/plugins
All checks passed!
pdm run ruff format pydantic tests docs/plugins
87 files left unchanged
It does not format the unformatted test file, so I have to explicitly format it:
$ pdm run ruff format .\tests\test_dataclasses.pytaclasses.py test_datetime.py
1 file reformatted
It does work for files under pydantic/ 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.
Interesting 🤔 might be worth a check IMO as pdm run ruff format pydantic tests docs/plugins does consider tests.
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.
Looks good overall - I had a few follow up requests.
Thanks so much for your contribution!
d70dc1c to
6b3b1fb
Compare
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.
Wonderful, thanks for the fix and the amazingly fast turnaround!
|
Thank you! |
Change Summary
This is a fix for bug reported in #9947. When using
pydantic.dataclasswithAnnotatedandField(default_factory=...), the generated field does not have correctdefault_factory.The only change I made was changing
to
in
FieldInfo._from_dataclass_fieldfunction.The original assignment of
Nonewill causedefault_factoryto be override inFieldInfo.merge_field_infosmethod, which is called incollect_dataclass_fields.Related issue number
fix #9947
Checklist