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

Type annotation on self is not enforced correctly for generic classes #5305

Closed
pnachum opened this issue Jul 2, 2018 · 3 comments · Fixed by #7860
Closed

Type annotation on self is not enforced correctly for generic classes #5305

pnachum opened this issue Jul 2, 2018 · 3 comments · Fixed by #7860
Labels
bug mypy got something wrong priority-2-low

Comments

@pnachum
Copy link

pnachum commented Jul 2, 2018

This code doesn't produce any type errors, but will crash at runtime when calling len with an int.

T = TypeVar('T')

class Box(Generic[T]):
    
    def __init__(self, val: T) -> None:
        self.val = val
		
    def foo(self: 'Box[str]') -> 'int':
        return len(self.val)

Box(1).foo()

Calling the foo method like this does result in a type error

Box.foo(Box(1))
@pnachum pnachum changed the title Type annotation on self is not enforced correctly for generic classes Type annotation on self is not enforced correctly for generic classes Jul 2, 2018
@ilevkivskyi
Copy link
Member

Hm, interesting. I think annotations on self apart from self-type-like (capturing type variable) should be prohibited.

@ilevkivskyi ilevkivskyi added bug mypy got something wrong priority-2-low labels Jul 2, 2018
@pnachum
Copy link
Author

pnachum commented Jul 2, 2018

I guess that would make this scenario impossible to express until #2354 is resolved?

@elazarg
Copy link
Contributor

elazarg commented Jul 8, 2018

@ilevkivskyi I think it should be allowed as long as the overloads are exhaustive.

ilevkivskyi added a commit to ilevkivskyi/mypy that referenced this issue Nov 5, 2019
Fixes python#3625
Fixes python#5305
Fixes python#5320
Fixes python#5868
Fixes python#7191
Fixes python#7778
Fixes python/typing#680

So, lately I was noticing many issues that would be fixed by (partially) moving the check for self-type from definition site to call site.

This morning I found that we actually have such function `check_self_arg()` that is applied at call site, but it is almost not used. After more reading of the code I found that all the patterns for self-types that I wanted to support should either already work, or work with minimal modifications. Finally, I discovered that the root cause of many of the problems is the fact that `bind_self()` uses wrong direction for type inference! All these years it expected actual argument type to be _supertype_ of the formal one.

After fixing this bug, it turned out it was easy to support following patterns for explicit self-types:
* Structured match on generic self-types
* Restricted methods in generic classes (methods that one is allowed to call only for some values or type arguments)
* Methods overloaded on self-type
* (Important case of the above) overloaded `__init__` for generic classes
* Mixin classes (using protocols)
* Private class-level decorators (a bit hacky)
* Precise types for alternative constructors (mostly already worked)

This PR cuts few corners, but it is ready for review (I left some TODOs). Note I also add some docs, I am not sure this is really needed, but probably good to have.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong priority-2-low
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants