pg.typing.annotation_from_str¶
Accessible via pg.typing.annotation_from_str.
- annotation_from_str(annotation_str, parent_module=None)[source]¶
Parses annotations from str.
BNF for PyType annotations:
` :rtype: :sphinx_autodoc_typehints_type:`\:py\:data\:\`\~typing.Any\<maybe_union> ::= <type> | <type> “|” <maybe_union> <type> ::= <literal_type> | <non_literal_type>
<literal_type> ::= “Literal”<literal_params> <literal_params> ::= “[“<python_values>”]” (parsed by pg.coding.evaluate)
<non_literal_type> ::= <type_id> | <type_id>”[“<type_arg>”]” <type_arg> ::= <maybe_type_list> | <maybe_type_list>”,”<maybe_type_list> <maybe_type_list> ::= “[“<type_arg>”]” | <maybe_union> <type_id> ::= ‘aAz_.1-9’ ```
- Parameters:
annotation_str – String form of type annotations. E.g. “list[str]”
parent_module – The module where the annotation was defined.
- Returns:
Object form of the annotation.
- Raises:
SyntaxError – If the annotation string is invalid.