Recursive type annotations

前端 未结 1 1707
一生所求
一生所求 2020-12-11 13:36

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

相关标签:
1条回答
  • 2020-12-11 13:41

    As of mypy 0.641, mypy doesn't support the sort of recursive type annotation you're looking for. 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']}
    

    produces 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"
    

    Also see the mypy issue tracker thread on recursive type support.

    For now, Dict[str, Any] is probably the way to go.

    0 讨论(0)
提交回复
热议问题