The problem is like this:
class A():
def foo() -> B:
pass
class B():
def bar() -> A:
pass
This will raise a NameError: name 'B' is not defined.
For the sake of type checking, I'm not willing to change -> B to -> "B". Is there any workaround?
You can pass the first reference as a string (Python 3.5+):
class A():
def foo() -> 'B':
pass
class B():
def bar() -> A:
pass
It's called "forward reference" and was laid out in PEP484. See this answer for more ways and detailed information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With