get-annotations
A backport of Python 3.10's inspect.get_annotations() function.
Install
pip3 install -U get-annotations
Usage
from get_annotations import get_annotations
def foo(x: int) -> str: ...
print(get_annotations(foo))
# {'x': <class 'int'>, 'return': <class 'str'>}
If your module uses from __future__ import annotations, you'll want to set eval_str=True, otherwise get_annotations will return strings:
from __future__ import annotations
import typing as t
def bar(x: t.List[MyObject]): ...
class MyObject:
pass
print(get_annotations(bar))
# {'x': 't.List[MyObject]'}
print(get_annotations(bar, eval_str=True))
# {'x': typing.List[__main__.MyObject]}
Note that it does not work with old-style forward ref annotations, such as t.List["MyObject"]:
>>> from typing import List
>>> def foo(a: int) -> List["MyObject"]: ...
...
>>> class MyObject: ...
...
>>> print(get_annotations(foo, eval_str=True)) # Note that 'MyObject' is returned as a string!
{'a': <class 'int'>, 'return': typing.List[ForwardRef('MyObject')]}
>>>
>>> print(get_annotations(foo, eval_str=False)) # Identical
{'a': <class 'int'>, 'return': typing.List[ForwardRef('MyObject')]}
If you really don't want to use from __future__ import annotations for some reason, you can surround an entire type annotation in quotes to forward ref it:
>>> def foo(a: int) -> "List[MyObject]": ...
...
>>> print(get_annotations(foo, eval_str=True)) # This works now
{'a': <class 'int'>, 'return': typing.List[__main__.MyObject]}
>>>
>>> print(get_annotations(foo, eval_str=False)) # For comparison
{'a': <class 'int'>, 'return': 'List[MyObject]'}
License
MIT
Contact
A library by Shawn Presser. If you found it useful, please consider joining my patreon!
My Twitter DMs are always open; you should send me one! It's the best way to reach me, and I'm always happy to hear from you.
- Twitter: @theshawwn
- Patreon: https://www.patreon.com/shawwn
- HN: sillysaurusx
- Website: shawwn.com
Metadata
Release files for get-annotations 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| get-annotations-0.1.2.tar.gz | 4.5 kB | Without provenance | Details for get-annotations-0.1.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| get_annotations-0.1.2-py3-none-any.whl | Python 3 | none | any | Details for get_annotations-0.1.2-py3-none-any.whl |
Total release size: 9.0 kB
Release files / get-annotations-0.1.2.tar.gz
| Download URL | get-annotations-0.1.2.tar.gz |
|---|---|
| Size | 4.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
da7b69b8043237cc7f7ce5919e9cc59bd18fc4e2704b43eb34e3ba4fa9374bab
|
|
BLAKE2b-256 checksum How to use checksums |
616e265083c3bc64f17b7bc52e8c3720739841772e81b44272b8ba12d46a01ce
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.2.0a2 CPython/3.9.9 Darwin/20.6.0
|
Release files / get_annotations-0.1.2-py3-none-any.whl
| Download URL | get_annotations-0.1.2-py3-none-any.whl |
|---|---|
| Size | 4.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
788ba8aa2434ee34ffb985c4aea2f9e575204c884c0e0dd0f969be846470e527
|
|
BLAKE2b-256 checksum How to use checksums |
c6429515026b884c7d5877c610082d35bcbad4f8ff4dfbf31aff0fe914f7ff3d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.2.0a2 CPython/3.9.9 Darwin/20.6.0
|
Security for get-annotations 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.