Skip to content

Report comprehension index errors at the index variable - #22266

Open
freddyuniverse wants to merge 1 commit into
python:masterfrom
freddyuniverse:fix-comprehension-annotation-line
Open

freddyuniverse wants to merge 1 commit into
python:masterfrom
freddyuniverse:fix-comprehension-annotation-line

Conversation

@freddyuniverse

Copy link
Copy Markdown

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 annotation
or 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 the
element line.

This PR passes the index expression as the error context in
check_for_comp, so the error lands on the for ... in ... clause that
defines the variable. With nested for clauses, each error now points
at its own clause. Single-line comprehensions report on the same line as
before; only the column changes, to the variable.

Tests: I added testInferenceOfIndexInMultiLineComprehension and an
_old_parser copy to check-inference.test. They cover a generator
passed to a call, list/set/dict comprehensions, a nested for clause,
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: 346
    passed, 1 xfailed
  • python runtests.py self: no issues
  • pre-commit run on the changed files (black, ruff, codespell): passed

One user-visible effect: a # type: ignore placed on the first line of a
multi-line comprehension to silence one of these errors would now be
unused, and the error would show up on the for line instead.

I used an AI assistant while working on this. I reviewed the change and
the tests myself and can answer questions about them.

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.
@github-actions

Copy link
Copy Markdown
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

var-annotated errors for iteration should point to the line where the variable is defined

1 participant