shrink whitelist for `sphinx.ext.autodoc.type_comment` (#11400)

Co-authored-by: daniel.eades <daniel.eades@hotmail.com>
This commit is contained in:
danieleades 2023-07-23 17:49:18 +01:00 committed by GitHub
parent f06ef37017
commit edd9ea0a39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 5 deletions

View File

@ -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",

View File

@ -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