8000 "incompatible type", but casting to correct type complains "redundant cast" · Issue #16385 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content
"incompatible type", but casting to correct type complains "redundant cast" #16385
Closed as not planned
@sliedes

Description

@sliedes

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0