Skip to content

Commit

Permalink
Merge pull request #8923 from tk0miya/8917_warning_for_wrong___globals__
Browse files Browse the repository at this point in the history
Fix #8917: autodoc: Raises a warning if function has wrong __globals__ value
  • Loading branch information
tk0miya committed Feb 25, 2021
2 parents 6d91db3 + 4f072ee commit 529386f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ Features added
Bugs fixed
----------

* #8917: autodoc: Raises a warning if function has wrong __globals__ value
* #8380: html search: Paragraphs in search results are not identified as ``<p>``
* #8915: html theme: The translation of sphinx_rtd_theme does not work
* #8342: Emit a warning if a unknown domain is given for directive or role (ex.
Expand Down
15 changes: 12 additions & 3 deletions sphinx/util/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,15 @@ def getannotations(obj: Any) -> Mapping[str, Any]:
return {}


def getglobals(obj: Any) -> Mapping[str, Any]:
"""Get __globals__ from given *obj* safely."""
__globals__ = safe_getattr(obj, '__globals__', None)
if isinstance(__globals__, Mapping):
return __globals__
else:
return {}


def getmro(obj: Any) -> Tuple["Type", ...]:
"""Get __mro__ from given *obj* safely."""
__mro__ = safe_getattr(obj, '__mro__', None)
Expand Down Expand Up @@ -484,9 +493,9 @@ def __repr__(self) -> str:

def _should_unwrap(subject: Callable) -> bool:
"""Check the function should be unwrapped on getting signature."""
if (safe_getattr(subject, '__globals__', None) and
subject.__globals__.get('__name__') == 'contextlib' and # type: ignore
subject.__globals__.get('__file__') == contextlib.__file__): # type: ignore
__globals__ = getglobals(subject)
if (__globals__.get('__name__') == 'contextlib' and
__globals__.get('__file__') == contextlib.__file__):
# contextmanger should be unwrapped
return True

Expand Down

0 comments on commit 529386f

Please sign in to comment.