Skip to content

Detect redundant casts after generic callable inference - #22510

Open
pei711 wants to merge 1 commit into
python:masterfrom
pei711:fix-22509-redundant-cast-generic-tuple
Open

pei711 wants to merge 1 commit into
python:masterfrom
pei711:fix-22509-redundant-cast-generic-tuple

Conversation

@pei711

@pei711 pei711 commented Nov 17, 2026

Copy link
Copy Markdown

When a generic function accepts a callable factory, checking its result under an Any context can erase inferred type arguments and hide a cast whose target matches the result type.

Check cast operands without an expected type and add regression coverage for this generic callable case.

Verified with python runtests.py check-warnings.test, python runtests.py testCastToSuperclassNotRedundant, Black, and Ruff.

LLM-assisted: prepared and verified with OpenAI Codex.

Fixes #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]

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

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

cwltool (https://github.com/common-workflow-language/cwltool)
+ cwltool/builder.py: note: In member "generate_arg" of class "Builder":
+ cwltool/builder.py:636:28: error: Redundant cast to "list[str]"  [redundant-cast]

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

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:2791: error: Redundant cast to "tuple[Transport, StreamProtocol]"  [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