Fix isinstance narrowing for classes with __getattr__ returning Any#21105
Open
jpbeaudry wants to merge 1 commit intopython:masterfrom
Open
Fix isinstance narrowing for classes with __getattr__ returning Any#21105jpbeaudry wants to merge 1 commit intopython:masterfrom
jpbeaudry wants to merge 1 commit intopython:masterfrom
Conversation
Contributor
|
Diff from mypy_primer, showing the effect of this PR on open source code: pandas (https://github.com/pandas-dev/pandas)
+ pandas/core/frame.py:14737: error: Incompatible return value type (got "DataFrame | Series", expected "DataFrame") [return-value]
werkzeug (https://github.com/pallets/werkzeug)
+ src/werkzeug/datastructures/file_storage.py:189: error: Unused "type: ignore" comment [unused-ignore]
|
Author
|
Primer results look correct per predominantly AI analysis:
Both appear to be cases where the fix improves narrowing accuracy rather than introducing false positives. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #16590
When a class has
__getattr__returningAnyand appears in a union with a protocol type, the else-branch ofisinstancefails to narrow the class away.The problem is in
narrow_declared_type()inmeet.py. When processing union members against the narrowed type,is_overlapping_types(A, Protocol)returns True because__getattr__→Anystructurally satisfies any protocol. Thenmeet_types(A, Protocol)returnsA, soAleaks back into the result and the narrowing is effectively undone.The fix: after narrowing a union member
dagainst a protocoln, if the result isdunchanged anddis not nominally related ton(only structurally, via__getattr__), exclude it from the narrowed union.Test added in
check-isinstance.testcovering both__getattr__→Anyand__getattr__→strcases.This is my first contribution to mypy — happy to adjust the approach if there's a better way to handle this.