Skip to content

Check cast operands without an Any type context - #22537

Open
lau0708 wants to merge 2 commits into
python:masterfrom
lau0708:fix/cast-operand-no-any-context
Open

lau0708 wants to merge 2 commits into
python:masterfrom
lau0708:fix/cast-operand-no-any-context

Conversation

@lau0708

@lau0708 lau0708 commented Nov 22, 2026

Copy link
Copy Markdown

Fixes #22509

What was wrong

visit_cast_expr checked the operand with type_context=AnyType(TypeOfAny.special_form). Passing an Any context disables inference of type variables from the operand's own arguments, so in

def f(factory: Callable[[], T]) -> tuple[A, T]: ...

cast(tuple[A, A], f(A))

f(A) was inferred as tuple[A, Any] (with the Any coming from the context) instead of tuple[A, A]. reveal_type on the same call outside a cast correctly shows tuple[A, A].

Since --warn-redundant-casts compares the operand type to the cast target with is_same_type, the unnecessary cast above was never reported. Because the Any-typed context leaked into the operand, the comparison also had no chance for every generic call whose result is only determined by its arguments.

Fix

Check the operand with no type context (type_context=None) instead of an Any one. A cast has no "expected type" for its operand, so nothing is lost by not providing a context, while local inference from the operand's own arguments is preserved. The redundant-cast comparison then sees tuple[A, A] and reports the cast.

Tests

  • New data-driven test testRedundantCastGenericCallWithTuple in test-data/unit/check-warnings.test, reproducing the issue. Verified red without the fix (Expected: main:10: error: Redundant cast to "tuple[A, A]", Actual: empty) and green with it.
  • pytest mypy/test/testcheck.py -q -n 8: 8361 passed, 16 skipped, 7 xfailed, 0 failed.
  • mypy --config-file mypy_self_check.ini mypy/checkexpr.py: clean. ruff check: clean.

The only behaviour change is that the operand of a cast is now inferred without an Any context; diagnostics that previously depended on that context (e.g. argument checks inside the cast operand) now see the type inferred from the operand itself.

Note: this change was prepared with LLM assistance; the root cause analysis, the red/green test verification, and the full test-suite run above were all done locally and reviewed.

lau0708 and others added 2 commits November 22, 2026 21:29
Passing AnyType(special_form) as the type context suppressed inference
of type variables from the operand's own arguments, so e.g.

    cast(tuple[A, A], f(A))

checked f(A) in a context of Any and inferred tuple[A, Any]. The
redundant-cast check therefore compared tuple[A, Any] with tuple[A, A]
and stayed silent. Checking the operand with no type context keeps the
inference local to the operand and lets --warn-redundant-casts report
such casts (fixes python#22509).
@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/input/run_input.py:527: error: Redundant cast to "type[AutomaticRunInput[T]]"  [redundant-cast]

openlibrary (https://github.com/internetarchive/openlibrary)
+ openlibrary/solr/data_provider.py: note: In member "get_work_reading_log" of class "DatabaseDataProvider":
+ openlibrary/solr/data_provider.py:569: error: Argument 1 to "sum" has incompatible type "dict_values[str, object]"; expected "Iterable[bool]"  [arg-type]

colour (https://github.com/colour-science/colour)
+ colour/plotting/volume.py:506: error: Redundant cast to "list[RGB_Colourspace]"  [redundant-cast]
+ colour/plotting/models.py:539: error: Redundant cast to "list[RGB_Colourspace]"  [redundant-cast]
+ colour/plotting/colorimetry.py:532: error: Redundant cast to "list[MultiSpectralDistributions]"  [redundant-cast]
+ colour/plotting/colorimetry.py:698: error: Redundant cast to "list[SpectralDistribution]"  [redundant-cast]

antidote (https://github.com/Finistere/antidote)
+ tests/core/test_exceptions.py:13: error: Redundant cast to "list[Callable[[object], str]]"  [redundant-cast]

core (https://github.com/home-assistant/core)
+ homeassistant/components/history/__init__.py:130: error: Redundant cast to "Response"  [redundant-cast]
+ homeassistant/components/deconz/light.py:138: error: Redundant cast to "dict[str, dict[str, Any]]"  [redundant-cast]

pytest-robotframework (https://github.com/detachhead/pytest-robotframework)
+ pytest_robotframework/_internal/robot/utils.py:232: error: Redundant cast to "list[object] | None"  [redundant-cast]

pandas-stubs (https://github.com/pandas-dev/pandas-stubs)
+ tests/series/test_add.py:49: error: Unused "type: ignore" comment  [unused-ignore]

bokeh (https://github.com/bokeh/bokeh)
+ src/bokeh/embed/resources.py: note: In member "union" of class "ResourceRequirements":
+ src/bokeh/embed/resources.py:262:22: error: Redundant cast to "tuple[Literal['bokeh/core', 'bokeh/widgets', 'bokeh/tables', 'bokeh/webgl', 'bokeh/mathjax', 'bokeh/api'], ...]"  [redundant-cast]

anyio (https://github.com/agronholm/anyio)
+ src/anyio/_backends/_asyncio.py:2795: error: Redundant cast to "tuple[Transport, StreamProtocol]"  [redundant-cast]

pandas (https://github.com/pandas-dev/pandas)
+ pandas/core/dtypes/concat.py:358: error: Redundant cast to "list[Categorical]"  [redundant-cast]

kopf (https://github.com/nolar/kopf)
+ kopf/_core/engines/posting.py:119: error: Argument 1 to "walk" has incompatible type "Body | Iterable[Body]"; expected "Iterable[Never] | Iterable[Iterable[Never]]"  [arg-type]
+ kopf/_core/engines/posting.py:134: error: Argument 1 to "walk" has incompatible type "Body | Iterable[Body]"; expected "Iterable[Never] | Iterable[Iterable[Never]]"  [arg-type]
+ kopf/_core/engines/posting.py:149: error: Argument 1 to "walk" has incompatible type "Body | Iterable[Body]"; expected "Iterable[Never] | Iterable[Iterable[Never]]"  [arg-type]
+ kopf/_core/engines/posting.py:169: error: Argument 1 to "walk" has incompatible type "Body | Iterable[Body]"; expected "Iterable[Never] | Iterable[Iterable[Never]]"  [arg-type]

meson (https://github.com/mesonbuild/meson)
+ mesonbuild/utils/universal.py:1918:16: error: Redundant cast to "list[_T]"  [redundant-cast]
+ mesonbuild/build.py:1644:33: error: Redundant cast to "list[str | None]"  [redundant-cast]

paasta (https://github.com/yelp/paasta)
+ paasta_tools/utils.py:4281: error: Redundant cast to "list[str]"  [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.

--warn-redundant-casts doesn’t flag cast to a tuple type returned by a generic function

1 participant