Skip to content

Conflict between structural subtyping and self annotated with a type variable #5277

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

Open
nilehmann opened this issue Jun 25, 2018 · 1 comment
Labels

Comments

@nilehmann
Copy link

If I create a protocol with a method that uses a type variable to annotate both self and the first argument of the method, mypy will accept as subtype, classes with methods accepting as first argument a parameter of the same class type. This unrestricted behavior can be used to get around the type system.

T = TypeVar('T')
class Fooable(Protocol):
    def foo(self: T, a: T) -> int:
        ...
class A:
    a = 1
    def foo(self, a: 'A') -> int:
        return self.a + a.a
class B:
    b = 1
    def foo(self, b: 'B') -> int:
        return self.b + b.b
def foo(a: Fooable, b: Fooable) -> None:
    a.foo(b)
foo(A(), B())
@ilevkivskyi
Copy link
Member

This is not something specific to protocols. Exactly the same problem exists for nominal classes that use self-types. So this is almost a duplicate of #2353. I think it not exactly a duplicate because the solution for protocols may be different (but likely we will just go with the option 4 proposed by @JukkaL).

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

2 participants