Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unpacking Python's Type Annotations

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 inspect module.

This part works as I expect when the type is a simple builtin class:

import inspect

def my_function() -> dict:
    pass

signature = inspect.signature(my_function)
signature.return_annotation is dict  # True

Though I'm not sure how to unwrap and inspect more complex annotations e.g:

from typing import List
import inspect

def my_function() -> List[int]:
    pass

signature = inspect.signature(my_function)
signature.return_annotation is List[int]  # False

Again similar problem with forward referencing a custom class:

def my_function() -> List['User']:
    pass
...
signature.return_annotation  # typing.List[_ForwardRef('User')]

What I'm looking to get out is something like this - so I can branch appropriately while generating the JavaScript:

type = signature.return_annotation... # list
member_type = signature.return_annotation... # int / 'User'

Thanks.

like image 930
freebie Avatar asked Aug 01 '16 16:08

freebie


People also ask

What is unpacking in Python?

Introduction. Unpacking in Python refers to an operation that consists of assigning an iterable of values to a tuple (or list ) of variables in a single assignment statement. As a complement, the term packing can be used when we collect several values in a single variable using the iterable unpacking operator, * .

What is Python type annotation?

What Are Type Annotations? Type annotations — also known as type signatures — are used to indicate the datatypes of variables and input/outputs of functions and methods. In many languages, datatypes are explicitly stated. In these languages, if you don't declare your datatype — the code will not run.

What is unpacking tuple in Python?

Python tuples are immutable means that they can not be modified in whole program. Packing and Unpacking a Tuple: In Python, there is a very powerful tuple assignment feature that assigns the right-hand side of values into the left-hand side. In another way, it is called unpacking of a tuple of values into a variable.


Video Answer


2 Answers

List is not a map of types to GenericMeta, despite the syntax. Each access to it generates a new instance:

>>> [ id(List[str]) for i in range(3) ]
[33105112, 33106872, 33046936]

This means that even List[int] is not List[int]. To compare two instances, you have multiple options:

  • Use ==, i.e., signature.return_annotation == List[int].
  • Store an instance of your type in a global variable and check against that, i.e.,

    a = List[int]
    def foo() -> a:
        pass
    inspect.signature(foo).return_annotation is a
    
  • Use issubclass. The typing module defines that. Note that this might do more than you'd like, make sure to read the _TypeAlias documentation if you use this.

  • Check against List only and read the contents yourself. Though the property is internal, it is unlikely that the implementation will change soon: List[int].__args__[0] contains the type argument starting from Python 3.5.2, and in earlier versions, its List[int].__parameters__[0].

If you'd like to write generic code for your exporter, then the last option is probably best. If you only need to cover a specific use case, I'd personally go with using ==.

like image 81
Phillip Avatar answered Oct 02 '22 06:10

Phillip


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

like image 36
Jason Lin Avatar answered Oct 02 '22 06:10

Jason Lin