Skip to content

Conversation

DouweM
Copy link
Contributor

@DouweM DouweM commented Apr 16, 2025

Change Summary

Builds on #11759, to be merged after that.

Related issue number

Part of #11453.

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

Selected Reviewer: @sydney-runkle

Copy link

codspeed-hq bot commented Apr 16, 2025

CodSpeed Performance Report

Merging #11762 will not alter performance

Comparing DouweM:on-complete-hook (d15bdd2) with main (bf06866)

Summary

✅ 46 untouched benchmarks

Copy link
Contributor

github-actions bot commented Apr 16, 2025

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  pydantic
  main.py
  pydantic/_internal
  _model_construction.py
Project Total  

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

@DouweM DouweM force-pushed the on-complete-hook branch from 0c9ec0f to 3b263a7 Compare April 17, 2025 17:43
…ready to be used

# Conflicts:
#	pydantic/_internal/_model_construction.py
#	pydantic/main.py
@DouweM DouweM force-pushed the on-complete-hook branch from 3b263a7 to 05762dd Compare April 17, 2025 20:07
@DouweM DouweM marked this pull request as ready for review April 17, 2025 20:08
@DouweM
Copy link
Contributor Author

DouweM commented Apr 17, 2025

@Viicos Please review!

@DouweM
Copy link
Contributor Author

DouweM commented Apr 20, 2025

I just realized we have a model_post_init hook that's called on a model after initialization/construction:

pydantic/pydantic/main.py

Lines 586 to 588 in bce81ef

def model_post_init(self, context: Any, /) -> None:
"""Override this method to perform additional initialization after `__init__` and `model_construct`.
This is useful if you want to do some validation that requires the entire model to be initialized.

That one’s name can be overridden through an attribute on BaseModel:

pydantic/pydantic/main.py

Lines 191 to 192 in bce81ef

__pydantic_post_init__: ClassVar[None | Literal['model_post_init']]
"""The name of the post-init method for the model, if defined."""

Maybe we should follow that pattern for the new hook?

Edit: If we were creating a model instance level hook, we should probably follow that pattern. But the new hook we're implementing here is more similar to the subclass hook and related to other dunder properties we already set, so I think the current approach is acceptable.

@DouweM DouweM self-assigned this Apr 25, 2025
@DouweM DouweM assigned Viicos and unassigned DouweM Apr 25, 2025
@Viicos Viicos changed the title Add __pydantic_on_complete__ hook that is called once model is fully ready to be used Add __pydantic_on_complete__() hook that is called once model is fully ready to be used Apr 28, 2025
@DouweM DouweM merged commit 34bba45 into pydantic:main Apr 28, 2025
61 checks passed
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