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

Skip to content

Calling functions with Protocols that involve operant invoked functions fail #9305

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
PartiallyUntyped opened this issue Aug 14, 2020 · 2 comments
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-protocols

Comments

@PartiallyUntyped
Copy link

PartiallyUntyped commented Aug 14, 2020

  • Are you reporting a bug, or opening a feature request? Bug
T = TypeVar("T")


class ClosedUnderFormat(Protocol[T]):
    def format(self: "ClosedUnderFormat"[T], other: "ClosedUnderFormat"[T]) -> "ClosedUnderFormat[T]":
        pass


class ClosedUnderAddition(Protocol[T]):
    def __add__(self: "ClosedUnderAddition"[T], other: "ClosedUnderAddition"[T]) -> "ClosedUnderAddition[T]":
        pass


class Bar(int):
    def format(self: int, other: int):
        return ""


def foofmt(x: ClosedUnderFormat[T], y: ClosedUnderFormat[T]) -> ClosedUnderFormat[T]:
    return x.format(y)


def fooadd(x: ClosedUnderAddition[T], y: ClosedUnderAddition[T]) -> ClosedUnderAddition[T]:
    return x.__add__(y)


def fooadd_operant(x: ClosedUnderAddition[T], y: ClosedUnderAddition[T]) -> ClosedUnderAddition[T]:
    return x + y


foofmt("foo", "foo") # ok
foofmt("foo", 10) # true positive - caught 
foofmt(Bar(10), Bar(11)) # true positive - caught

fooadd("foo", "foo") # False positive - caught but shouldn't
fooadd(10, 10) # False positive - caught but shouldn't
fooadd_operant(10, 10) # False positive - caught but shouldn't
  • What is the actual behavior/output?
pythoid/definitions.py:40: error: Argument 2 to "foofmt" has incompatible type "int"; expected "ClosedUnderFormat[<nothing>]" <-- Correct
pythoid/definitions.py:41: error: Argument 1 to "foofmt" has incompatible type "Bar"; expected "ClosedUnderFormat[<nothing>]" <-- Correct
pythoid/definitions.py:41: error: Argument 2 to "foofmt" has incompatible type "Bar"; expected "ClosedUnderFormat[<nothing>]" <-- Correct
pythoid/definitions.py:43: error: Argument 1 to "fooadd" has incompatible type "str"; expected "ClosedUnderAddition[<nothing>]" <-- Wrong, str implements .__add__
pythoid/definitions.py:43: error: Argument 2 to "fooadd" has incompatible type "str"; expected "ClosedUnderAddition[<nothing>]" <-- Wrong, str implements .__add__
pythoid/definitions.py:44: error: Argument 1 to "fooadd" has incompatible type "int"; expected "ClosedUnderAddition[<nothing>]" <-- Wrong, int implements .__add__
pythoid/definitions.py:44: error: Argument 2 to "fooadd" has incompatible type "int"; expected "ClosedUnderAddition[<nothing>]" <-- Wrong, int implements .__add__
pythoid/definitions.py:45: error: Argument 1 to "fooadd_operant" has incompatible type "int"; expected "ClosedUnderAddition[<nothing>]" <-- Wrong, int implements .__add__, invoked by +
pythoid/definitions.py:45: error: Argument 2 to "fooadd_operant" has incompatible type "int"; expected "ClosedUnderAddition[<nothing>]" <-- Wrong, int implements .__add__, invoked by +
  • What is the behavior/output you expect?
pythoid/definitions.py:40: error: Argument 2 to "foofmt" has incompatible type "int"; expected "ClosedUnderFormat[<nothing>]" <-- Correct
pythoid/definitions.py:41: error: Argument 1 to "foofmt" has incompatible type "Bar"; expected "ClosedUnderFormat[<nothing>]" <-- Correct
pythoid/definitions.py:41: error: Argument 2 to "foofmt" has incompatible type "Bar"; expected "ClosedUnderFormat[<nothing>]" <-- Correct
  • What are the versions of mypy and Python you are using?

    • mypy0.782
    • Python 3.8.3
  • Do you see the same issue after installing mypy from Git master? Yes

  • What are the mypy flags you are using? None

@JukkaL JukkaL added bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-protocols labels Aug 14, 2020
@JukkaL
Copy link
Collaborator

JukkaL commented Aug 14, 2020

Yeah, this seems inconsistent.

@ilevkivskyi
Copy link
Member

I think this is a duplicate of #3829. Btw thank for the real life example!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-protocols
Projects
None yet
Development

No branches or pull requests

3 participants