Thanks to visit codestin.com
Credit goes to github.com

Skip to content

warn redundant casts doesn't work with Protocol.__call__ #12328

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
graingert opened this issue Mar 10, 2022 · 3 comments
Closed

warn redundant casts doesn't work with Protocol.__call__ #12328

graingert opened this issue Mar 10, 2022 · 3 comments
Labels

Comments

@graingert
Copy link
Contributor

graingert commented Mar 10, 2022

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.10&flags=strict%2Cwarn-redundant-casts&gist=bc9999020b0fb32610c0c3ec99a8568c

from typing import Protocol, cast

class FooType(Protocol):
    def __call__(self, ham: str) -> str:
        pass

def foo(ham: str) -> str:
    pass


v = cast(FooType, foo)
u: FooType = foo

Expected Behavior
I was expecting v = cast(FooType, foo) to work about the same as v: FooType = foo # type: ignore[assignment] with warn redundant ignores

Your Environment

See mypy-play

@graingert graingert added the bug mypy got something wrong label Mar 10, 2022
@graingert
Copy link
Contributor Author

graingert commented Mar 10, 2022

It seems even very simple redundant casts are not detected:

from typing import cast

class Foo:
    pass

class Bar(Foo):
    pass

v = cast(Foo, Bar())

https://mypy-play.net/?mypy=latest&python=3.10&flags=strict%2Cwarn-redundant-casts&gist=c166276e9623ced551d45e7a74d31510

@jhance
Copy link
Collaborator

jhance commented Mar 11, 2022

Is the behavior with respect to subtyping documented?

@graingert
Copy link
Contributor Author

The docs refer to casts that can be safely removed:

https://mypy.readthedocs.io/en/stable/command_line.html#cmdoption-mypy-warn-redundant-casts

This flag will make mypy report an error whenever your code uses an unnecessary cast that can safely be removed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants