Skip to content

Do not use types as error contexts during type checking - #22156

Open
ilevkivskyi wants to merge 2 commits into
python:masterfrom
ilevkivskyi:fix-some-lineno
Open

ilevkivskyi wants to merge 2 commits into
python:masterfrom
ilevkivskyi:fix-some-lineno

Conversation

@ilevkivskyi

@ilevkivskyi ilevkivskyi commented Oct 9, 2026 •

Copy link
Copy Markdown
Member

Fixes #16175
Fixes #8592

Situations where an error has a wrong line number are very annoying (maybe even more than a crash). Using types as error context during type checking is really risky. So, I removed all such cases in checker.py in checkexpr.py (by locally removing Context as a base class for Type). Most remaining uses are in semanal.py and typeanal.py. Those are less risky, and trickier to remove.

Notes:

  • This will cause less precise error locations, but IMO it is much better than some random bizarre locations.
  • I only add tests for situations with known repros.
  • While I am at it, I do not emit variance errors for lambdas, unless I a missing something, those are bogus.

cc @JukkaL

@github-actions

This comment has been minimized.

@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@ilevkivskyi ilevkivskyi changed the title Do not use types as contexts during type checking Do not use types as error contexts during type checking Oct 10, 2026
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.

Error location show incorrect file. Covariance error inferred for lambda has invalid line number associated with it

1 participant