Include __init__ call signature in Class.doc

This commit is contained in:
Takafumi Arakaki
2013-02-24 22:56:43 +01:00
parent 4ba9fd2b68
commit 38fc49022f
3 changed files with 27 additions and 3 deletions

View File

@@ -288,7 +288,8 @@ class Class(use_metaclass(cache.CachedMetaClass, pr.IsScope)):
def __getattr__(self, name): def __getattr__(self, name):
if name not in ['start_pos', 'end_pos', 'parent', 'asserts', 'docstr', if name not in ['start_pos', 'end_pos', 'parent', 'asserts', 'docstr',
'get_imports', 'get_parent_until', 'get_code', 'subscopes']: 'doc', 'get_imports', 'get_parent_until', 'get_code',
'subscopes']:
raise AttributeError("Don't touch this: %s of %s !" % (name, self)) raise AttributeError("Don't touch this: %s of %s !" % (name, self))
return getattr(self.base, name) return getattr(self.base, name)

View File

@@ -346,6 +346,18 @@ class Class(Scope):
string += "pass\n" string += "pass\n"
return string return string
@property
def doc(self):
"""
Return a document string including call signature of __init__.
"""
for sub in self.subscopes:
if sub.name.names[-1] == '__init__':
return '%s\n\n%s' % (
sub.get_call_signature(funcname=self.name.names[-1]),
self.docstr)
return self.docstr
class Function(Scope): class Function(Scope):
""" """
@@ -394,16 +406,18 @@ class Function(Scope):
debug.warning("multiple names in param %s" % n) debug.warning("multiple names in param %s" % n)
return n return n
def get_call_signature(self, width=72): def get_call_signature(self, width=72, funcname=None):
""" """
Generate call signature of this function. Generate call signature of this function.
:param width: Fold lines if a line is longer than this value. :param width: Fold lines if a line is longer than this value.
:type width: int :type width: int
:arg funcname: Override function name when given.
:type funcname: str
:rtype: str :rtype: str
""" """
l = self.name.names[-1] + '(' l = (funcname or self.name.names[-1]) + '('
lines = [] lines = []
for (i, p) in enumerate(self.params): for (i, p) in enumerate(self.params):
code = p.get_code(False) code = p.get_code(False)

View File

@@ -98,6 +98,15 @@ class TestRegression(TestBase):
doc = defs[0].doc doc = defs[0].doc
assert "f(x, y = 1, z = 'a')" in doc assert "f(x, y = 1, z = 'a')" in doc
def test_class_call_signature(self):
defs = self.definition("""
class Foo:
def __init__(self, x, y=1, z='a'):
pass
Foo""")
doc = defs[0].doc
assert "Foo(self, x, y = 1, z = 'a')" in doc
def test_definition_at_zero(self): def test_definition_at_zero(self):
assert self.definition("a", (1, 1)) == [] assert self.definition("a", (1, 1)) == []
s = self.definition("str", (1, 1)) s = self.definition("str", (1, 1))