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

Do not warn about shadowed fields if they are not redefined in a child class #9111

Conversation

chan-vince
Copy link
Contributor

@chan-vince chan-vince commented Mar 26, 2024

Change Summary

Fix for issue #9107.

Adds another early exit condition when evaluating whether to log a warning message during detection for shadowed fields.

In the case where a field is defined in a parent class, but it has not been defined at all in a child class, it is technically not a shadowed field, and so shouldn't be warned as such.

Note this is very different from the case where a child class does redefine a field but with a narrower type or even defined as the same type but with a different default. Conceptually this is probably ok, but checking for that is quite complex and this PR does not attempt to try. So this is about checking if a field is defined or not defined - if it is, regardless of type or default value, the warning message is still logged.

Example of a case where a warning is currently logged, but is now no longer logged:

class MyMixin:
    my_field: bool = False

class MyModel(BaseModel, MyMixin):
    pass

Example of a case where a warning is still logged, and it is important to do so:

class MyMixin:
    my_field: bool = False

class MyModel(BaseModel, MyMixin):
    my_field: bool = True

Related issue number

fix #9107

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: @adriangb

Copy link

codspeed-hq bot commented Mar 26, 2024

CodSpeed Performance Report

Merging #9111 will not alter performance

Comparing chan-vince:bug/9107-improve-warning-on-parent-inherited-field (f42b70a) with main (e3b4633)

Summary

✅ 10 untouched benchmarks

@chan-vince
Copy link
Contributor Author

please review

@sydney-runkle
Copy link
Member

@chan-vince,

Looks great. Could you add a test for each of those above cases, showcasing that a warning is raised for one and not the other?

Comment on lines 3164 to 3165
with warnings.catch_warnings():
warnings.simplefilter('error')
Copy link
Member

Choose a reason for hiding this comment

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

Why is this necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I misunderstood the simplefilter() docs - I have adjusted it now.

Essentially follows this example from the docs, to ensure that all warnings are captured, then run the code, then check the recorded warnings list is empty.

Copy link
Member

Choose a reason for hiding this comment

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

Ahh I see, looks good.

Copy link
Member

@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 good overall, just one comment!

Copy link
Member

@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.

Thanks for adding the test!

@sydney-runkle sydney-runkle enabled auto-merge (squash) March 26, 2024 18:42
@sydney-runkle sydney-runkle merged commit 09d2036 into pydantic:main Mar 26, 2024
51 checks passed
@chan-vince chan-vince deleted the bug/9107-improve-warning-on-parent-inherited-field branch March 26, 2024 19:03
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.

UserWarning: Field name "XXX" shadows an attribute in parent "XXX"
3 participants