Skip to content

mypy cannot decide if a cast is necessary or redundant #21607

@clo-vis

Description

@clo-vis

Bug Report

Here a case where mypy says a cast is redundant, but if you remove the cast, mypy cannot infer the right type.

To Reproduce

Gist
Playground

Expected Behavior
Either no error with the cast or no error without the cast.

Actual Behavior
Using a type cast, mypy says it's not needed.
Without the type cast, mypy cannot infer the type.

Your Environment

  • Mypy version used: 2.10
  • Mypy configuration options from mypy.ini (and other config files): warn_redundant_casts = true
  • Python version used: 3.14

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-inferenceWhen to infer types or require explicit annotationstopic-literal-types
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions