Repository navigation
Report comprehension index errors at the index variable - #22266
Open
freddyuniverse wants to merge 1 commit into
Open
freddyuniverse wants to merge 1 commit into
freddyuniverse wants to merge 1 commit into
Conversation
Errors from assigning a comprehension's item type to its index variable, such as "Need type annotation" or unpacking errors, were reported at the start of the whole comprehension. For multi-line comprehensions this pointed at the wrong line, and for generator expressions the line also depended on which parser was used. Use the index expression as the error context instead, so the error lands on the `for ... in ...` clause that defines the variable. Fixes python#22265.
Contributor
|
Diff from mypy_primer, showing the effect of this PR on open source code: prefect (https://github.com/PrefectHQ/prefect)
- src/prefect/utilities/collections.py:507: error: "None" object is not iterable [misc]
+ src/prefect/utilities/collections.py:508: error: "None" object is not iterable [misc]
- src/prefect/server/events/storage/__init__.py:80: error: "int" object is not iterable [misc]
+ src/prefect/server/events/storage/__init__.py:88: error: "int" object is not iterable [misc]
jax (https://github.com/google/jax)
- jax/_src/api_util.py:769: error: Need type annotation for "path" [var-annotated]
+ jax/_src/api_util.py:771: error: Need type annotation for "path" [var-annotated]
ibis (https://github.com/ibis-project/ibis)
- ibis/backends/sql/datatypes.py:1794: error: Need type annotation for "mapper" [var-annotated]
+ ibis/backends/sql/datatypes.py:1796: error: Need type annotation for "mapper" [var-annotated]
- ibis/expr/types/strings.py:973: error: Need type annotation for "pattern" [var-annotated]
+ ibis/expr/types/strings.py:975: error: Need type annotation for "pattern" [var-annotated]
- ibis/expr/types/strings.py:1019: error: Need type annotation for "pattern" [var-annotated]
+ ibis/expr/types/strings.py:1021: error: Need type annotation for "pattern" [var-annotated]
- ibis/expr/types/relations.py:1141: error: Need type annotation for "arg" [var-annotated]
+ ibis/expr/types/relations.py:1143: error: Need type annotation for "arg" [var-annotated]
sphinx (https://github.com/sphinx-doc/sphinx)
+ sphinx/testing/util.py:80:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
static-frame (https://github.com/static-frame/static-frame)
- static_frame/core/frame.py:7193: error: Need type annotation for "name" [var-annotated]
- static_frame/core/frame.py:7436: error: Need type annotation for "name" [var-annotated]
scipy-stubs (https://github.com/scipy/scipy-stubs)
- tests/misc/test_submodules_empty.pyi:3: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
|
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 #22265.
When mypy checks a comprehension, it assigns each item type to the index
variable. Any error from that assignment, such as
Need type annotationor an unpacking error, was reported at the start of the whole
comprehension. That's the wrong line for a multi-line comprehension. For
a generator expression, the line also depended on the parser: the old
parser reported the
sum(line and the native parser reported theelement line.
This PR passes the index expression as the error context in
check_for_comp, so the error lands on thefor ... in ...clause thatdefines the variable. With nested
forclauses, each error now pointsat its own clause. Single-line comprehensions report on the same line as
before; only the column changes, to the variable.
Tests: I added
testInferenceOfIndexInMultiLineComprehensionand an_old_parsercopy tocheck-inference.test. They cover a generatorpassed to a call, list/set/dict comprehensions, a nested
forclause,and an unpacking error. Both fail on master and pass with the change.
No existing expected outputs needed updating.
Locally (Python 3.13, Linux):
testcheck,testfinegrained,testsemanal,testtypegen,testerrorstream,testmerge,testdeps: 10010 passed, 56 skipped,7 xfailed
testpythoneval,testcmdline,testdaemon,testreports: 346passed, 1 xfailed
python runtests.py self: no issuespre-commit runon the changed files (black, ruff, codespell): passedOne user-visible effect: a
# type: ignoreplaced on the first line of amulti-line comprehension to silence one of these errors would now be
unused, and the error would show up on the
forline instead.I used an AI assistant while working on this. I reviewed the change and
the tests myself and can answer questions about them.