Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursive type annotations

I'm trying to introduce static type annotations to my codebase where applicable. One case is when reading a JSON, the resulting object will be a dictionary keyed by strings, with values of one of the following types:

  • bool
  • str
  • float
  • int
  • list
  • dict

However the list and dict above can contain that same sort of dictionary, leading to a recursive definition. Is this representable in Python3's type structure?

like image 637
Adam Smith Avatar asked Mar 04 '23 23:03

Adam Smith


1 Answers

As of mypy 0.990, mypy finally supports recursive type annotations, using the natural syntax:

from typing import Union, Dict, List

JSONVal = Union[None, bool, str, float, int, List['JSONVal'], Dict[str, 'JSONVal']]

d: JSONVal = {'a': ['b']}

mypy output:

Success: no issues found in 1 source file

Before 0.990, this would produce an error reporting a lack of recursive type support:

$ mypy asdf.py
asdf.py:3: error: Recursive types not fully supported yet, nested types replaced with "Any"

On such versions, Dict[str, Any] would be the way to go.


You can also use mutually recursive type aliases now, so you can do things like

from typing import Union, Dict, List

JSONVal = Union[None, bool, str, float, int, 'JSONArray', 'JSONObject']
JSONArray = List[JSONVal]
JSONObject = Dict[str, JSONVal]

d: JSONObject = {'a': ['b']}
like image 59
user2357112 supports Monica Avatar answered Mar 23 '23 01:03

user2357112 supports Monica