0

Imagine I have a data structure of the following form:

[0, 1, [2, [3, 4, [5]], 6], []]

I want to create a TypeHint to describe this structure, but the structure itself follows a recursive pattern. Would this be the correct way to declare the type variable?

NestedInts = List[Union[int, 'NestedInts']]

I tried this in PyCharm, and the IDE just gives up and introspects it as type Any

flakes
  • 21,558
  • 8
  • 41
  • 88
  • Maybe this will be any help? https://stackoverflow.com/questions/38340808/recursive-typing-in-python-3-5 – Andrej Kesely Jun 21 '19 at 18:52
  • 1
    @AndrejKesely no, that's just a forward reference. Note sure about PyCharm's type-checker, but at least mypy does not support recursive types *yet* although they are in the process of implementing it. https://github.com/python/mypy/issues/731 – juanpa.arrivillaga Jun 21 '19 at 18:54
  • @AndrejKesely Yeah, I saw that documentation. I was thrown off by the fact that this is an alias vs a class definition, and that the IDE doesn't seem to grok what's going on. – flakes Jun 21 '19 at 18:55

0 Answers0