Skip to content

Support defer_build for Pydantic dataclasses #10313

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

Merged
merged 7 commits into from
Sep 18, 2024
Merged

Conversation

Viicos
Copy link
Member

@Viicos Viicos commented Sep 5, 2024

Also return early if defer_build is set for Pydantic models.

Change Summary

Related issue number

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

Also return early if `defer_build` is set for Pydantic models.
@github-actions github-actions bot added the relnotes-fix Used for bugfixes. label Sep 5, 2024
Copy link

cloudflare-workers-and-pages bot commented Sep 5, 2024

Deploying pydantic-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: 7ae6da8
Status: ✅  Deploy successful!
Preview URL: https://c4413a6e.pydantic-docs.pages.dev
Branch Preview URL: https://dc-defer-build.pydantic-docs.pages.dev

View logs

Copy link

codspeed-hq bot commented Sep 5, 2024

CodSpeed Performance Report

Merging #10313 will not alter performance

Comparing dc-defer-build (7ae6da8) with main (59a3de7)

Summary

✅ 49 untouched benchmarks

Copy link
Contributor

github-actions bot commented Sep 5, 2024

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  pydantic
  dataclasses.py
  pydantic/_internal
  _dataclasses.py
  _model_construction.py
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.

Looks great, left one comment re the _force_build argument - would like to avoid that if possible.

I've submitted #10329, which we can probably merge + rebase with to make this PR just a bit more simple.

@Viicos
Copy link
Member Author

Viicos commented Sep 18, 2024

So the reason why the added _force_build argument was required is because currently ultimately, rebuild_dataclass calls complete_dataclass. Because the configuration can come from both the decorator or the __pydantic_config__ attribute, complete_dataclass sets cls.__pydantic_config__:

cls.__pydantic_config__ = config_wrapper.config_dict # type: ignore

So that the following works as expected:

@pydantic_dataclass(config={'strict': True})
class DC:
    a: int

assert DC.__pydantic_config__ == {'strict': True}

If in rebuild_dataclass, we pass a new config object, as done currently here:

existing_config = getattr(cls, '__pydantic_config__', {})
new_config = {**existing_config, 'defer_build': False}
return _pydantic_dataclasses.complete_dataclass(
cls,
_config.ConfigWrapper(new_config, check=False),
raise_errors=raise_errors,
types_namespace=types_namespace,
)

The following will happen:

@pydantic_dataclass
class DC:
    __pydantic_config__ = {'defer_build': True}  # or as a decorator parameter, doesn't matter

assert DC.__pydantic_config__ == {'defer_build': True}
rebuild_dataclass(DC)
assert DC.__pydantic_config__ == {'defer_build': True}  # AssertionError, `'defer_build'` is now set to `False`

And is inconsistent with the BaseModel behavior. Besides, I think the config dict on models and dataclasses shouldn't change after the class is defined, no matter if a rebuild was performed or not (and this is what's causing issues with TypeAdapter failing tests I think).

@sydney-runkle
Copy link
Contributor

And is inconsistent with the BaseModel behavior. Besides, I think the config dict on models and dataclasses shouldn't change after the class is defined, no matter if a rebuild was performed or not (and this is what's causing issues with TypeAdapter failing tests I think).

I see, makes sense. Let's revert back then to use the _force_build flag. Apologies for the back and forth on the diff.

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.

Nice work!

@sydney-runkle sydney-runkle added relnotes-feature and removed awaiting author revision awaiting changes from the PR author relnotes-fix Used for bugfixes. labels Sep 18, 2024
@sydney-runkle sydney-runkle changed the title Respect defer_build for Pydantic dataclasses Support defer_build for Pydantic dataclasses Sep 18, 2024
@sydney-runkle sydney-runkle merged commit 7ac0eb9 into main Sep 18, 2024
61 checks passed
@sydney-runkle sydney-runkle deleted the dc-defer-build branch September 18, 2024 13:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants