Better control over docstring generation

This commit is contained in:
Dave Halter
2019-05-05 19:50:52 +02:00
parent d0b0fb3cb3
commit 45a5eee18a
3 changed files with 25 additions and 7 deletions

View File

@@ -41,6 +41,8 @@ class TreeSignature(AbstractSignature):
return get_call_signature( return get_call_signature(
self._function_context.tree_node, self._function_context.tree_node,
call_string=self.name.string_name, call_string=self.name.string_name,
omit_first_param=self.is_bound,
omit_return_annotation=self.context.is_class(),
) )

View File

@@ -138,7 +138,8 @@ def safe_literal_eval(value):
return '' return ''
def get_call_signature(funcdef, width=72, call_string=None): def get_call_signature(funcdef, width=72, call_string=None,
omit_first_param=False, omit_return_annotation=False):
""" """
Generate call signature of this function. Generate call signature of this function.
@@ -155,12 +156,14 @@ def get_call_signature(funcdef, width=72, call_string=None):
call_string = '<lambda>' call_string = '<lambda>'
else: else:
call_string = funcdef.name.value call_string = funcdef.name.value
if funcdef.type == 'lambdef': params = funcdef.get_params()
p = '(' + ''.join(param.get_code() for param in funcdef.get_params()).strip() + ')' if omit_first_param:
else: params = params[1:]
p = funcdef.children[2].get_code() print(params, omit_first_param)
p = '(' + ''.join(param.get_code() for param in params).strip() + ')'
# TODO this is pretty bad, we should probably just normalize.
p = re.sub(r'\s+', ' ', p) p = re.sub(r'\s+', ' ', p)
if funcdef.annotation: if funcdef.annotation and not omit_return_annotation:
rtype = " ->" + funcdef.annotation.get_code() rtype = " ->" + funcdef.annotation.get_code()
else: else:
rtype = "" rtype = ""

View File

@@ -35,7 +35,20 @@ def test_class_doc(Script):
class TestClass(): class TestClass():
'''Docstring of `TestClass`.''' '''Docstring of `TestClass`.'''
TestClass""").goto_definitions() TestClass""").goto_definitions()
assert defs[0].docstring() == 'Docstring of `TestClass`.'
expected = 'Docstring of `TestClass`.'
assert defs[0].docstring(raw=True) == expected
assert defs[0].docstring() == 'TestClass()\n\n' + expected
def test_class_doc_with_init(Script):
d, = Script("""
class TestClass():
'''Docstring'''
def __init__(self, foo, bar=3): pass
TestClass""").goto_definitions()
assert d.docstring() == 'TestClass(foo, bar=3)\n\nDocstring'
def test_instance_doc(Script): def test_instance_doc(Script):