diff --git a/pyproject.toml b/pyproject.toml index c68e7d3a0..b31ae439c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -324,7 +324,6 @@ module = [ "sphinx.ext.autodoc.mock", "sphinx.ext.autodoc.importer", "sphinx.ext.autodoc.preserve_defaults", - "sphinx.ext.autodoc.type_comment", "sphinx.ext.autosummary", "sphinx.ext.autosummary.generate", "sphinx.ext.doctest", diff --git a/sphinx/ext/autodoc/type_comment.py b/sphinx/ext/autodoc/type_comment.py index f3137d496..e16d5d190 100644 --- a/sphinx/ext/autodoc/type_comment.py +++ b/sphinx/ext/autodoc/type_comment.py @@ -88,14 +88,19 @@ def get_type_comment(obj: Any, bound_method: bool = False) -> Signature | None: # subject is placed inside class or block. To read its docstring, # this adds if-block before the declaration. module = ast.parse('if True:\n' + source, type_comments=True) - subject = cast(ast.FunctionDef, module.body[0].body[0]) # type: ignore + subject = cast( + ast.FunctionDef, module.body[0].body[0], # type: ignore[attr-defined] + ) else: module = ast.parse(source, type_comments=True) subject = cast(ast.FunctionDef, module.body[0]) - if getattr(subject, "type_comment", None): - function = ast.parse(subject.type_comment, mode='func_type', type_comments=True) - return signature_from_ast(subject, bound_method, function) # type: ignore + type_comment = getattr(subject, "type_comment", None) + if type_comment: + function = ast.parse(type_comment, mode='func_type', type_comments=True) + return signature_from_ast( + subject, bound_method, function, # type: ignore[arg-type] + ) else: return None except (OSError, TypeError): # failed to load source code