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
Do not warn about shadowed fields if they are not redefined in a child class #9111
Conversation
CodSpeed Performance ReportMerging #9111 will not alter performanceComparing Summary
|
please review |
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? |
tests/test_main.py
Outdated
with warnings.catch_warnings(): | ||
warnings.simplefilter('error') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this necessary?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this 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!
There was a problem hiding this 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!
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:
Example of a case where a warning is still logged, and it is important to do so:
Related issue number
fix #9107
Checklist
Selected Reviewer: @adriangb