Skip to content

Check the type of the first argument of the function enclosing super() #3931

Closed

Description

@ilevkivskyi

Consider this code:

class B:
    x = 1

class C(B):
    def meth(self) -> None:
        def inner(x: int) -> None:
            reveal_type(super().x)  # Revealed type is 'builtins.int'
        inner(42)

C().meth()

This passes type check, but fails at runtime with a scary error. An additional problem is that mypy requires to put an argument in inner, but then it looks like mypy completely ignores it. This seem to be a bug in analyze_super, the code in this function is a bit hard to follow and might be worth refactoring.

Activity

  1. ilevkivskyi commented on Sep 6, 2017

    @ilevkivskyi
    MemberAuthor

    This is a follow up for #3919

  2. ilevkivskyi commented on Aug 27, 2019

    @ilevkivskyi
    MemberAuthor

    I think this can be closed now. Although error message that mypy now gives is IMO still not perfect, it is very similar to the runtime error:

    mypy: Argument 2 for "super" not an instance of argument 1
    runtime: super(type, obj): obj must be an instance or subtype of type
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions