Fix type narrowing for variable swaps after isinstance checks#21624
Open
BHUVANSH855 wants to merge 2 commits into
Open
Fix type narrowing for variable swaps after isinstance checks#21624BHUVANSH855 wants to merge 2 commits into
BHUVANSH855 wants to merge 2 commits into
Conversation
Contributor
|
Diff from mypy_primer, showing the effect of this PR on open source code: discord.py (https://github.com/Rapptz/discord.py)
- discord/app_commands/commands.py:1583: error: Incompatible types in assignment (expression has type "str", variable has type "locale_str | None") [assignment]
- discord/app_commands/commands.py:1595: error: Incompatible types in assignment (expression has type "str", variable has type "locale_str | None") [assignment]
|
Author
|
I investigated the mypy-primer report from discord.py. The reported locations are tuple assignments of the form: which are directly affected by this change. I tried reproducing the pattern locally with a minimal example and mypy reports no issues: All existing mypy tests also pass, including the full testcheck suite. I'd appreciate maintainer feedback on whether the primer diffs represent genuine newly-detected issues or indicate a problem with the implementation. |
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 #21586
Summary
This PR fixes incorrect type narrowing when variables are swapped after an
isinstance()check.Previously, in code such as:
mypy incorrectly revealed
basSub1instead ofSub2.The issue occurred because narrowed binder information for RHS variables could be overwritten while processing a multi-assignment statement. This change captures narrowed RHS
NameExprtypes before assignments are applied, preserving simultaneous assignment semantics.Changes
Capture narrowed RHS variable types before processing multi-assignment pairs.
Use the captured types when checking assignments to avoid interference from earlier assignments in the same statement.
Add regression tests covering:
isinstance()narrowing.Testing
Verified with:
python -m pytest mypy/test/testcheck.py -k testIsInstanceVariableSwapNarrowing -n0python -m pytest mypy/test/testcheck.py -k testTupleSwapAfterIsinstance -n0python -m pytest mypy/test/testcheck.py -k narrowing -n0python -m pytest mypy/test/testcheck.py -k assignment -n0python -m pytest mypy/test/testcheck.py -k tuple -n0python -m pytest mypy/test/testcheck.py -k isinstance -n0python -m pytest mypy/test/testcheck.py -n0python -m pytest mypy/test/testcheck.py -n autoAll tests pass.