Repository navigation
Conversation
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).
for more information, see https://pre-commit.ci
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]
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #22509
What was wrong
visit_cast_exprchecked the operand withtype_context=AnyType(TypeOfAny.special_form). Passing anAnycontext disables inference of type variables from the operand's own arguments, so inf(A)was inferred astuple[A, Any](with theAnycoming from the context) instead oftuple[A, A].reveal_typeon the same call outside a cast correctly showstuple[A, A].Since
--warn-redundant-castscompares the operand type to the cast target withis_same_type, the unnecessary cast above was never reported. Because theAny-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 anAnyone. Acasthas 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 seestuple[A, A]and reports the cast.Tests
testRedundantCastGenericCallWithTupleintest-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
castis now inferred without anAnycontext; 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.