Skip to content

Incorrect redundant cast warning in lambda #9568

Closed
@JelleZijlstra

Description

@JelleZijlstra

With a cast() in a lambda, --warn-redundant-cast tells me to remove the cast, but then I get a different error if I actually do so. This is a regression in 0.790.

$ cat tmp/matchcast.py 
from typing import cast, Match, Pattern


def _get_nodes(key: str, rgx: Pattern[str]) -> None:
    matches = rgx.fullmatch(key)
    if matches is None:
        return

    sorted(
        matches.groupdict().items(),
        key=lambda g: cast(Match[str], matches).start(g[0]),
    )
$ mypy --warn-redundant-casts tmp/matchcast.py 
tmp/matchcast.py:11: error: Redundant cast to "Match[str]"
Found 1 error in 1 file (checked 1 source file)

And if I remove the cast:

$ mypy --warn-redundant-casts tmp/matchcast.py 
tmp/matchcast.py:11: error: Item "None" of "Optional[Match[str]]" has no attribute "start"
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 0.790
  • Mypy command-line flags: --warn-redundant-casts
  • Python version used: 3.6
  • Operating system and version: Ubuntu

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions