OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Can you define recursive type hints in Python? [duplicate]

  • Thread starter Thread starter ChrisProsser
  • Start date Start date
C

ChrisProsser

Guest
I have an alias for a type in Python:

Code:
from typing import Tuple, Dict, Union

SelectedModelArgs = Dict[
    str,
    Union[
        str,
        float,
        Tuple[float, float],
        Dict[str, Union[str, float, Tuple[float, float]]],
    ],
]

At present this allows for a dictionary with str to str, float, a tuple of floats or a nested dictionary of the same type. Is there a way to define this recursively to allow arbitrary nesting?

e.g.

Code:
SelectedModelArgs = Dict[
    str,
    Union[
        str,
        float,
        Tuple[float, float],
        SelectedModelArgs,
    ],
]

When I try this I get IDE errors:

  • Pylance: "SelectedModelArgs" is not defined
  • Mypy: Cannot resolve now "SelectedModelArgs" (possible cyclic definition)

Adding from __future__ import annotations to the top of the file does not resolve these (as it does with using a class name within a class definition).

I have also tried using the code example above but with the nested "SelectedModelArgs" as a string (as noted in this answer). This resolves the pylance error, but not the mypy error.
<p>I have an alias for a type in Python:</p>
<pre class="lang-py prettyprint-override"><code>from typing import Tuple, Dict, Union

SelectedModelArgs = Dict[
str,
Union[
str,
float,
Tuple[float, float],
Dict[str, Union[str, float, Tuple[float, float]]],
],
]
</code></pre>
<p>At present this allows for a dictionary with str to str, float, a tuple of floats or a nested dictionary of the same type. Is there a way to define this recursively to allow arbitrary nesting?</p>
<p>e.g.</p>
<pre class="lang-py prettyprint-override"><code>SelectedModelArgs = Dict[
str,
Union[
str,
float,
Tuple[float, float],
SelectedModelArgs,
],
]
</code></pre>
<p>When I try this I get IDE errors:</p>
<ul>
<li>Pylance: "SelectedModelArgs" is not defined</li>
<li>Mypy: Cannot resolve now "SelectedModelArgs" (possible cyclic definition)</li>
</ul>
<p>Adding <code>from __future__ import annotations</code> to the top of the file does not resolve these (as it does with using a class name within a class definition).</p>
<p>I have also tried using the code example above but with the nested <code>"SelectedModelArgs"</code> as a string (as noted in <a href="https://stackoverflow.com/a/53845083/2012446">this answer</a>). This resolves the pylance error, but not the mypy error.</p>
 

Latest posts

Top