Skip to content
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

Fix duplicate calls to __set_name__ for non-private attributes in BaseModel.__new__ #4410

Merged
merged 3 commits into from Aug 22, 2022

Conversation

tlambert03
Copy link
Contributor

@tlambert03 tlambert03 commented Aug 21, 2022

Change Summary

My apologies @samuelcolvin, as I was working on adding the test you requested in #4407 (comment), I found a bug in my implementation, which is that __set_name__ was now getting called twice for public attributes (i.e. anything in new_namespace), and only once for private attributes. I didn't notice the auto-merge in time :)

This PR fixes that, adds a test that __set_name__ is called only once both for regular object subclasses as well as ModelPrivateAttrs, and adds the test requested above to make sure the bound method works (for regular objects... privateAttrs likely won't work for this pattern, as commented in the test)

skip change file check

Related issue number

Checklist

  • Unit tests for the changes exist
  • Tests pass on CI and coverage remains at 100%
  • Documentation reflects the changes where applicable
  • changes/<pull request or issue id>-<github username>.md file added describing change
    (see changes/README.md for details)
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

@tlambert03
Copy link
Contributor Author

please review

@samuelcolvin
Copy link
Member

Ok, good catch.

I'm afk, but I'll review this and revert the other pr when I'm back.

Copy link
Member

@samuelcolvin samuelcolvin left a comment

Choose a reason for hiding this comment

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

otherwise LGTM.

please update.

@@ -0,0 +1 @@
Fix duplicate calls to `__set_name__` for non-private attributes in `BaseModel.__new__`. Add more tests.
Copy link
Member

Choose a reason for hiding this comment

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

This file can be removed, if anything, update the change from #4407.

You'll need to add "skip change file check" to the PR body.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, all set this time for real 😂
sorry for the 2-step PR!

@samuelcolvin
Copy link
Member

LGTM, @tlambert03 are you happy for this to be merged? (Just double checking 😆)

@tlambert03
Copy link
Contributor Author

LGTM, @tlambert03 are you happy for this to be merged? (Just double checking 😆)

🥴 yes! this time for realz

@samuelcolvin samuelcolvin enabled auto-merge (squash) August 22, 2022 11:06
@samuelcolvin samuelcolvin merged commit beb3e4c into pydantic:main Aug 22, 2022
@tlambert03 tlambert03 deleted the fix-double-self-call branch August 22, 2022 11:17
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.

None yet

4 participants