Reputation: 4785
Say I already have a file with a class that is correctly typed:
#main.py
class Runner(object):
...
def operation(self, arg: str) -> Runner:
self.attrib = 'something ' + arg
return self
...
Now I want to separate many of these operation
methods sicen my file is becoming too large, so I create a new file subs.py
and transfer my code to a mixing:
#main.py
from subs import Mixin
class Runner(Mixin):
...
#subs.py
class Mixin(object):
...
def operation(self, arg: str) -> Runner:
self.attrib = 'something ' + arg
return self
The Runner
Type is unknowm to subs.py
and if I import it then I will be creating circular import dependency, and I dont even think it will work anyway, so Im not sure the correct way to deal with this, in the assumed case that :class:Mixin
will only ever be used inherited by the main class.
Upvotes: 0
Views: 196
Reputation: 531708
Once you move the method into the class Mixin
, you don't know that operation
will return an instance of Runner
. Consider:
class Foo(Bar, Mixin):
...
f = Foo()
f.operation() # Returns something that is a Bar and a Mixin, but not a Runner.
The correct type hint is
def operation(self, arg: str) -> Mixin:
self.attrib = 'something ' + arg
return self
Upvotes: 1