Skip to content

Conversation

kc0506
Copy link
Contributor

@kc0506 kc0506 commented Jul 25, 2024

Change Summary

This is a fix for bug reported in #9947. When using pydantic.dataclass with Annotated and Field(default_factory=...), the generated field does not have correct default_factory.

The only change I made was changing

if dc_field.default_factory is dataclasses.MISSING:
    default_factory: typing.Callable[[], Any] | None = None

to

if dc_field.default_factory is dataclasses.MISSING:
    default_factory: typing.Callable[[], Any] | None = _Unset

in FieldInfo._from_dataclass_field function.

The original assignment of None will cause default_factory to be override in FieldInfo.merge_field_infos method, which is called in collect_dataclass_fields.

Related issue number

fix #9947

Checklist

  • The pull request title is a good summary of the changes - it will be used in the changelog
  • Unit tests for the changes exist
  • Tests pass on CI
  • Documentation reflects the changes where applicable
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

@github-actions github-actions bot added the relnotes-fix Used for bugfixes. label Jul 25, 2024
Copy link

codspeed-hq bot commented Jul 25, 2024

CodSpeed Performance Report

Merging #9971 will not alter performance

Comparing kc0506:fix-dataclass-field-default-factory (6b3b1fb) with main (d92d341)

Summary

✅ 14 untouched benchmarks


@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:
Copy link
Contributor

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...

Copy link
Contributor Author

@kc0506 kc0506 Jul 25, 2024

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.

Copy link
Contributor

@AlessandroMiola AlessandroMiola Jul 25, 2024

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.

Copy link
Contributor

@sydney-runkle sydney-runkle left a 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!

@pydantic-hooky pydantic-hooky bot added awaiting author revision awaiting changes from the PR author labels Jul 25, 2024
@kc0506 kc0506 force-pushed the fix-dataclass-field-default-factory branch from d70dc1c to 6b3b1fb Compare July 25, 2024 14:51
Copy link
Contributor

@sydney-runkle sydney-runkle left a 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!

@sydney-runkle sydney-runkle enabled auto-merge (squash) July 25, 2024 14:54
@sydney-runkle sydney-runkle merged commit d7931c0 into pydantic:main Jul 25, 2024
@kc0506 kc0506 deleted the fix-dataclass-field-default-factory branch July 26, 2024 08:58
@saintx
Copy link

saintx commented Jul 26, 2024

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting author revision awaiting changes from the PR author relnotes-fix Used for bugfixes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

default_factory passed in annotation Field silently ignored
4 participants