1

While working through a custom django mixin, I came across code with the following structure:

class A:
    def method(self, i): # this is a method provided by django, so can't easily change this signature
        print(f"A: {i}")


class B:
    def method(self, i: int): # with type hint
        print(f"B: {i}")
        super().method(i)


class C(B, A):
    pass

# at some point
C().method(0)

mypy gives error: "method" undefined in superclass. For now, the workaround I use is to treat method as dynamically typed (i.e drop the int). Is it possible to (or does it make sense to) add type annotations so method is treated as statically typed? Thanks!

EricC
  • 1,355
  • 1
  • 20
  • 33
  • 1
    That doesn't sound like it has anything to do with the lack of type hints on `A.method`. Also, nothing in this code makes any reference to anything called `m`, so that error message makes no sense. The *actual* error message mypy produces for this code is `main.py:9: error: "method" undefined in superclass`, because `B` does not inherit from anything with a method named `method`. – user2357112 Jan 29 '21 at 21:54
  • 1
    mypy is just terrible with mixins for now. There's no good solution, only workarounds. – user2357112 Jan 29 '21 at 21:57
  • Tahnks @user2357112supportsMonica you are right, "m" is a typo, it's "method" instead. fixed. – EricC Jan 29 '21 at 22:01

0 Answers0