1
0
forked from VimPlug/jedi

Definition -> Name

This commit is contained in:
Dave Halter
2020-03-17 09:33:12 +01:00
parent 0731206b9d
commit d26926a582
10 changed files with 59 additions and 59 deletions

View File

@@ -52,7 +52,7 @@ will probably be Jedi 1.0.0.
- ``usages`` deprecated, use ``get_references`` instead - ``usages`` deprecated, use ``get_references`` instead
- ``jedi.names`` deprecated, use ``jedi.Script(...).get_names()`` - ``jedi.names`` deprecated, use ``jedi.Script(...).get_names()``
- ``BaseName.goto_assignments`` renamed to ``BaseName.goto`` - ``BaseName.goto_assignments`` renamed to ``BaseName.goto``
- Add follow_imports to ``Definition.goto``. Now its signature matches - Add follow_imports to ``Name.goto``. Now its signature matches
``Script.goto``. ``Script.goto``.
- **Python 2 support deprecated**. For this release it is best effort. Python 2 - **Python 2 support deprecated**. For this release it is best effort. Python 2
has reached the end of its life and now it's just about a smooth transition. has reached the end of its life and now it's just about a smooth transition.
@@ -92,13 +92,13 @@ will probably be Jedi 1.0.0.
New APIs: New APIs:
- ``Definition.get_signatures() -> List[Signature]``. Signatures are similar to - ``Name.get_signatures() -> List[Signature]``. Signatures are similar to
``CallSignature``. ``Definition.params`` is therefore deprecated. ``CallSignature``. ``Name.params`` is therefore deprecated.
- ``Signature.to_string()`` to format signatures. - ``Signature.to_string()`` to format signatures.
- ``Signature.params -> List[ParamDefinition]``, ParamDefinition has the - ``Signature.params -> List[ParamDefinition]``, ParamDefinition has the
following additional attributes ``infer_default()``, ``infer_annotation()``, following additional attributes ``infer_default()``, ``infer_annotation()``,
``to_string()``, and ``kind``. ``to_string()``, and ``kind``.
- ``Definition.execute() -> List[Definition]``, makes it possible to infer - ``Name.execute() -> List[Name]``, makes it possible to infer
return values of functions. return values of functions.
@@ -114,7 +114,7 @@ New APIs:
- Added ``goto_*(prefer_stubs=True)`` as well as ``goto_*(prefer_stubs=True)`` - Added ``goto_*(prefer_stubs=True)`` as well as ``goto_*(prefer_stubs=True)``
- Stubs are used now for type inference - Stubs are used now for type inference
- Typeshed is used for better type inference - Typeshed is used for better type inference
- Reworked Definition.full_name, should have more correct return values - Reworked Name.full_name, should have more correct return values
0.13.3 (2019-02-24) 0.13.3 (2019-02-24)
+++++++++++++++++++ +++++++++++++++++++
@@ -194,7 +194,7 @@ New APIs:
- Actual semantic completions for the complete Python syntax. - Actual semantic completions for the complete Python syntax.
- Basic type inference for ``yield from`` PEP 380. - Basic type inference for ``yield from`` PEP 380.
- PEP 484 support (most of the important features of it). Thanks Claude! (@reinhrst) - PEP 484 support (most of the important features of it). Thanks Claude! (@reinhrst)
- Added ``get_line_code`` to ``Definition`` and ``Completion`` objects. - Added ``get_line_code`` to ``Name`` and ``Completion`` objects.
- Completely rewritten the type inference engine. - Completely rewritten the type inference engine.
- A new and better parser for (fast) parsing diffs of Python code. - A new and better parser for (fast) parsing diffs of Python code.

View File

@@ -13,7 +13,7 @@ Abstract Base Class
Name Name
~~~~ ~~~~
.. autoclass:: jedi.api.classes.Definition .. autoclass:: jedi.api.classes.Name
:members: :members:
:show-inheritance: :show-inheritance:

View File

@@ -110,10 +110,10 @@ Type Inference / Goto
>>> script = jedi.Script(code) >>> script = jedi.Script(code)
>>> >>>
>>> script.goto(8, 1) >>> script.goto(8, 1)
[<Definition full_name='__main__.inception', description='inception = my_list[2]'>] [<Name full_name='__main__.inception', description='inception = my_list[2]'>]
>>> >>>
>>> script.infer(8, 1) >>> script.infer(8, 1)
[<Definition full_name='__main__.my_func', description='def my_func'>] [<Name full_name='__main__.my_func', description='def my_func'>]
References References
~~~~~~~~~~ ~~~~~~~~~~
@@ -130,9 +130,9 @@ References
>>> script = jedi.Script(code) >>> script = jedi.Script(code)
>>> rns = script.get_references(5, 8) >>> rns = script.get_references(5, 8)
>>> rns >>> rns
[<Definition full_name='__main__.x', description='x = 3'>, [<Name full_name='__main__.x', description='x = 3'>,
<Definition full_name='__main__.x', description='x = 4'>, <Name full_name='__main__.x', description='x = 4'>,
<Definition full_name='__main__.x', description='del x'>] <Name full_name='__main__.x', description='del x'>]
>>> rns[1].line >>> rns[1].line
3 3
>>> rns[1].column >>> rns[1].column

View File

@@ -299,7 +299,7 @@ class Script(object):
:param only_stubs: Only return stubs for this method. :param only_stubs: Only return stubs for this method.
:param prefer_stubs: Prefer stubs to Python objects for this method. :param prefer_stubs: Prefer stubs to Python objects for this method.
:rtype: list of :class:`.Definition` :rtype: list of :class:`.Name`
""" """
with debug.increase_indent_cm('infer'): with debug.increase_indent_cm('infer'):
return self._infer(line, column, **kwargs) return self._infer(line, column, **kwargs)
@@ -325,7 +325,7 @@ class Script(object):
prefer_stubs=prefer_stubs, prefer_stubs=prefer_stubs,
) )
defs = [classes.Definition(self._inference_state, c.name) for c in values] defs = [classes.Name(self._inference_state, c.name) for c in values]
# The additional set here allows the definitions to become unique in an # The additional set here allows the definitions to become unique in an
# API sense. In the internals we want to separate more things than in # API sense. In the internals we want to separate more things than in
# the API. # the API.
@@ -351,7 +351,7 @@ class Script(object):
to look up names in builtins (i.e. compiled or extension modules). to look up names in builtins (i.e. compiled or extension modules).
:param only_stubs: Only return stubs for this method. :param only_stubs: Only return stubs for this method.
:param prefer_stubs: Prefer stubs to Python objects for this method. :param prefer_stubs: Prefer stubs to Python objects for this method.
:rtype: list of :class:`.Definition` :rtype: list of :class:`.Name`
""" """
with debug.increase_indent_cm('goto'): with debug.increase_indent_cm('goto'):
return self._goto(line, column, **kwargs) return self._goto(line, column, **kwargs)
@@ -389,7 +389,7 @@ class Script(object):
prefer_stubs=prefer_stubs, prefer_stubs=prefer_stubs,
) )
defs = [classes.Definition(self._inference_state, d) for d in set(names)] defs = [classes.Name(self._inference_state, d) for d in set(names)]
# Avoid duplicates # Avoid duplicates
return list(set(helpers.sorted_definitions(defs))) return list(set(helpers.sorted_definitions(defs)))
@@ -403,7 +403,7 @@ class Script(object):
:param bool all_scopes: Default False; searches not only for :param bool all_scopes: Default False; searches not only for
definitions on the top level of a module level, but also in definitions on the top level of a module level, but also in
functions and classes. functions and classes.
:yields: :class:`.Definition` :yields: :class:`.Name`
""" """
return self._search(string, **kwargs) # Python 2 ... return self._search(string, **kwargs) # Python 2 ...
@@ -448,11 +448,11 @@ class Script(object):
Typically you will want to display :meth:`.BaseName.docstring` to the Typically you will want to display :meth:`.BaseName.docstring` to the
user for all the returned definitions. user for all the returned definitions.
The additional definitions are ``Definition(...).type == 'keyword'``. The additional definitions are ``Name(...).type == 'keyword'``.
These definitions do not have a lot of value apart from their docstring These definitions do not have a lot of value apart from their docstring
attribute, which contains the output of Python's :func:`help` function. attribute, which contains the output of Python's :func:`help` function.
:rtype: list of :class:`.Definition` :rtype: list of :class:`.Name`
""" """
definitions = self.goto(line, column, follow_imports=True) definitions = self.goto(line, column, follow_imports=True)
if definitions: if definitions:
@@ -462,7 +462,7 @@ class Script(object):
reserved = self._inference_state.grammar._pgen_grammar.reserved_syntax_strings.keys() reserved = self._inference_state.grammar._pgen_grammar.reserved_syntax_strings.keys()
if leaf.value in reserved: if leaf.value in reserved:
name = KeywordName(self._inference_state, leaf.value) name = KeywordName(self._inference_state, leaf.value)
return [classes.Definition(self._inference_state, name)] return [classes.Name(self._inference_state, name)]
return [] return []
def usages(self, **kwargs): def usages(self, **kwargs):
@@ -478,7 +478,7 @@ class Script(object):
:param include_builtins: Default True, checks if a reference is a :param include_builtins: Default True, checks if a reference is a
builtin (e.g. ``sys``) and in that case does not return it. builtin (e.g. ``sys``) and in that case does not return it.
:rtype: list of :class:`.Definition` :rtype: list of :class:`.Name`
""" """
def _references(include_builtins=True): def _references(include_builtins=True):
@@ -489,7 +489,7 @@ class Script(object):
names = find_references(self._get_module_context(), tree_name) names = find_references(self._get_module_context(), tree_name)
definitions = [classes.Definition(self._inference_state, n) for n in names] definitions = [classes.Name(self._inference_state, n) for n in names]
if not include_builtins: if not include_builtins:
definitions = [d for d in definitions if not d.in_builtin_module()] definitions = [d for d in definitions if not d.in_builtin_module()]
return helpers.sorted_definitions(definitions) return helpers.sorted_definitions(definitions)
@@ -542,7 +542,7 @@ class Script(object):
Returns the context of cursor. This basically means the function, class Returns the context of cursor. This basically means the function, class
or module where the cursor is at. or module where the cursor is at.
:rtype: :class:`.Definition` :rtype: :class:`.Name`
""" """
pos = (line, column) pos = (line, column)
leaf = self._module_node.get_leaf_for_position(pos, include_prefixes=True) leaf = self._module_node.get_leaf_for_position(pos, include_prefixes=True)
@@ -566,7 +566,7 @@ class Script(object):
while context.name is None: while context.name is None:
context = context.parent_context # comprehensions context = context.parent_context # comprehensions
definition = classes.Definition(self._inference_state, context.name) definition = classes.Name(self._inference_state, context.name)
while definition.type != 'module': while definition.type != 'module':
name = definition._name # TODO private access name = definition._name # TODO private access
tree_name = name.tree_name tree_name = name.tree_name
@@ -621,10 +621,10 @@ class Script(object):
class, function or a statement (``a = b`` returns ``a``). class, function or a statement (``a = b`` returns ``a``).
:param references: If True lists all the names that are not listed by :param references: If True lists all the names that are not listed by
``definitions=True``. E.g. ``a = b`` returns ``b``. ``definitions=True``. E.g. ``a = b`` returns ``b``.
:rtype: list of :class:`.Definition` :rtype: list of :class:`.Name`
""" """
names = self._names(**kwargs) names = self._names(**kwargs)
return [classes.Definition(self._inference_state, n) for n in names] return [classes.Name(self._inference_state, n) for n in names]
def get_syntax_errors(self): def get_syntax_errors(self):
""" """

View File

@@ -2,7 +2,7 @@
There are a couple of classes documented in here: There are a couple of classes documented in here:
- :class:`.BaseName` as an abstact base class for almost everything. - :class:`.BaseName` as an abstact base class for almost everything.
- :class:`.Definition` used in a lot of places - :class:`.Name` used in a lot of places
- :class:`.Completion` for completions - :class:`.Completion` for completions
- :class:`.BaseSignature` as a base class for signatures - :class:`.BaseSignature` as a base class for signatures
- :class:`.Signature` for :meth:`.Script.get_signatures` only - :class:`.Signature` for :meth:`.Script.get_signatures` only
@@ -41,15 +41,15 @@ def defined_names(inference_state, context):
List sub-definitions (e.g., methods in class). List sub-definitions (e.g., methods in class).
:type scope: Scope :type scope: Scope
:rtype: list of Definition :rtype: list of Name
""" """
filter = next(context.get_filters()) filter = next(context.get_filters())
names = [name for name in filter.values()] names = [name for name in filter.values()]
return [Definition(inference_state, n) for n in _sort_names_by_start_pos(names)] return [Name(inference_state, n) for n in _sort_names_by_start_pos(names)]
def _values_to_definitions(values): def _values_to_definitions(values):
return [Definition(c.inference_state, c.name) for c in values] return [Name(c.inference_state, c.name) for c in values]
class BaseName(object): class BaseName(object):
@@ -150,10 +150,10 @@ class BaseName(object):
>>> defs = sorted(defs, key=lambda d: d.line) >>> defs = sorted(defs, key=lambda d: d.line)
>>> no_unicode_pprint(defs) # doctest: +NORMALIZE_WHITESPACE >>> no_unicode_pprint(defs) # doctest: +NORMALIZE_WHITESPACE
[<Definition full_name='keyword', description='module keyword'>, [<Name full_name='keyword', description='module keyword'>,
<Definition full_name='__main__.C', description='class C'>, <Name full_name='__main__.C', description='class C'>,
<Definition full_name='__main__.D', description='instance D'>, <Name full_name='__main__.D', description='instance D'>,
<Definition full_name='__main__.f', description='def f'>] <Name full_name='__main__.f', description='def f'>]
Finally, here is what you can get from :attr:`type`: Finally, here is what you can get from :attr:`type`:
@@ -280,7 +280,7 @@ class BaseName(object):
@property @property
def description(self): def description(self):
""" """
A description of the :class:`.Definition` object, which is heavily used A description of the :class:`.Name` object, which is heavily used
in testing. e.g. for ``isinstance`` it returns ``def isinstance``. in testing. e.g. for ``isinstance`` it returns ``def isinstance``.
Example: Example:
@@ -299,8 +299,8 @@ class BaseName(object):
>>> defs = script.infer(column=3) >>> defs = script.infer(column=3)
>>> defs = sorted(defs, key=lambda d: d.line) >>> defs = sorted(defs, key=lambda d: d.line)
>>> no_unicode_pprint(defs) # doctest: +NORMALIZE_WHITESPACE >>> no_unicode_pprint(defs) # doctest: +NORMALIZE_WHITESPACE
[<Definition full_name='__main__.f', description='def f'>, [<Name full_name='__main__.f', description='def f'>,
<Definition full_name='__main__.C', description='class C'>] <Name full_name='__main__.C', description='class C'>]
>>> str(defs[0].description) # strip literals in python2 >>> str(defs[0].description) # strip literals in python2
'def f' 'def f'
>>> str(defs[1].description) >>> str(defs[1].description)
@@ -396,7 +396,7 @@ class BaseName(object):
look up names in builtins (i.e. compiled or extension modules). look up names in builtins (i.e. compiled or extension modules).
:param only_stubs: Only return stubs for this goto call. :param only_stubs: Only return stubs for this goto call.
:param prefer_stubs: Prefer stubs to Python objects for this goto call. :param prefer_stubs: Prefer stubs to Python objects for this goto call.
:rtype: list of :class:`Definition` :rtype: list of :class:`Name`
""" """
with debug.increase_indent_cm('goto for %s' % self._name): with debug.increase_indent_cm('goto for %s' % self._name):
return self._goto(**kwargs) return self._goto(**kwargs)
@@ -423,7 +423,7 @@ class BaseName(object):
only_stubs=only_stubs, only_stubs=only_stubs,
prefer_stubs=prefer_stubs, prefer_stubs=prefer_stubs,
) )
return [self if n == self._name else Definition(self._inference_state, n) return [self if n == self._name else Name(self._inference_state, n)
for n in names] for n in names]
def infer(self, **kwargs): # Python 2... def infer(self, **kwargs): # Python 2...
@@ -441,7 +441,7 @@ class BaseName(object):
:param only_stubs: Only return stubs for this goto call. :param only_stubs: Only return stubs for this goto call.
:param prefer_stubs: Prefer stubs to Python objects for this type :param prefer_stubs: Prefer stubs to Python objects for this type
inference call. inference call.
:rtype: list of :class:`Definition` :rtype: list of :class:`Name`
""" """
with debug.increase_indent_cm('infer for %s' % self._name): with debug.increase_indent_cm('infer for %s' % self._name):
return self._infer(**kwargs) return self._infer(**kwargs)
@@ -462,7 +462,7 @@ class BaseName(object):
prefer_stubs=prefer_stubs, prefer_stubs=prefer_stubs,
) )
resulting_names = [c.name for c in values] resulting_names = [c.name for c in values]
return [self if n == self._name else Definition(self._inference_state, n) return [self if n == self._name else Name(self._inference_state, n)
for n in resulting_names] for n in resulting_names]
@property @property
@@ -477,7 +477,7 @@ class BaseName(object):
# with overloading. # with overloading.
for signature in self._get_signatures(): for signature in self._get_signatures():
return [ return [
Definition(self._inference_state, n) Name(self._inference_state, n)
for n in signature.get_param_names(resolve_stars=True) for n in signature.get_param_names(resolve_stars=True)
] ]
@@ -491,7 +491,7 @@ class BaseName(object):
""" """
Returns the parent scope of this identifier. Returns the parent scope of this identifier.
:rtype: Definition :rtype: Name
""" """
if not self._name.is_value_name: if not self._name.is_value_name:
return None return None
@@ -518,7 +518,7 @@ class BaseName(object):
# Happens for comprehension contexts # Happens for comprehension contexts
context = context.parent_context context = context.parent_context
return Definition(self._inference_state, context.name) return Name(self._inference_state, context.name)
def __repr__(self): def __repr__(self):
return "<%s %sname=%r, description=%r>" % ( return "<%s %sname=%r, description=%r>" % (
@@ -576,7 +576,7 @@ class BaseName(object):
Uses type inference to "execute" this identifier and returns the Uses type inference to "execute" this identifier and returns the
executed objects. executed objects.
:rtype: list of :class:`Definition` :rtype: list of :class:`Name`
""" """
return _values_to_definitions(self._name.infer().execute_with_values()) return _values_to_definitions(self._name.infer().execute_with_values())
@@ -718,13 +718,13 @@ class Completion(BaseName):
return '<%s: %s>' % (type(self).__name__, self._name.get_public_name()) return '<%s: %s>' % (type(self).__name__, self._name.get_public_name())
class Definition(BaseName): class Name(BaseName):
""" """
*Definition* objects are returned from many different APIs including *Name* objects are returned from many different APIs including
:meth:`.Script.goto` or :meth:`.Script.infer`. :meth:`.Script.goto` or :meth:`.Script.infer`.
""" """
def __init__(self, inference_state, definition): def __init__(self, inference_state, definition):
super(Definition, self).__init__(inference_state, definition) super(Name, self).__init__(inference_state, definition)
@property @property
def desc_with_module(self): def desc_with_module(self):
@@ -741,7 +741,7 @@ class Definition(BaseName):
""" """
List sub-definitions (e.g., methods in class). List sub-definitions (e.g., methods in class).
:rtype: list of :class:`Definition` :rtype: list of :class:`Name`
""" """
defs = self._name.infer() defs = self._name.infer()
return sorted( return sorted(
@@ -772,7 +772,7 @@ class Definition(BaseName):
return hash((self._name.start_pos, self.module_path, self.name, self._inference_state)) return hash((self._name.start_pos, self.module_path, self.name, self._inference_state))
class BaseSignature(Definition): class BaseSignature(Name):
""" """
These signatures are returned by :meth:`BaseName.get_signatures` These signatures are returned by :meth:`BaseName.get_signatures`
calls. calls.
@@ -842,12 +842,12 @@ class Signature(BaseSignature):
) )
class ParamDefinition(Definition): class ParamDefinition(Name):
def infer_default(self): def infer_default(self):
""" """
Returns default values like the ``1`` of ``def foo(x=1):``. Returns default values like the ``1`` of ``def foo(x=1):``.
:rtype: list of :class:`.Definition` :rtype: list of :class:`.Name`
""" """
return _values_to_definitions(self._name.infer_default()) return _values_to_definitions(self._name.infer_default())
@@ -855,7 +855,7 @@ class ParamDefinition(Definition):
""" """
:param execute_annotation: Default True; If False, values are not :param execute_annotation: Default True; If False, values are not
executed and classes are returned instead of instances. executed and classes are returned instead of instances.
:rtype: list of :class:`.Definition` :rtype: list of :class:`.Name`
""" """
return _values_to_definitions(self._name.infer_annotation(ignore_stars=True, **kwargs)) return _values_to_definitions(self._name.infer_annotation(ignore_stars=True, **kwargs))

View File

@@ -612,6 +612,6 @@ def search_in_module(inference_state, module_context, names, wanted_names,
is_fuzzy=fuzzy, is_fuzzy=fuzzy,
) )
else: else:
def_ = classes.Definition(inference_state, n2) def_ = classes.Name(inference_state, n2)
if not wanted_type or wanted_type == def_.type: if not wanted_type or wanted_type == def_.type:
yield def_ yield def_

View File

@@ -230,7 +230,7 @@ class Project(object):
:param bool all_scopes: Default False; searches not only for :param bool all_scopes: Default False; searches not only for
definitions on the top level of a module level, but also in definitions on the top level of a module level, but also in
functions and classes. functions and classes.
:yields: :class:`.Definition` :yields: :class:`.Name`
""" """
return self._search(string, **kwargs) return self._search(string, **kwargs)

View File

@@ -124,7 +124,7 @@ import pytest
import jedi import jedi
from jedi import debug from jedi import debug
from jedi._compatibility import unicode, is_py3 from jedi._compatibility import unicode, is_py3
from jedi.api.classes import Definition from jedi.api.classes import Name
from jedi.api.completion import get_user_context from jedi.api.completion import get_user_context
from jedi import parser_utils from jedi import parser_utils
from jedi.api.environment import get_default_environment, get_system_environment from jedi.api.environment import get_default_environment, get_system_environment
@@ -244,7 +244,7 @@ class IntegrationTestCase(BaseTestCase):
raise Exception('Could not resolve %s on line %s' raise Exception('Could not resolve %s on line %s'
% (match.string, self.line_nr - 1)) % (match.string, self.line_nr - 1))
should_be |= set(Definition(inference_state, r.name) for r in results) should_be |= set(Name(inference_state, r.name) for r in results)
debug.dbg('Finished getting types', color='YELLOW') debug.dbg('Finished getting types', color='YELLOW')
# Because the objects have different ids, `repr`, then compare. # Because the objects have different ids, `repr`, then compare.

View File

@@ -141,7 +141,7 @@ def test_infer_on_generator(Script):
def test_goto_definition_not_multiple(Script): def test_goto_definition_not_multiple(Script):
""" """
There should be only one Definition result if it leads back to the same There should be only one result if it leads back to the same
origin (e.g. instance method) origin (e.g. instance method)
""" """

View File

@@ -304,7 +304,7 @@ def test_builtins(Script):
def test_signature_is_definition(Script): def test_signature_is_definition(Script):
""" """
Through inheritance, a signature is a sub class of Definition. Through inheritance, a signature is a sub class of Name.
Check if the attributes match. Check if the attributes match.
""" """
s = """class Spam(): pass\nSpam""" s = """class Spam(): pass\nSpam"""