Skip to content

"incompatible type", but casting to correct type complains "redundant cast" #16385

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

Closed
sliedes opened this issue Nov 1, 2023 · 1 comment
Closed
Labels
bug mypy got something wrong topic-casts

Comments

@sliedes
Copy link

sliedes commented Nov 1, 2023

Bug Report

In this code, mypy --strict is unhappy both with and without a cast (playground):

from typing import cast


def foo(name: str) -> None:
    pass


def bar(
    name: str | None = None,
) -> None:
    if name is None:
        name = "foo"

    a = [lambda: foo(name)]  # incompatible type "str | None"; expected "str"
    b = [lambda: foo(cast(str, name))]  # Redundant cast to "str"

    del a, b

Expected Behavior

One of the lines should be ok for mypy.

Actual Behavior

main.py:14: error: Argument 1 to "foo" has incompatible type "str | None"; expected "str"  [arg-type]
main.py:15: error: Redundant cast to "str"  [redundant-cast]
Found 2 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.6.1, master
  • Mypy command-line flags: --strict
  • Python version used: 3.11, 3.12
@sliedes sliedes added the bug mypy got something wrong label Nov 1, 2023
@ilevkivskyi
Copy link
Member

This is essentially a duplicate of #4297

@ilevkivskyi ilevkivskyi closed this as not planned Won't fix, can't repro, duplicate, stale Nov 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-casts
Projects
None yet
Development

No branches or pull requests

3 participants