Repository navigation
Fix non-local partial None detection for non-literal None assignments - #22473
Open
Jahongir-Qurbonov wants to merge 1 commit into
Open
Jahongir-Qurbonov wants to merge 1 commit into
Jahongir-Qurbonov wants to merge 1 commit into
Conversation
python#22437 made semantic analysis mark a function to be checked together with the module top level (needed for non-local partial None types) only when the variable's first assignment has a literal `None` rvalue. However, the checker creates a partial None type for any inferred variable whose initial type is None, so a for loop index iterating over `[None]`, an assignment expression target, or `x = None if c else None` no longer got completed in functions using `global`/`nonlocal` or `self.x = ...`, and produced spurious errors such as "Incompatible types in assignment (expression has type "int", variable has type "None")". Treat binding forms without an rvalue (for loop index, assignment expression target, with/match/star targets, unmatched tuple items) as possibly None, and also look through conditional expressions and assignment expressions in the rvalue. Other rvalues are still treated as not None to keep the speedup from python#22437. In particular, `x = f()` where `f` returns None is intentionally not handled, since it is already reported as a func-returns-value error.
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
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.
#22437 changed which functions get processed together with the module top level, which is what makes non-local partial
Nonetypes work. It now does this only for variables whose first assignment has a literalNonervalue. The checker, however, creates a partialNonetype for any inferred variable whose initial type isNone, including variables that have no rvalue at all. As a result this now fails:Before #22437 this was accepted and
vwas inferred asint | None. The same regression happens fory = (z := None)(forz), forw = None if c else None, and for class attributes initialized this way and later assigned viaself.a = ...in a method.This PR treats binding forms that have no rvalue as possibly
None, as they were before #22437. These are for loop indices, assignment expression targets,with/match/star targets and tuple items that can't be matched to an rvalue item. It also looks through conditional expressions and assignment expressions in the rvalue. All other rvalues still count as notNone, so the speedup from #22437 is kept. This leaves one case unhandled on purpose:x = f()wherefreturnsNone. That assignment is already reported as afunc-returns-valueerror.Added
testPartialNoneTypeNonLiteralNonetocheck-inference.test, next to the test added in #22437. It covers conditional expressions, walrus targets and for loop indices withglobal, and conditional expressions and walrus rvalues for class attributes assigned viaself.