Unpacking Python's Type Annotations

后端 未结 3 456
终归单人心
终归单人心 2021-01-04 13:52

I\'m trying to generate some JavaScript based on the type annotations I have provided in on some Python functions by using the signature() function in the

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-04 14:27

    Python 3.8 provides typing.get_origin() and typing.get_args() for this!

    assert get_origin(Dict[str, int]) is dict
    assert get_args(Dict[int, str]) == (int, str)
    
    assert get_origin(Union[int, str]) is Union
    assert get_args(Union[int, str]) == (int, str)
    

    See https://docs.python.org/3/library/typing.html#typing.get_origin

提交回复
热议问题