Skip to content
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

Incorrect redundant cast warning in lambda #9568

Closed
JelleZijlstra opened this issue Oct 9, 2020 · 3 comments
Closed

Incorrect redundant cast warning in lambda #9568

JelleZijlstra opened this issue Oct 9, 2020 · 3 comments
Labels
bug mypy got something wrong topic-casts

Comments

@JelleZijlstra
Copy link
Member

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
@JelleZijlstra JelleZijlstra added the bug mypy got something wrong label Oct 9, 2020
@hauntsaninja
Copy link
Collaborator

(Aliasing will probably work as a workaround)

@hauntsaninja
Copy link
Collaborator

mypy_primer bisects it to #8965 , I assume it's triggered by the typeshed change to sorted. There's probably some relation to #2608 as well

@hauntsaninja
Copy link
Collaborator

#16407 will make this work if you remove the cast

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

2 participants