Description
This is more of a question or a feature request if there's not a good way to do it.
I'm currently adding types to existing python 2.7 code base hoping that it will help me with migrating the code to Python 3.
I run into code that looks like this:
#! /usr/bin/env python
class A(object):
def __init__(self, var1, var2):
# type: (str, str) -> None
self.var1 = var1
self.var2 = var2
class Mixin(object):
def funcA(self):
# type: () -> str
return self.var1
class B(A, Mixin):
pass
def main():
# type: () -> None
a = B('value1', 'value2')
print(a.funcA())
if __name__ == '__main__':
main()
This is valid Python code, i.e.:
$ ./test.py
value1
But mypy is complaining:
$ ../py37/bin/mypy --python-executable ../py27/bin/python test.py
test.py:12: error: "Mixin" has no attribute "var1"
I understand the issue, there's nothing in Mixin
class that states that var1
will be available.
My question is is there a way to tell mypy somehow that this attribute will be available?
I did not have much luck with using abstract classes and Protocol
.
I also had a suggestion to use stub file, but that doesn't look like be perfect solution either, since I might need to define types outside of the module and keep them in sync. And this pattern is used in multiple places not just one.
Is there a way how this could be expressed in mypy? If the code needs to be slightly refactored without changing how it works I suppose that might also be acceptable.