Dov Benyomin Sohacheski
Dov Benyomin Sohacheski

Reputation: 7714

Python annotation error due to circular dependency

I have two classes that are dependent one another and are working fine without annotations. Unfortunately, when I try and annotate return values, it causes an expected circular dependency error.

Network.py

def processors(self) -> List[Processor]:
    # implementation

Processor.py

def network(self) -> Network:
    # implementation

How can I annotate without having to use the import statement which causes a circular dependency?

Upvotes: 0

Views: 155

Answers (1)

fjarri
fjarri

Reputation: 9726

You can use a forward reference:

def processors(self) -> List['Processor']:
    # implementation

Yes, it looks a bit ugly, but it appears to be the intended solution.

Upvotes: 1

Related Questions