1
0
forked from VimPlug/jedi

add a Documentation() class to the API, which will be used in the future for all kind of docstrings. Also add a documentation method on BaseDefinition that returns a Documentation object. Deprecate at the same time its doc and raw_doc functions

This commit is contained in:
Dave Halter
2014-03-25 02:14:34 +01:00
parent fa664534e4
commit 3f3788e800
3 changed files with 44 additions and 16 deletions

View File

@@ -202,8 +202,7 @@ class BaseDefinition(object):
return None return None
return self._start_pos[1] return self._start_pos[1]
@property def documentation(self):
def doc(self):
r""" r"""
Return a document string for this completion object. Return a document string for this completion object.
@@ -215,31 +214,38 @@ class BaseDefinition(object):
... "Document for function f." ... "Document for function f."
... ''' ... '''
>>> script = Script(source, 1, len('def f'), 'example.py') >>> script = Script(source, 1, len('def f'), 'example.py')
>>> d = script.goto_definitions()[0] >>> doc = script.goto_definitions()[0].documentation()
>>> print(d.doc) >>> print(doc)
f(a, b = 1) f(a, b = 1)
<BLANKLINE> <BLANKLINE>
Document for function f. Document for function f.
Notice that useful extra information is added to the actual Notice that useful extra information is added to the actual
docstring. For function, it is call signature. If you need docstring. For function, it is call signature. If you need
actual docstring, use :attr:`raw_doc` instead. actual docstring, use :attr:`raw` instead.
>>> print(d.raw_doc) >>> print(doc.raw())
Document for function f. Document for function f.
""" """
try: return Documentation(self._definition)
return self._definition.doc
except AttributeError: @property
return self.raw_doc def doc(self):
"""
.. deprecated:: 0.8.0
Use :meth:`.documentation` instead.
.. todo:: Remove!
"""
warnings.warn("Use documentation() instead.", DeprecationWarning)
return self.documentation()
@property @property
def raw_doc(self): def raw_doc(self):
""" """
The raw docstring ``__doc__`` for any object. .. deprecated:: 0.8.0
Use :meth:`.documentation` instead.
See :attr:`doc` for example. .. todo:: Remove!
""" """
try: try:
return self._definition.raw_doc return self._definition.raw_doc
@@ -637,3 +643,25 @@ class _Param(Definition):
""" """
warnings.warn("Use description instead.", DeprecationWarning) warnings.warn("Use description instead.", DeprecationWarning)
return self.description return self.description
class Documentation(object):
def __init__(self, definition):
self._definition = definition
def __str__(self):
try:
return self._definition.doc
except AttributeError:
return self.raw_doc
def raw(self):
"""
The raw docstring ``__doc__`` for any object.
See :attr:`doc` for example.
"""
try:
return self._definition.raw_doc
except AttributeError:
return ''

View File

@@ -93,7 +93,7 @@ def test_function_call_signature_in_doc():
pass pass
f""").goto_definitions() f""").goto_definitions()
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 str(doc)
def test_class_call_signature(): def test_class_call_signature():
@@ -103,7 +103,7 @@ def test_class_call_signature():
pass pass
Foo""").goto_definitions() Foo""").goto_definitions()
doc = defs[0].doc doc = defs[0].doc
assert "Foo(self, x, y = 1, z = 'a')" in doc assert "Foo(self, x, y = 1, z = 'a')" in str(doc)
def test_position_none_if_builtin(): def test_position_none_if_builtin():

View File

@@ -193,7 +193,7 @@ def test_signature_is_definition():
# Now compare all the attributes that a CallSignature must also have. # Now compare all the attributes that a CallSignature must also have.
for attr_name in dir(definition): for attr_name in dir(definition):
dont_scan = ['defined_names', 'line_nr', 'start_pos'] dont_scan = ['defined_names', 'line_nr', 'start_pos', 'documentation', 'doc']
if attr_name.startswith('_') or attr_name in dont_scan: if attr_name.startswith('_') or attr_name in dont_scan:
continue continue
attribute = getattr(definition, attr_name) attribute = getattr(definition, attr_name)