summaryrefslogtreecommitdiff
path: root/sphinx/util/inspect.py
diff options
context:
space:
mode:
Diffstat (limited to 'sphinx/util/inspect.py')
-rw-r--r--sphinx/util/inspect.py20
1 files changed, 15 insertions, 5 deletions
diff --git a/sphinx/util/inspect.py b/sphinx/util/inspect.py
index 147d43592..4439e09f6 100644
--- a/sphinx/util/inspect.py
+++ b/sphinx/util/inspect.py
@@ -12,10 +12,14 @@
import re
from six import PY3, binary_type
-from six.moves import builtins
+from six.moves import builtins # type: ignore
from sphinx.util import force_decode
+if False:
+ # For type annotation
+ from typing import Any, Callable, Tuple # NOQA
+
# this imports the standard library inspect module without resorting to
# relatively import this module
inspect = __import__('inspect')
@@ -67,7 +71,7 @@ else: # 2.7
"""Like inspect.getargspec but supports functools.partial as well."""
if inspect.ismethod(func):
func = func.__func__
- parts = 0, ()
+ parts = 0, () # type: Tuple[int, Tuple[unicode, ...]]
if type(func) is partial:
keywords = func.keywords
if keywords is None:
@@ -101,6 +105,7 @@ except ImportError:
def isenumattribute(x):
+ # type: (Any) -> bool
"""Check if the object is attribute of enum."""
if enum is None:
return False
@@ -108,6 +113,7 @@ def isenumattribute(x):
def isdescriptor(x):
+ # type: (Any) -> bool
"""Check if the object is some kind of descriptor."""
for item in '__get__', '__set__', '__delete__':
if hasattr(safe_getattr(x, item, None), '__call__'):
@@ -116,6 +122,7 @@ def isdescriptor(x):
def safe_getattr(obj, name, *defargs):
+ # type: (Any, unicode, unicode) -> object
"""A getattr() that turns all exceptions into AttributeErrors."""
try:
return getattr(obj, name, *defargs)
@@ -138,8 +145,9 @@ def safe_getattr(obj, name, *defargs):
def safe_getmembers(object, predicate=None, attr_getter=safe_getattr):
+ # type: (Any, Callable[[unicode], bool], Callable) -> List[Tuple[unicode, Any]]
"""A version of inspect.getmembers() that uses safe_getattr()."""
- results = []
+ results = [] # type: List[Tuple[unicode, Any]]
for key in dir(object):
try:
value = attr_getter(object, key, None)
@@ -152,6 +160,7 @@ def safe_getmembers(object, predicate=None, attr_getter=safe_getattr):
def object_description(object):
+ # type: (Any) -> unicode
"""A repr() implementation that returns text safe to use in reST context."""
try:
s = repr(object)
@@ -166,6 +175,7 @@ def object_description(object):
def is_builtin_class_method(obj, attr_name):
+ # type: (Any, unicode) -> bool
"""If attr_name is implemented at builtin class, return True.
>>> is_builtin_class_method(int, '__init__')
@@ -177,6 +187,6 @@ def is_builtin_class_method(obj, attr_name):
classes = [c for c in inspect.getmro(obj) if attr_name in c.__dict__]
cls = classes[0] if classes else object
- if not hasattr(builtins, safe_getattr(cls, '__name__', '')):
+ if not hasattr(builtins, safe_getattr(cls, '__name__', '')): # type: ignore
return False
- return getattr(builtins, safe_getattr(cls, '__name__', '')) is cls
+ return getattr(builtins, safe_getattr(cls, '__name__', '')) is cls # type: ignore