Skip to content

Preserve overload precision when nested Any is not ambiguous - #22293

Open
subotac wants to merge 1 commit into
python:masterfrom
subotac:fix/nested-any-overload-selection
Open

subotac wants to merge 1 commit into
python:masterfrom
subotac:fix/nested-any-overload-selection

Conversation

@subotac

@subotac subotac commented Oct 25, 2026

Copy link
Copy Markdown

Fixes #22174.

Keep the first matching overload when the argument is a proper subtype of every matching parameter and the overlap does not depend on Any. Preserve conservative handling for starred and callable arguments. Adds nominal-generic and reversed-list regressions.

Validation: 283 overload/integration tests passed (1 skipped), mypy self-check and changed-file pre-commit hooks passed. The full type-check suite passed 8,347 tests; testIgnoreWholeModule5 and testIgnoreWholeModule6 also fail on pristine upstream in this environment.

@github-actions

Copy link
Copy Markdown
Contributor

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

scrapy (https://github.com/scrapy/scrapy)
+ tests/utils/decorators.py:39: error: Unused "type: ignore" comment  [unused-ignore]

core (https://github.com/home-assistant/core)
- homeassistant/components/esphome/climate.py:211: error: Returning Any from function declared to return "HVACMode | None"  [no-any-return]
- homeassistant/components/esphome/climate.py:221: error: Returning Any from function declared to return "HVACAction | None"  [no-any-return]
- homeassistant/components/esphome/climate.py:229: error: Returning Any from function declared to return "str | None"  [no-any-return]
- homeassistant/components/esphome/climate.py:237: error: Returning Any from function declared to return "str | None"  [no-any-return]
- homeassistant/components/esphome/climate.py:246: error: Returning Any from function declared to return "str | None"  [no-any-return]
- homeassistant/components/esphome/alarm_control_panel.py:108: error: Returning Any from function declared to return "AlarmControlPanelState | None"  [no-any-return]
- homeassistant/components/esphome/fan.py:161: error: Returning Any from function declared to return "str | None"  [no-any-return]
- homeassistant/components/esphome/media_player.py:126: error: Returning Any from function declared to return "MediaPlayerState | None"  [no-any-return]
- homeassistant/components/esphome/water_heater.py:99: error: Returning Any from function declared to return "str | None"  [no-any-return]

pandas-stubs (https://github.com/pandas-dev/pandas-stubs)
+ tests/scalars/test_scalars.py:914: error: Unused "type: ignore" comment  [unused-ignore]
+ tests/scalars/test_scalars.py:915: error: Unused "type: ignore" comment  [unused-ignore]
+ tests/scalars/test_scalars.py:917: error: Unused "type: ignore" comment  [unused-ignore]
+ tests/scalars/test_scalars.py:918: error: Unused "type: ignore" comment  [unused-ignore]
+ tests/scalars/test_scalars.py:920: error: Unused "type: ignore" comment  [unused-ignore]
+ tests/scalars/test_scalars.py:921: error: Unused "type: ignore" comment  [unused-ignore]
+ tests/scalars/test_scalars.py:1299: error: Unused "type: ignore" comment  [unused-ignore]
+ tests/scalars/test_scalars.py:1300: error: Unused "type: ignore" comment  [unused-ignore]
+ tests/scalars/test_scalars.py:1362: error: Unused "type: ignore" comment  [unused-ignore]
+ tests/scalars/test_scalars.py:1363: error: Unused "type: ignore" comment  [unused-ignore]
+ tests/scalars/test_scalars.py:1365: error: Unused "type: ignore" comment  [unused-ignore]
+ tests/scalars/test_scalars.py:1366: error: Unused "type: ignore" comment  [unused-ignore]
+ tests/scalars/test_scalars.py:1368: error: Unused "type: ignore" comment  [unused-ignore]
+ tests/scalars/test_scalars.py:1369: error: Unused "type: ignore" comment  [unused-ignore]

rotki (https://github.com/rotki/rotki)
+ rotkehlchen/exchanges/coinbase.py:597: error: Incompatible types in assignment (expression has type "dict[str, list[dict[Any, Any]]]", variable has type "defaultdict[str, list[dict[Any, Any]]]")  [assignment]

pytest (https://github.com/pytest-dev/pytest)
+ src/_pytest/monkeypatch.py:443: error: "Mapping[Any, Any]" has no attribute "__delitem__"; maybe "__getitem__"?  [attr-defined]
+ src/_pytest/monkeypatch.py:448: error: Unsupported target for indexed assignment ("Mapping[Any, Any]")  [index]

pandas (https://github.com/pandas-dev/pandas)
+ pandas/core/arrays/string_.py:1023: error: Redundant cast to "ndarray[tuple[Any, ...], dtype[numpy.bool[builtins.bool]]]"  [redundant-cast]
+ pandas/core/arrays/sparse/array.py:3186: error: "ndarray[tuple[Any, ...], dtype[numpy.bool[builtins.bool]]]" has no attribute "sp_index"  [attr-defined]
+ pandas/core/arrays/sparse/array.py:3194: error: Incompatible types in assignment (expression has type "ExtensionArray | ndarray[tuple[Any, ...], dtype[Any]]", variable has type "ndarray[tuple[Any, ...], dtype[Any]]")  [assignment]

jinja (https://github.com/pallets/jinja)
- src/jinja2/utils.py:570: error: Returning Any from function declared to return "Iterator[Any]"  [no-any-return]

cvxpy (https://github.com/cvxpy/cvxpy)
- cvxpy/reductions/solvers/conic_solvers/pdlp_conif.py:81: error: Incompatible types in assignment (expression has type "csr_array[Any, Any]", variable has type "csc_array[Any]")  [assignment]
+ cvxpy/reductions/solvers/conic_solvers/pdlp_conif.py:81: error: Incompatible types in assignment (expression has type "csr_array[Any, tuple[int, int]]", variable has type "csc_array[Any]")  [assignment]
- cvxpy/reductions/solvers/conic_solvers/glop_conif.py:81: error: Incompatible types in assignment (expression has type "csr_array[Any, Any]", variable has type "csc_array[Any]")  [assignment]
+ cvxpy/reductions/solvers/conic_solvers/glop_conif.py:81: error: Incompatible types in assignment (expression has type "csr_array[Any, tuple[int, int]]", variable has type "csc_array[Any]")  [assignment]

poetry (https://github.com/python-poetry/poetry)
+ tests/console/commands/test_show.py:43: error: Redundant cast to "F"  [redundant-cast]

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.

[mypy 2.4 regression] Incorrect type narrowing for reversed list of tuples

1 participant