diff --git a/packages/python-packages/api-stub-generator/CHANGELOG.md b/packages/python-packages/api-stub-generator/CHANGELOG.md index 29e28c43de3..1ed7770e371 100644 --- a/packages/python-packages/api-stub-generator/CHANGELOG.md +++ b/packages/python-packages/api-stub-generator/CHANGELOG.md @@ -2,6 +2,7 @@ ## Version 0.3.7 (Unreleased) Fix incorrect type annotation. +Update to follow best practices for accessing '__annotations__'. ## Version 0.3.6 (2022-10-27) Suppressed unwanted base class methods in DPG libraries. diff --git a/packages/python-packages/api-stub-generator/apistub/nodes/_annotation_parser.py b/packages/python-packages/api-stub-generator/apistub/nodes/_annotation_parser.py index 8692430f879..378699cd058 100644 --- a/packages/python-packages/api-stub-generator/apistub/nodes/_annotation_parser.py +++ b/packages/python-packages/api-stub-generator/apistub/nodes/_annotation_parser.py @@ -15,7 +15,12 @@ def __init__(self, obj, namespace: str, func_node): self.posargs = {} self.varargs = None self.return_type = None - annotations = getattr(obj, "__annotations__", None) + # TODO: Replace with "get_annotations" once min Python is 3.10+ + # See: https://docs.python.org/3.10/howto/annotations.html#accessing-the-annotations-dict-of-an-object-in-python-3-9-and-older + if isinstance(obj, type): + annotations = obj.__dict__.get("__annotations__", None) + else: + annotations = getattr(obj, "__annotations__", None) if annotations: self.return_type = annotations.pop('return', inspect.Parameter.empty) self.args = {