Skip to content

Commit

Permalink
Merge pull request sphinx-doc#8024 from tk0miya/8032_evaluate_signatu…
Browse files Browse the repository at this point in the history
…re_in_py310

Fix sphinx-doc#8023: evaluate_signature() does not work properly in python3.10
  • Loading branch information
tk0miya committed Aug 1, 2020
2 parents a443538 + 01ff500 commit 2644199
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions sphinx/util/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,19 +497,26 @@ def signature(subject: Callable, bound_method: bool = False, follow_wrapped: boo
def evaluate_signature(sig: inspect.Signature, globalns: Dict = None, localns: Dict = None
) -> inspect.Signature:
"""Evaluate unresolved type annotations in a signature object."""
def evaluate_forwardref(ref: ForwardRef, globalns: Dict, localns: Dict) -> Any:
"""Evaluate a forward reference."""
if sys.version_info > (3, 10):
return ref._evaluate(globalns, localns, frozenset())
else:
return ref._evaluate(globalns, localns)

def evaluate(annotation: Any, globalns: Dict, localns: Dict) -> Any:
"""Evaluate unresolved type annotation."""
try:
if isinstance(annotation, str):
ref = ForwardRef(annotation, True)
annotation = ref._evaluate(globalns, localns)
annotation = evaluate_forwardref(ref, globalns, localns)

if isinstance(annotation, ForwardRef):
annotation = annotation._evaluate(globalns, localns)
annotation = evaluate_forwardref(ref, globalns, localns)
elif isinstance(annotation, str):
# might be a ForwardRef'ed annotation in overloaded functions
ref = ForwardRef(annotation, True)
annotation = ref._evaluate(globalns, localns)
annotation = evaluate_forwardref(ref, globalns, localns)
except (NameError, TypeError):
# failed to evaluate type. skipped.
pass
Expand Down

0 comments on commit 2644199

Please sign in to comment.