use is_same_type when determining if a cast is redundant#18588
Merged
hauntsaninja merged 2 commits intopython:masterfrom Apr 22, 2025
Merged
use is_same_type when determining if a cast is redundant#18588hauntsaninja merged 2 commits intopython:masterfrom
hauntsaninja merged 2 commits intopython:masterfrom
Conversation
This comment has been minimized.
This comment has been minimized.
Contributor
Author
|
almost all of the primer output is an improvement. I noticed one new bug which was sort of hidden before (for instance from 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 I can fix this with The other oddity I noticed is the one from 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 |
Contributor
|
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]
|
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.
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.