Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
A backport of Python 3.10's inspect.get_annotations()
function.
pip3 install -U get-annotations
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]'}
MIT
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.
FAQs
A backport of Python 3.10's inspect.get_annotation() function
We found that get-annotations demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.