KEMBAR78
Do not warn about shadowed fields if they are not redefined in a child class by chan-vince · Pull Request #9111 · pydantic/pydantic · GitHub
Skip to content

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

@codspeed-hq
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
Contributor

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

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
Contributor

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
Contributor

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

@pydantic-hooky pydantic-hooky bot added awaiting author revision awaiting changes from the PR author and removed ready for review labels Mar 26, 2024
@pydantic-hooky pydantic-hooky bot assigned chan-vince and unassigned adriangb Mar 26, 2024
Copy link
Contributor

@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
@chan-vince chan-vince deleted the bug/9107-improve-warning-on-parent-inherited-field branch March 26, 2024 19:03
jvansanten added a commit to AmpelProject/Ampel-interface that referenced this pull request May 28, 2024
False-positive warnings appear to have been fixed in
pydantic/pydantic#9111 and possibly
pydantic/pydantic#9128
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting author revision awaiting changes from the PR author relnotes-fix Used for bugfixes.

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