Skip to content

Conversation

mochi22
Copy link
Contributor

@mochi22 mochi22 commented Aug 8, 2024

Change Summary

This PR addresses an issue where defining frozen both in the @dataclass decorator and config=ConfigDict(frozen=True) led to confusing and inconsistent behavior.
The changes made in this PR aim to clarify the expected behavior and provide better guidance to users.

  • docs change
    It's not recommended to define both `frozen` in the `dataclass` decorator and `ConfigDict`.
    If both are defined, the setting from the dataclass decorator will take priority.

In the dataclasses.md file, there was no specific explanation about frozen, so I added an explanation about the frozen setting in ConfigDict(config.py).

  • code change
            # It's not recommended to define both, as the setting from the dataclass decorator will take priority.
            if config_wrapper.frozen:
                warn(
                    "Both 'dataclass' decorator and 'config.frozen' are defined. 'config.frozen' take priority.",
                    UserWarning,
                )

Related issue number

fix #9850

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

please review: @sydney-runkle

@github-actions github-actions bot added the relnotes-fix Used for bugfixes. label Aug 8, 2024
@mochi22
Copy link
Contributor Author

mochi22 commented Aug 8, 2024

Hi @sydney-runkle,
Could you please check the PR when it'll be possible!

Copy link

codspeed-hq bot commented Aug 8, 2024

CodSpeed Performance Report

Merging #10082 will not alter performance

Comparing mochi22:mochi22-issue9850 (f6b2763) with main (cb3ac5a)

Summary

✅ 17 untouched benchmarks

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 like a good start. We've left a few requests for changes. Thanks for your contribution!

@pydantic-hooky pydantic-hooky bot added the awaiting author revision awaiting changes from the PR author label Aug 8, 2024
Viicos
Viicos previously requested changes Aug 11, 2024
Copy link
Member

@Viicos Viicos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe a small test (with pytest.warns) would be great. We also need to specify the correct stacklevel argument, as per my comment.

Copy link
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  pydantic
  dataclasses.py 235
Project Total  

This report was generated by python-coverage-comment-action

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.

Requested 2 other changes. Looks good otherwise, thanks!

@sydney-runkle
Copy link
Contributor

As @Viicos mentioned, I'd love to see a test (and the stacklevel specification)

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.

@mochi22,

Looks great - can merge once we add a test. Thanks!

@sydney-runkle
Copy link
Contributor

I'll go ahead and merge this and add a test in a separate PR

@sydney-runkle sydney-runkle merged commit 079d00c into pydantic:main Aug 13, 2024
59 checks passed
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.

Dataclass ConfigDict frozen doesn't work as expected
3 participants