diff --git a/mypy/stubgenc.py b/mypy/stubgenc.py index 94468cb36dff..a44acc303228 100755 --- a/mypy/stubgenc.py +++ b/mypy/stubgenc.py @@ -848,10 +848,7 @@ def generate_class_stub(self, class_name: str, cls: type, output: list[str]) -> attrs.append((attr, value)) # Gets annotations if they exist - try: - annotations = cls.__annotations__ - except AttributeError: - annotations = {} + annotations = inspect.get_annotations(cls) for attr, value in attrs: if attr == "__hash__" and value is None: @@ -910,10 +907,7 @@ def generate_variable_stub(self, name: str, obj: object, output: list[str]) -> N self.record_name(name) # Gets annotations if they exist - try: - annotations = self.module.__annotations__ - except AttributeError: - annotations = {} + annotations = inspect.get_annotations(self.module) if name in annotations: type_str = self.strip_or_import(annotations[name])