FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

use is_same_type when determining if a cast is redundant by asottile · Pull Request #18588 · python/mypy · GitHub

Repository navigation

use is_same_type when determining if a cast is redundant - #18588

Merged
hauntsaninja merged 2 commits into
python:masterfrom
asottile:redundant-casts-same-type
Apr 22, 2025
Merged

hauntsaninja merged 2 commits into
python:masterfrom
asottile:redundant-casts-same-type

Conversation

asottile commented Feb 2, 2025

Copy link
Copy Markdown
Contributor

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 commented Feb 2, 2025

Copy link
Copy Markdown
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 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 marked this pull request as ready for review February 2, 2025 20:53

github-actions Bot commented Feb 2, 2025

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/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]

sterliakov left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Also fixes #12328?

hauntsaninja left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Great, thank you!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants


Back | FazBrowse Home | New Git URL