Skip to content

use is_same_type when determining if a cast is redundant #18588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 22, 2025

Conversation

asottile
Copy link
Contributor

@asottile asottile commented Feb 2, 2025

while working on #18540 (which my original prototype based the code on warn-redundant-casts) I noticed the suggestion here would probably make sense to apply to redundant-cast as well!

I also made sure to include the example from the original implementation just to make sure I wasn't regressing that as well since it seemed related.

This comment has been minimized.

@asottile
Copy link
Contributor Author

asottile commented Feb 2, 2025

almost all of the primer output is an improvement.

I noticed one new bug which was sort of hidden before (for instance from pandera):

from typing import TypeAlias, Any, cast

x: TypeAlias = Any
y: TypeAlias = Any
z: TypeAlias = Any


def f(q: x | y | z) -> None:
    cast(x | y, q)

the current redundant-cast code checks for Any specifically but not for equivalent "Any"s such as Any | Any

I can fix this with is_same_type(target_type, AnyType(TypeOfAny.whatever)) I believe


The other oddity I noticed is the one from antidote where a cast from a clearly abstract protocol to type[ThatProtocol] is written to avoid type-abstract. imo this should be an error and they should ignore type-abstract instead -- here's a minimal example:

from typing import cast, Protocol

def f[T](t: type[T]) -> None: ...

class P[T](Protocol):
    def get(self) -> T: ...

f(P[int])  # type: ignore[type-abstract]
f(cast(type[P[int]], P[int]))  # ok: becomes warn-redundant-cast

@asottile asottile marked this pull request as ready for review February 2, 2025 20:53
Copy link
Contributor

github-actions bot commented Feb 2, 2025

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

prefect (https://github.com/PrefectHQ/prefect)
+ src/prefect/utilities/collections.py:141: error: Redundant cast to "NestedDict[Any]"  [redundant-cast]
+ src/prefect/utilities/collections.py:456: error: Redundant cast to "BaseAnnotation"  [redundant-cast]
+ src/prefect/utilities/collections.py:693: error: Redundant cast to "NestedDict[Any]"  [redundant-cast]
+ src/prefect/utilities/collections.py:720: error: Redundant cast to "Union[VT2?, NestedDict[Any]]"  [redundant-cast]
+ src/prefect/utilities/callables.py:681: error: Redundant cast to "unmapped"  [redundant-cast]

altair (https://github.com/vega/altair)
+ altair/vegalite/v5/api.py:1110: error: Redundant cast to "_Conditional[list[_ConditionClosed]]"  [redundant-cast]

cwltool (https://github.com/common-workflow-language/cwltool)
+ cwltool/update.py: note: In function "v1_0to1_1":
+ cwltool/update.py:135:56: error: Redundant cast to "CWLOutputType"  [redundant-cast]
+ cwltool/secrets.py: note: In member "has_secret" of class "SecretStore":
+ cwltool/secrets.py:46:36: error: Redundant cast to "CWLOutputType"  [redundant-cast]
+ cwltool/secrets.py:50:36: error: Redundant cast to "CWLOutputType"  [redundant-cast]
+ cwltool/secrets.py: note: In member "retrieve" of class "SecretStore":
+ cwltool/secrets.py:61:38: error: Redundant cast to "CWLOutputType"  [redundant-cast]
+ cwltool/secrets.py:63:35: error: Redundant cast to "CWLOutputType"  [redundant-cast]
+ cwltool/cwlprov/ro.py: note: In member "_relativise_files" of class "ResearchObject":
+ cwltool/cwlprov/ro.py:677:44: error: Redundant cast to "CWLOutputType"  [redundant-cast]
+ cwltool/cwlprov/ro.py:685:40: error: Redundant cast to "CWLOutputType"  [redundant-cast]
+ cwltool/process.py: note: In function "avroize_type":
+ cwltool/process.py:447:47: error: Redundant cast to "CWLOutputType"  [redundant-cast]
+ cwltool/process.py: note: In function "var_spool_cwl_detector":
+ cwltool/process.py:493:40: error: Redundant cast to "CWLOutputType"  [redundant-cast]
+ cwltool/process.py:496:40: error: Redundant cast to "CWLOutputType"  [redundant-cast]
+ cwltool/checker.py: note: In function "merge_flatten_type":
+ cwltool/checker.py:57:36: error: Redundant cast to "SinkType"  [redundant-cast]
+ cwltool/checker.py: note: In function "can_assign_src_to_sink":
+ cwltool/checker.py:98:13: error: Redundant cast to "SinkType"  [redundant-cast]
+ cwltool/checker.py:98:42: error: Redundant cast to "SinkType | None"  [redundant-cast]
+ cwltool/checker.py:103:47: error: Redundant cast to "SinkType"  [redundant-cast]
+ cwltool/checker.py:107:62: error: Redundant cast to "SinkType"  [redundant-cast]
+ cwltool/checker.py:112:44: error: Redundant cast to "SinkType"  [redundant-cast]
+ cwltool/pack.py: note: In function "find_ids":
+ cwltool/pack.py:50:22: error: Redundant cast to "CWLOutputType"  [redundant-cast]
+ cwltool/pack.py: note: In function "import_embed":
+ cwltool/pack.py:87:26: error: Redundant cast to "CWLOutputType"  [redundant-cast]
+ cwltool/pack.py:103:26: error: Redundant cast to "CWLOutputType"  [redundant-cast]
+ cwltool/command_line_tool.py: note: In member "_initialworkdir" of class "CommandLineTool":
+ cwltool/command_line_tool.py:519:33: error: Redundant cast to "MutableSequence[CWLOutputType | None]"  [redundant-cast]
+ cwltool/command_line_tool.py: note: In member "collect_output" of class "CommandLineTool":
+ cwltool/command_line_tool.py:1403:38: error: Redundant cast to "CWLOutputType"  [redundant-cast]
+ cwltool/main.py: note: In function "realize_input_schema":
+ cwltool/main.py:308:43: error: Redundant cast to "CWLObjectType"  [redundant-cast]

pandas (https://github.com/pandas-dev/pandas)
+ pandas/core/internals/blocks.py:822: error: Redundant cast to "ExtensionArray | ndarray[Any, Any]"  [redundant-cast]

dd-trace-py (https://github.com/DataDog/dd-trace-py)
+ ddtrace/_trace/utils_botocore/span_pointers/dynamodb.py:488: error: Redundant cast to "_DynamoDBDeleteRequestWriteRequest"  [redundant-cast]
+ ddtrace/_trace/utils_botocore/span_pointers/dynamodb.py:559: error: Redundant cast to "_DynamoDBTransactUpdateItem"  [redundant-cast]

streamlit (https://github.com/streamlit/streamlit)
+ lib/streamlit/elements/lib/color_util.py: note: In function "_normalize_tuple":
+ lib/streamlit/elements/lib/color_util.py:229:24: error: Redundant cast to "Union[Tuple[float, float, float, float], Tuple[int, int, int, int], Tuple[int, int, int, float]]"  [redundant-cast]
+ lib/streamlit/elements/vega_charts.py: note: In member "_vega_lite_chart" of class "VegaChartsMixin":
+ lib/streamlit/elements/vega_charts.py:1971:20: error: Redundant cast to "VegaLiteState"  [redundant-cast]
+ lib/streamlit/elements/plotly_chart.py: note: In function "plotly_chart":
+ lib/streamlit/elements/plotly_chart.py:540:20: error: Redundant cast to "PlotlyState"  [redundant-cast]
+ lib/streamlit/elements/deck_gl_json_chart.py: note: In function "pydeck_chart":
+ lib/streamlit/elements/deck_gl_json_chart.py:522:20: error: Redundant cast to "PydeckState"  [redundant-cast]
+ lib/streamlit/elements/arrow.py: note: In function "dataframe":
+ lib/streamlit/elements/arrow.py:628:20: error: Redundant cast to "DataframeState"  [redundant-cast]
+ lib/streamlit/elements/widgets/time_widgets.py: note: In member "_date_input" of class "TimeWidgetsMixin":
+ lib/streamlit/elements/widgets/time_widgets.py:866:49: error: Redundant cast to "Union[date, datetime, str, Literal['today'], None]"  [redundant-cast]

pylint (https://github.com/pycqa/pylint)
+ pylint/lint/report_functions.py:59: error: Redundant cast to "Literal['convention', 'error', 'fatal', 'info', 'refactor', 'statement', 'warning']"  [redundant-cast]
+ pylint/checkers/raw_metrics.py:28: error: Redundant cast to "Literal['code', 'docstring', 'comment', 'empty']"  [redundant-cast]
+ pylint/checkers/base/basic_checker.py:73: error: Redundant cast to "Literal['function', 'class', 'method', 'module']"  [redundant-cast]

spark (https://github.com/apache/spark)
+ python/pyspark/profiler.py:332: error: Redundant cast to "tuple[float, float, int]"  [redundant-cast]
+ python/pyspark/profiler.py:333: error: Redundant cast to "tuple[float, float, int]"  [redundant-cast]
+ python/pyspark/profiler.py:334: error: Redundant cast to "tuple[float, float, int]"  [redundant-cast]
+ python/pyspark/profiler.py:335: error: Redundant cast to "tuple[float, float, int]"  [redundant-cast]
+ python/pyspark/profiler.py:338: error: Redundant cast to "tuple[float, float, int]"  [redundant-cast]
+ python/pyspark/profiler.py:340: error: Redundant cast to "tuple[float, float, int]"  [redundant-cast]

openlibrary (https://github.com/internetarchive/openlibrary)
+ openlibrary/core/lists/model.py: note: In member "from_json" of class "Seed":
+ openlibrary/core/lists/model.py:458: error: Redundant cast to "ThingReferenceDict"  [redundant-cast]
+ openlibrary/plugins/openlibrary/lists.py: note: In member "normalize_input_seed" of class "ListRecord":
+ openlibrary/plugins/openlibrary/lists.py:91: error: Redundant cast to "ThingReferenceDict"  [redundant-cast]
+ openlibrary/plugins/openlibrary/lists.py: note: At top level:

scrapy (https://github.com/scrapy/scrapy)
+ scrapy/http/request/form.py:110: error: Redundant cast to "Iterable[str]"  [redundant-cast]

sockeye (https://github.com/awslabs/sockeye)
+ sockeye/data_io.py:1364: error: Redundant cast to "Iterator[Any]"  [redundant-cast]
+ sockeye/data_io.py:1365: error: Redundant cast to "Iterator[Any]"  [redundant-cast]

steam.py (https://github.com/Gobot1234/steam.py)
+ steam/ext/commands/help.py:35: error: Redundant cast to "CommandKwargs[Any]"  [redundant-cast]

jax (https://github.com/google/jax)
+ jax/_src/lax/lax.py:665: error: Redundant cast to "str | type[Any] | dtype[Any] | SupportsDType | None"  [redundant-cast]

core (https://github.com/home-assistant/core)
+ homeassistant/components/mealie/services.py:131: error: Redundant cast to "ConfigEntry[MealieData]"  [redundant-cast]
+ homeassistant/components/command_line/switch.py:43: error: Redundant cast to "dict[str, Any]"  [redundant-cast]
+ homeassistant/components/command_line/sensor.py:54: error: Redundant cast to "dict[str, Any]"  [redundant-cast]
+ homeassistant/components/command_line/notify.py:29: error: Redundant cast to "dict[str, Any]"  [redundant-cast]
+ homeassistant/components/command_line/cover.py:44: error: Redundant cast to "dict[str, Any]"  [redundant-cast]
+ homeassistant/components/nws/weather.py:181: error: Redundant cast to "Literal['twice_daily', 'hourly']"  [redundant-cast]
+ homeassistant/components/command_line/binary_sensor.py:46: error: Redundant cast to "dict[str, Any]"  [redundant-cast]

kornia (https://github.com/kornia/kornia)
+ kornia/contrib/extract_patches.py:118: error: Redundant cast to "tuple[int, int, int, int]"  [redundant-cast]
+ kornia/contrib/extract_patches.py:384: error: Redundant cast to "tuple[int, int, int, int]"  [redundant-cast]

pyinstrument (https://github.com/joerick/pyinstrument)
- pyinstrument/renderers/jsonrenderer.py:17: error: Unused "type: ignore" comment  [unused-ignore]

antidote (https://github.com/Finistere/antidote)
+ tests/lib/interface/test_custom.py:300: error: Redundant cast to "type[EventSubscriber[InitializationEvent]]"  [redundant-cast]

dedupe (https://github.com/dedupeio/dedupe)
+ dedupe/api.py:1547: error: Redundant cast to "Literal['match', 'distinct']"  [redundant-cast]

Copy link
Collaborator

@sterliakov sterliakov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also fixes #12328?

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thank you!

@hauntsaninja hauntsaninja merged commit 057f8ad into python:master Apr 22, 2025
18 checks passed
@asottile asottile deleted the redundant-casts-same-type branch April 22, 2025 10:33
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.

None yet

3 participants