mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Signature formats args of method correctly in py2
This commit is contained in:
parent
3be5eebd6b
commit
4dcc35a649
@ -275,6 +275,13 @@ class Signature(object):
|
|||||||
except:
|
except:
|
||||||
self.annotations = None
|
self.annotations = None
|
||||||
|
|
||||||
|
if PY3:
|
||||||
|
# inspect.signature recognizes bound methods automatically
|
||||||
|
self.skip_first_argument = False
|
||||||
|
else:
|
||||||
|
# check subject is bound method or not to skip first argument (ex. self)
|
||||||
|
self.skip_first_argument = inspect.ismethod(subject) and subject.__self__
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def parameters(self):
|
def parameters(self):
|
||||||
# type: () -> Dict
|
# type: () -> Dict
|
||||||
@ -309,7 +316,11 @@ class Signature(object):
|
|||||||
# type: () -> unicode
|
# type: () -> unicode
|
||||||
args = []
|
args = []
|
||||||
last_kind = None
|
last_kind = None
|
||||||
for param in itervalues(self.parameters):
|
for i, param in enumerate(itervalues(self.parameters)):
|
||||||
|
# skip first argument if subject is bound method
|
||||||
|
if self.skip_first_argument and i == 0:
|
||||||
|
continue
|
||||||
|
|
||||||
arg = StringIO()
|
arg = StringIO()
|
||||||
|
|
||||||
# insert '*' between POSITIONAL args and KEYWORD_ONLY args::
|
# insert '*' between POSITIONAL args and KEYWORD_ONLY args::
|
||||||
|
@ -148,28 +148,45 @@ def test_Signature_partial():
|
|||||||
assert sig == '(b, *, c=11, d=2)'
|
assert sig == '(b, *, c=11, d=2)'
|
||||||
|
|
||||||
|
|
||||||
def test_Signature_bound_methods():
|
def test_Signature_methods():
|
||||||
class Foo:
|
class Foo:
|
||||||
def method(self, arg1, **kwargs):
|
def meth1(self, arg1, **kwargs):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
bound_method = Foo().method
|
@classmethod
|
||||||
|
def meth2(cls, arg1, *args, **kwargs):
|
||||||
|
pass
|
||||||
|
|
||||||
@functools.wraps(bound_method)
|
@staticmethod
|
||||||
|
def meth3(arg1, *args, **kwargs):
|
||||||
|
pass
|
||||||
|
|
||||||
|
@functools.wraps(Foo().meth1)
|
||||||
def wrapped_bound_method(*args, **kwargs):
|
def wrapped_bound_method(*args, **kwargs):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# class method
|
# unbound method
|
||||||
sig = inspect.Signature(Foo.method).format_args()
|
sig = inspect.Signature(Foo.meth1).format_args()
|
||||||
assert sig == '(self, arg1, **kwargs)'
|
assert sig == '(self, arg1, **kwargs)'
|
||||||
|
|
||||||
# bound method
|
# bound method
|
||||||
sig = inspect.Signature(bound_method).format_args()
|
sig = inspect.Signature(Foo().meth1).format_args()
|
||||||
if sys.version_info < (3, 4, 4):
|
|
||||||
assert sig == '(self, arg1, **kwargs)'
|
|
||||||
else:
|
|
||||||
assert sig == '(arg1, **kwargs)'
|
assert sig == '(arg1, **kwargs)'
|
||||||
|
|
||||||
|
# class method
|
||||||
|
sig = inspect.Signature(Foo.meth2).format_args()
|
||||||
|
assert sig == '(arg1, *args, **kwargs)'
|
||||||
|
|
||||||
|
sig = inspect.Signature(Foo().meth2).format_args()
|
||||||
|
assert sig == '(arg1, *args, **kwargs)'
|
||||||
|
|
||||||
|
# static method
|
||||||
|
sig = inspect.Signature(Foo.meth3).format_args()
|
||||||
|
assert sig == '(arg1, *args, **kwargs)'
|
||||||
|
|
||||||
|
sig = inspect.Signature(Foo().meth3).format_args()
|
||||||
|
assert sig == '(arg1, *args, **kwargs)'
|
||||||
|
|
||||||
# wrapped bound method
|
# wrapped bound method
|
||||||
sig = inspect.Signature(wrapped_bound_method).format_args()
|
sig = inspect.Signature(wrapped_bound_method).format_args()
|
||||||
if sys.version_info < (3, 4, 4):
|
if sys.version_info < (3, 4, 4):
|
||||||
|
Loading…
Reference in New Issue
Block a user