# get-annotations
A backport of Python 3.10's [`inspect.get_annotations()`](https://docs.python.org/3/library/inspect.html#inspect.get_annotations) function.
## Install
```
pip3 install -U get-annotations
```
## Usage
```py
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:
```py
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"]`:
```py
>>> 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:
```py
>>> 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](https://www.shawwn.com). If you found it useful, please consider [joining my patreon](https://www.patreon.com/shawwn)!
My Twitter DMs are always open; you should [send me one](https://twitter.com/theshawwn)! It's the best way to reach me, and I'm always happy to hear from you.
- Twitter: [@theshawwn](https://twitter.com/theshawwn)
- Patreon: [https://www.patreon.com/shawwn](https://www.patreon.com/shawwn)
- HN: [sillysaurusx](https://news.ycombinator.com/threads?id=sillysaurusx)
- Website: [shawwn.com](https://www.shawwn.com)
Raw data
{
"_id": null,
"home_page": "https://github.com/shawwn/get-annotations",
"name": "get-annotations",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.6,<4.0",
"maintainer_email": "",
"keywords": "",
"author": "Shawn Presser",
"author_email": "",
"download_url": "https://files.pythonhosted.org/packages/61/6e/265083c3bc64f17b7bc52e8c3720739841772e81b44272b8ba12d46a01ce/get-annotations-0.1.2.tar.gz",
"platform": "",
"description": "# get-annotations\n\nA backport of Python 3.10's [`inspect.get_annotations()`](https://docs.python.org/3/library/inspect.html#inspect.get_annotations) function.\n\n## Install\n\n```\npip3 install -U get-annotations\n```\n\n## Usage\n\n```py\nfrom get_annotations import get_annotations\n\ndef foo(x: int) -> str: ...\n\nprint(get_annotations(foo))\n# {'x': <class 'int'>, 'return': <class 'str'>}\n```\n\n\nIf your module uses `from __future__ import annotations`, you'll want to set `eval_str=True`, otherwise `get_annotations` will return strings:\n\n```py\nfrom __future__ import annotations\nimport typing as t\n\ndef bar(x: t.List[MyObject]): ...\n\nclass MyObject:\n pass\n\nprint(get_annotations(bar))\n# {'x': 't.List[MyObject]'}\n\nprint(get_annotations(bar, eval_str=True))\n# {'x': typing.List[__main__.MyObject]}\n```\n\nNote that it does _not_ work with old-style forward ref annotations, such as `t.List[\"MyObject\"]`:\n\n```py\n>>> from typing import List\n>>> def foo(a: int) -> List[\"MyObject\"]: ...\n...\n>>> class MyObject: ...\n...\n>>> print(get_annotations(foo, eval_str=True)) # Note that 'MyObject' is returned as a string!\n{'a': <class 'int'>, 'return': typing.List[ForwardRef('MyObject')]}\n>>>\n>>> print(get_annotations(foo, eval_str=False)) # Identical\n{'a': <class 'int'>, 'return': typing.List[ForwardRef('MyObject')]}\n```\n\n\nIf 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:\n\n```py\n>>> def foo(a: int) -> \"List[MyObject]\": ...\n...\n>>> print(get_annotations(foo, eval_str=True)) # This works now\n{'a': <class 'int'>, 'return': typing.List[__main__.MyObject]}\n>>>\n>>> print(get_annotations(foo, eval_str=False)) # For comparison\n{'a': <class 'int'>, 'return': 'List[MyObject]'}\n```\n\n\n## License\n\nMIT\n\n## Contact\n\nA library by [Shawn Presser](https://www.shawwn.com). If you found it useful, please consider [joining my patreon](https://www.patreon.com/shawwn)!\n\nMy Twitter DMs are always open; you should [send me one](https://twitter.com/theshawwn)! It's the best way to reach me, and I'm always happy to hear from you.\n\n- Twitter: [@theshawwn](https://twitter.com/theshawwn)\n- Patreon: [https://www.patreon.com/shawwn](https://www.patreon.com/shawwn)\n- HN: [sillysaurusx](https://news.ycombinator.com/threads?id=sillysaurusx)\n- Website: [shawwn.com](https://www.shawwn.com)\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A backport of Python 3.10's inspect.get_annotation() function",
"version": "0.1.2",
"project_urls": {
"@theshawwn on Twitter": "https://www.twitter.com/theshawwn",
"Homepage": "https://github.com/shawwn/get-annotations",
"Shawn's Website": "https://www.shawwn.com",
"Support me on Patreon": "https://www.patreon.com/shawwn",
"sillysaurusx on Hacker News": "https://news.ycombinator.com/threads?id=sillysaurusx"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "c6429515026b884c7d5877c610082d35bcbad4f8ff4dfbf31aff0fe914f7ff3d",
"md5": "f028d6aa0b44d5345d69051bd7eefcf9",
"sha256": "788ba8aa2434ee34ffb985c4aea2f9e575204c884c0e0dd0f969be846470e527"
},
"downloads": -1,
"filename": "get_annotations-0.1.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "f028d6aa0b44d5345d69051bd7eefcf9",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6,<4.0",
"size": 4482,
"upload_time": "2021-12-27T06:42:41",
"upload_time_iso_8601": "2021-12-27T06:42:41.790153Z",
"url": "https://files.pythonhosted.org/packages/c6/42/9515026b884c7d5877c610082d35bcbad4f8ff4dfbf31aff0fe914f7ff3d/get_annotations-0.1.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "616e265083c3bc64f17b7bc52e8c3720739841772e81b44272b8ba12d46a01ce",
"md5": "010dd12b1b454536150c83b5e45896bd",
"sha256": "da7b69b8043237cc7f7ce5919e9cc59bd18fc4e2704b43eb34e3ba4fa9374bab"
},
"downloads": -1,
"filename": "get-annotations-0.1.2.tar.gz",
"has_sig": false,
"md5_digest": "010dd12b1b454536150c83b5e45896bd",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6,<4.0",
"size": 4506,
"upload_time": "2021-12-27T06:42:40",
"upload_time_iso_8601": "2021-12-27T06:42:40.220129Z",
"url": "https://files.pythonhosted.org/packages/61/6e/265083c3bc64f17b7bc52e8c3720739841772e81b44272b8ba12d46a01ce/get-annotations-0.1.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2021-12-27 06:42:40",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "shawwn",
"github_project": "get-annotations",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "get-annotations"
}