From 673ea0c5a5ff5ae5ddc6aa30bad3a11d2270bdb6 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Fri, 3 Jan 2020 10:38:00 +0100 Subject: [PATCH] Little refactoring --- jedi/api/classes.py | 105 ++++++++++++++++++++------------------------ 1 file changed, 47 insertions(+), 58 deletions(-) diff --git a/jedi/api/classes.py b/jedi/api/classes.py index a9ec284d..4a3a07d2 100644 --- a/jedi/api/classes.py +++ b/jedi/api/classes.py @@ -256,8 +256,53 @@ class BaseDefinition(object): @property def description(self): - """A textual description of the object.""" - return self._name.get_public_name() + """ + A description of the :class:`.Definition` object, which is heavily used + in testing. e.g. for ``isinstance`` it returns ``def isinstance``. + + Example: + + >>> from jedi._compatibility import no_unicode_pprint + >>> from jedi import Script + >>> source = ''' + ... def f(): + ... pass + ... + ... class C: + ... pass + ... + ... variable = f if random.choice([0,1]) else C''' + >>> script = Script(source) # line is maximum by default + >>> defs = script.infer(column=3) + >>> defs = sorted(defs, key=lambda d: d.line) + >>> no_unicode_pprint(defs) # doctest: +NORMALIZE_WHITESPACE + [, + ] + >>> str(defs[0].description) # strip literals in python2 + 'def f' + >>> str(defs[1].description) + 'class C' + + """ + typ = self.type + tree_name = self._name.tree_name + if typ == 'param': + return typ + ' ' + self._name.to_string() + if typ in ('function', 'class', 'module', 'instance') or tree_name is None: + if typ == 'function': + # For the description we want a short and a pythonic way. + typ = 'def' + return typ + ' ' + self._name.get_public_name() + + definition = tree_name.get_definition(include_setitem=True) or tree_name + # Remove the prefix, because that's not what we want for get_code + # here. + txt = definition.get_code(include_prefix=False) + # Delete comments: + txt = re.sub(r'#[^\n]+\n', ' ', txt) + # Delete multi spaces/newlines + txt = re.sub(r'\s+', ' ', txt).strip() + return txt @property def full_name(self): @@ -527,12 +572,6 @@ class Completion(BaseDefinition): fast = False return super(Completion, self).docstring(raw=raw, fast=fast) - @property - def description(self): - """Provide a description of the completion object.""" - # TODO improve the class structure. - return Definition.description.__get__(self) - def __repr__(self): return '<%s: %s>' % (type(self).__name__, self._name.get_public_name()) @@ -564,56 +603,6 @@ class Definition(BaseDefinition): def __init__(self, inference_state, definition): super(Definition, self).__init__(inference_state, definition) - @property - def description(self): - """ - A description of the :class:`.Definition` object, which is heavily used - in testing. e.g. for ``isinstance`` it returns ``def isinstance``. - - Example: - - >>> from jedi._compatibility import no_unicode_pprint - >>> from jedi import Script - >>> source = ''' - ... def f(): - ... pass - ... - ... class C: - ... pass - ... - ... variable = f if random.choice([0,1]) else C''' - >>> script = Script(source) # line is maximum by default - >>> defs = script.infer(column=3) - >>> defs = sorted(defs, key=lambda d: d.line) - >>> no_unicode_pprint(defs) # doctest: +NORMALIZE_WHITESPACE - [, - ] - >>> str(defs[0].description) # strip literals in python2 - 'def f' - >>> str(defs[1].description) - 'class C' - - """ - typ = self.type - tree_name = self._name.tree_name - if typ == 'param': - return typ + ' ' + self._name.to_string() - if typ in ('function', 'class', 'module', 'instance') or tree_name is None: - if typ == 'function': - # For the description we want a short and a pythonic way. - typ = 'def' - return typ + ' ' + self._name.get_public_name() - - definition = tree_name.get_definition(include_setitem=True) or tree_name - # Remove the prefix, because that's not what we want for get_code - # here. - txt = definition.get_code(include_prefix=False) - # Delete comments: - txt = re.sub(r'#[^\n]+\n', ' ', txt) - # Delete multi spaces/newlines - txt = re.sub(r'\s+', ' ', txt).strip() - return txt - @property def desc_with_module(self): """