Skip to content

Properly wrap custom model_post_init implementation when private attributes are defined #11251

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

Conversation

thomasfrimannkoren
Copy link
Contributor

@thomasfrimannkoren thomasfrimannkoren commented Jan 10, 2025

Change Summary

The docstring of model_post_init is overwritten with the docstring of wrapped_model_post_init.
In order to fix this, I just copy the docstring of model_post_init over to the new wrapped-object.
I also modified an existing test-case to test for this instead of creating a new one.

Related issue number

I think the change is quite small - so I didn't create an issue. I don't mind creating one if it is necessary :-)

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

@github-actions github-actions bot added documentation relnotes-fix Used for bugfixes. labels Jan 10, 2025
Copy link

codspeed-hq bot commented Jan 10, 2025

CodSpeed Performance Report

Merging #11251 will not alter performance

Comparing thomasfrimannkoren:fix_model_post_init_docstring (9a55377) with main (db061c2)

Summary

✅ 45 untouched benchmarks

Copy link
Contributor

github-actions bot commented Jan 10, 2025

Coverage report

Click to see where and how coverage changed

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

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

@thomasfrimannkoren
Copy link
Contributor Author

please review

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.

Thanks for looking at this. I believe it would be better to just apply @functools.wraps() on the new model_post_init

@thomasfrimannkoren
Copy link
Contributor Author

Thanks @Viicos - that is a much more elegant and correct solution!

Copy link
Member

Choose a reason for hiding this comment

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

Let's add an extra test following test_post_init:

def test_post_init_function_attrs_preserved() -> None:
    class Model(BaseModel):
        _a: int  # Necessary to have model_post_init wrapped
        def model_post_init(self, context, /) -> None:
            "Custom docstring"""

    assert Model.model_post_init.__doc__ = "Custom docstring"

And revert the existing test changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the thorough review! Commit reverted, and new test added as requested.

@Viicos Viicos changed the title Ensure docstring of model_post_init is retained Properly wrap custom model_post_init implementation when private attributes are defined Jan 13, 2025
Thomas Frimann Koren added 2 commits January 14, 2025 12:04
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.

This looks great, thanks for the fix.

@Viicos Viicos merged commit 7506b1c into pydantic:main Jan 14, 2025
59 checks passed
@thomasfrimannkoren thomasfrimannkoren deleted the fix_model_post_init_docstring branch January 23, 2025 17:18
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.

3 participants