Skip to content

Conversation

Viicos
Copy link
Member

@Viicos Viicos commented Apr 10, 2025

Change Summary

This can be reviewed commit per commit, and each commit message gives the necessary context to understand the motivation.

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

@github-actions github-actions bot added the relnotes-fix Used for bugfixes. label Apr 10, 2025
Copy link

codspeed-hq bot commented Apr 10, 2025

CodSpeed Performance Report

Merging #11733 will not alter performance

Comparing reorganize-fields-collection (da78135) with main (b77ad73)

Summary

✅ 46 untouched benchmarks

@Viicos Viicos force-pushed the reorganize-fields-collection branch from 14c90b2 to 5790c25 Compare April 11, 2025 06:53
Copy link

cloudflare-workers-and-pages bot commented Apr 11, 2025

Deploying pydantic-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: da78135
Status: ✅  Deploy successful!
Preview URL: https://2b47d3ae.pydantic-docs.pages.dev
Branch Preview URL: https://reorganize-fields-collection.pydantic-docs.pages.dev

View logs

Copy link
Contributor

github-actions bot commented Apr 11, 2025

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  pydantic
  dataclasses.py
  fields.py
  pydantic/_internal
  _dataclasses.py
  _decorators.py
  _fields.py
  _generate_schema.py
  _model_construction.py
  _utils.py
Project Total  

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

@Viicos Viicos added relnotes-change Used for changes to existing functionality which don't have a better categorization. and removed relnotes-fix Used for bugfixes. labels Apr 11, 2025
@@ -1537,7 +1415,8 @@ def _typed_dict_schema(self, typed_dict_cls: Any, origin: Any) -> core_schema.Co
and field_name in field_docstrings
):
field_info.description = field_docstrings[field_name]
self._apply_field_title_generator_to_field_info(self._config_wrapper, field_info, field_name)
Copy link
Member Author

Choose a reason for hiding this comment

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

This was actually called twice, here in the _typed_dict_schema() and later in _common_field_schema().

@Viicos Viicos force-pushed the reorganize-fields-collection branch 2 times, most recently from 6f8584a to c89bcbf Compare April 18, 2025 08:46
Viicos added 3 commits April 18, 2025 10:50
Also rewrite the warning message, switch from `NameError`
(meant for names not found) to a `ValueError`, and improve
coverage.
There's no caller of the relevant functions that are providing None,
so we can safely make `config_wrapper` a required parameter.
This is where most of the work is being done. A new `update_field_from_config()`
function is defined and can be used to update the `FieldInfo` instances
from the configuration of the model/class the field belongs to.

Currently, this function will update the field instances by applying
alias and title generators.

Two reasons to move this logic out:
- Alleviate the `GenerateSchema` class, which is currently a big monster.
- Apply these updates as early as possible, when models are being created.
  This way (assuming annotations where resolved), users can safely inspect
  `FieldInfo.alias/title` even if `defer_build=True`.
@Viicos Viicos force-pushed the reorganize-fields-collection branch from c89bcbf to 9e3f6a1 Compare April 18, 2025 08:50
Viicos added 2 commits April 18, 2025 10:54
This is done through the `DecoratorInfos` via a new method.
Ideally, we could also pass the `config_wrapper` in the existing
`build()` method, but this method is recursive so it isn't
straightforward. This has the unfortunate consequence of
having to create the `DecoratorInfos` in two steps/calls, which
is error prone. Perhaps in the future `build()` can be refactored
to avoid the recursive calls.
@Viicos Viicos force-pushed the reorganize-fields-collection branch from 9e3f6a1 to da78135 Compare April 18, 2025 08:54
@Viicos Viicos added the third-party-tests Add this label on a PR to trigger 3rd party tests label Apr 18, 2025
@Viicos Viicos closed this Apr 18, 2025
@Viicos Viicos reopened this Apr 18, 2025
@DouweM DouweM requested a review from Copilot April 25, 2025 21:36
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors field‐related logic by extracting parts of the alias and title generation from the GenerateSchema class into dedicated helper functions, thereby improving modularity and clarity. Key changes include:

  • Consistent updates to warning messages in tests and error messages in schema generation.
  • Introduction of new helper functions (e.g. get_first_not_none, update_field_from_config) to centralize logic previously embedded in GenerateSchema.
  • Propagation of configuration changes in fields, decorators, and dataclass handling.

Reviewed Changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tests/test_main.py & test_create_model.py Updated warning messages for protected namespace conflicts.
tests/test_computed_fields.py & test_aliases.py Added tests for alias generator behavior when deferring build.
pydantic/fields.py Refactored type hints and moved some legacy alias logic.
pydantic/_internal/_generate_schema.py Removed in‐module alias generation and added config_wrapper to field rebuild calls.
pydantic/_internal/_fields.py Centralized protected namespace checking using a new helper.
pydantic/_internal/_decorators.py Introduced update_from_config to update computed field info.
pydantic/dataclasses.py Updated dataclass processing with explicit config updates.
Comments suppressed due to low confidence (3)

pydantic/_internal/_generate_schema.py:743

  • Ensure that passing config_wrapper to rebuild_model_fields correctly propagates the new alias and title generator settings, given that legacy alias generation logic has been removed from this module.
fields = rebuild_model_fields(
                              cls,
+                            config_wrapper=self._config_wrapper,

pydantic/_internal/_decorators.py:518

  • Ensure that the new update_from_config method is covered by unit tests to confirm that computed field info is correctly updated from the config.
def update_from_config(self, config_wrapper: ConfigWrapper) -> None:

pydantic/dataclasses.py:219

  • Verify that adding the call to decorators.update_from_config(config_wrapper) in the dataclass processing pipeline does not introduce any unintended side effects; ensure corresponding tests validate this integration.
decorators.update_from_config(config_wrapper)

@DouweM
Copy link
Contributor

DouweM commented Apr 25, 2025

@Viicos Reading the commit messages, the changes seem reasonable, and the fact that CI is green gives me confidence, but since I haven't actually read each line of the diff I thought I'd let Copilot do that :D Let's see if it has anything to add...

@DouweM
Copy link
Contributor

DouweM commented Apr 25, 2025

@Viicos Good to go unless you still had something you wanted to add.

@Viicos Viicos merged commit d992117 into main Apr 30, 2025
147 checks passed
@Viicos Viicos deleted the reorganize-fields-collection branch April 30, 2025 10:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
relnotes-change Used for changes to existing functionality which don't have a better categorization. third-party-tests Add this label on a PR to trigger 3rd party tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants