mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Add bound_method hint to Signature class
This commit is contained in:
parent
4dcc35a649
commit
fd201666a2
@ -255,8 +255,8 @@ class Signature(object):
|
|||||||
its return annotation.
|
its return annotation.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, subject):
|
def __init__(self, subject, bound_method=False):
|
||||||
# type: (Callable) -> None
|
# type: (Callable, bool) -> None
|
||||||
# check subject is not a built-in class (ex. int, str)
|
# check subject is not a built-in class (ex. int, str)
|
||||||
if (isinstance(subject, type) and
|
if (isinstance(subject, type) and
|
||||||
is_builtin_class_method(subject, "__new__") and
|
is_builtin_class_method(subject, "__new__") and
|
||||||
@ -275,12 +275,22 @@ class Signature(object):
|
|||||||
except:
|
except:
|
||||||
self.annotations = None
|
self.annotations = None
|
||||||
|
|
||||||
if PY3:
|
if bound_method:
|
||||||
# inspect.signature recognizes bound methods automatically
|
# client gives a hint that the subject is a bound method
|
||||||
self.skip_first_argument = False
|
|
||||||
|
if PY3 and inspect.ismethod(subject):
|
||||||
|
# inspect.signature already considers the subject is bound method.
|
||||||
|
# So it is not need to skip first argument.
|
||||||
|
self.skip_first_argument = False
|
||||||
|
else:
|
||||||
|
self.skip_first_argument = True
|
||||||
else:
|
else:
|
||||||
# check subject is bound method or not to skip first argument (ex. self)
|
if PY3:
|
||||||
self.skip_first_argument = inspect.ismethod(subject) and subject.__self__
|
# inspect.signature recognizes type of method properly without any hints
|
||||||
|
self.skip_first_argument = False
|
||||||
|
else:
|
||||||
|
# check the subject is bound method or not
|
||||||
|
self.skip_first_argument = inspect.ismethod(subject) and subject.__self__ # type: ignore # NOQA
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def parameters(self):
|
def parameters(self):
|
||||||
|
@ -169,6 +169,9 @@ def test_Signature_methods():
|
|||||||
sig = inspect.Signature(Foo.meth1).format_args()
|
sig = inspect.Signature(Foo.meth1).format_args()
|
||||||
assert sig == '(self, arg1, **kwargs)'
|
assert sig == '(self, arg1, **kwargs)'
|
||||||
|
|
||||||
|
sig = inspect.Signature(Foo.meth1, bound_method=True).format_args()
|
||||||
|
assert sig == '(arg1, **kwargs)'
|
||||||
|
|
||||||
# bound method
|
# bound method
|
||||||
sig = inspect.Signature(Foo().meth1).format_args()
|
sig = inspect.Signature(Foo().meth1).format_args()
|
||||||
assert sig == '(arg1, **kwargs)'
|
assert sig == '(arg1, **kwargs)'
|
||||||
|
Loading…
Reference in New Issue
Block a user