0

Custom typing in Python 3.8

I have:

/zoo.py

from typing import List

class Zoo(object):

   animals: List

/animal.py

from zoo import Zoo

class Animal(object):
   
  zoo: Zoo

How do I determine the type of data in the first file without the cyclicity of imports? I want to get the following

/zoo.py

from typing import List
from animal import Animal

class Zoo(object):

   animals: List[Animal]

In large projects, cyclicality interferes. How to use typing correctly in such cases

Ch3steR
  • 20,090
  • 4
  • 28
  • 58
markus
  • 9
  • 1
  • So why doesn't your intended solution work? Looks fine to me. – NotAName Jan 10 '22 at 05:34
  • In large projects, cyclicality interferes. How to use typing correctly in such cases. When there are many classes, and it is difficult to connect - it is not always possible to import them without cyclicity – markus Jan 10 '22 at 05:38
  • Especially since you're not actually invoking the imported type in the class definition, you can just do `import animal` and `animals: List[animal.Animal]`, and that should work fine – Green Cloak Guy Jan 10 '22 at 05:38

0 Answers0