Skip to content

Correctly infer TypeVarTuple against Any - #22140

Open
ilevkivskyi wants to merge 1 commit into
python:masterfrom
ilevkivskyi:tvt-no-union
Open

ilevkivskyi wants to merge 1 commit into
python:masterfrom
ilevkivskyi:tvt-no-union

Conversation

@ilevkivskyi

Copy link
Copy Markdown
Member

Fixes #22108

This has two parts:

  • Use proper constraint target for TypeVarTuples. Any is special-cased in expand_type() (for convenience), but we should not abuse that.
  • Do not apply "union with Any" special-casing to ParamSpec and TypeVarTuple. These can't really be unions.

Either of the two would be sufficient to fix the crash, but I apply them both, since both make sense on they own (even without the crash). Note I don't try to improve the inference here, I am just fixing the crash. We are already doing a decent inference here. Also the ParamSpec test doesn't crash on master (since we don't infer it against Any ATM), but it would be good to have it to prevent possible regressions.

cc @JukkaL

@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

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

@ilevkivskyi

Copy link
Copy Markdown
Member Author

Btw @hauntsaninja do you have any ideas why we have the spurious diff from scipy-stubs? There is a # type: ignore[import-not-found] on that line, and I don't see any errors there (nor notes) when I try to run mypy on it locally.

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.

Crash with RuntimeError: Invalid type replacement to expand: tuple[*tuple[Any, ...]] | Any

2 participants