mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-08 14:54:47 +08:00
Remove NamePart from existance and rename it to Name.
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
import pydoc
|
import pydoc
|
||||||
import keyword
|
import keyword
|
||||||
|
|
||||||
from jedi.parser.representation import NamePart
|
|
||||||
from jedi._compatibility import is_py3
|
from jedi._compatibility import is_py3
|
||||||
from jedi import common
|
from jedi import common
|
||||||
from jedi.evaluate import compiled
|
from jedi.evaluate import compiled
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ from jedi.evaluate import helpers
|
|||||||
|
|
||||||
def usages(evaluator, definitions, mods):
|
def usages(evaluator, definitions, mods):
|
||||||
"""
|
"""
|
||||||
:param definitions: list of NameParts
|
:param definitions: list of Name
|
||||||
"""
|
"""
|
||||||
def compare_array(definitions):
|
def compare_array(definitions):
|
||||||
""" `definitions` are being compared by module/start_pos, because
|
""" `definitions` are being compared by module/start_pos, because
|
||||||
@@ -34,7 +34,7 @@ def usages(evaluator, definitions, mods):
|
|||||||
follow = [] # There might be multiple search_name's in one call_path
|
follow = [] # There might be multiple search_name's in one call_path
|
||||||
call_path = list(call.generate_call_path())
|
call_path = list(call.generate_call_path())
|
||||||
for i, name in enumerate(call_path):
|
for i, name in enumerate(call_path):
|
||||||
# name is `pr.NamePart`.
|
# name is `pr.Name`.
|
||||||
if u(name) == search_name:
|
if u(name) == search_name:
|
||||||
follow.append(call_path[:i + 1])
|
follow.append(call_path[:i + 1])
|
||||||
|
|
||||||
|
|||||||
@@ -243,7 +243,7 @@ def save_parser(path, name, parser, pickling=True):
|
|||||||
|
|
||||||
class ParserPickling(object):
|
class ParserPickling(object):
|
||||||
|
|
||||||
version = 17
|
version = 18
|
||||||
"""
|
"""
|
||||||
Version number (integer) for file system cache.
|
Version number (integer) for file system cache.
|
||||||
|
|
||||||
|
|||||||
@@ -219,7 +219,7 @@ class Evaluator(object):
|
|||||||
else:
|
else:
|
||||||
types = [iterable.Array(self, current)]
|
types = [iterable.Array(self, current)]
|
||||||
else:
|
else:
|
||||||
if isinstance(current, pr.NamePart):
|
if isinstance(current, pr.Name):
|
||||||
# This is the first global lookup.
|
# This is the first global lookup.
|
||||||
types = self.find_types(scope, current, position=position,
|
types = self.find_types(scope, current, position=position,
|
||||||
search_global=True)
|
search_global=True)
|
||||||
@@ -336,7 +336,7 @@ class Evaluator(object):
|
|||||||
# statement name definitions. Only return, if it's one name and one
|
# statement name definitions. Only return, if it's one name and one
|
||||||
# name only. Otherwise it's a mixture between a definition and a
|
# name only. Otherwise it's a mixture between a definition and a
|
||||||
# reference. In this case it's just a definition. So we stay on it.
|
# reference. In this case it's just a definition. So we stay on it.
|
||||||
if len(call_path) == 1 and isinstance(call_path[0], pr.NamePart) \
|
if len(call_path) == 1 and isinstance(call_path[0], pr.Name) \
|
||||||
and call_path[0] in stmt.get_defined_names():
|
and call_path[0] in stmt.get_defined_names():
|
||||||
# Named params should get resolved to their param definitions.
|
# Named params should get resolved to their param definitions.
|
||||||
if pr.Array.is_type(stmt.parent, pr.Array.TUPLE, pr.Array.NOARRAY) \
|
if pr.Array.is_type(stmt.parent, pr.Array.TUPLE, pr.Array.NOARRAY) \
|
||||||
@@ -382,7 +382,7 @@ class Evaluator(object):
|
|||||||
|
|
||||||
def filter_private_variable(scope, call_scope, var_name):
|
def filter_private_variable(scope, call_scope, var_name):
|
||||||
"""private variables begin with a double underline `__`"""
|
"""private variables begin with a double underline `__`"""
|
||||||
var_name = str(var_name) # var_name could be a NamePart
|
var_name = str(var_name) # var_name could be a Name
|
||||||
if isinstance(var_name, (str, unicode)) and isinstance(scope, er.Instance)\
|
if isinstance(var_name, (str, unicode)) and isinstance(scope, er.Instance)\
|
||||||
and var_name.startswith('__') and not var_name.endswith('__'):
|
and var_name.startswith('__') and not var_name.endswith('__'):
|
||||||
s = call_scope.get_parent_until((pr.Class, er.Instance, compiled.CompiledObject))
|
s = call_scope.get_parent_until((pr.Class, er.Instance, compiled.CompiledObject))
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ def search_params(evaluator, param):
|
|||||||
|
|
||||||
# Need to take right index, because there could be a
|
# Need to take right index, because there could be a
|
||||||
# func usage before.
|
# func usage before.
|
||||||
call_path_simple = [unicode(d) if isinstance(d, pr.NamePart)
|
call_path_simple = [unicode(d) if isinstance(d, pr.Name)
|
||||||
else d for d in call_path]
|
else d for d in call_path]
|
||||||
i = listRightIndex(call_path_simple, func_name)
|
i = listRightIndex(call_path_simple, func_name)
|
||||||
before, after = call_path[:i], call_path[i + 1:]
|
before, after = call_path[:i], call_path[i + 1:]
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ class NameFinder(object):
|
|||||||
types = self._names_to_types(names, resolve_decorator)
|
types = self._names_to_types(names, resolve_decorator)
|
||||||
|
|
||||||
if not names and not types \
|
if not names and not types \
|
||||||
and not (isinstance(self.name_str, pr.NamePart)
|
and not (isinstance(self.name_str, pr.Name)
|
||||||
and isinstance(self.name_str.parent.parent, pr.Param)):
|
and isinstance(self.name_str.parent.parent, pr.Param)):
|
||||||
if not isinstance(self.name_str, (str, unicode)): # TODO Remove?
|
if not isinstance(self.name_str, (str, unicode)): # TODO Remove?
|
||||||
if search_global:
|
if search_global:
|
||||||
@@ -148,7 +148,7 @@ class NameFinder(object):
|
|||||||
def _check_getattr(self, inst):
|
def _check_getattr(self, inst):
|
||||||
"""Checks for both __getattr__ and __getattribute__ methods"""
|
"""Checks for both __getattr__ and __getattribute__ methods"""
|
||||||
result = []
|
result = []
|
||||||
# str is important to lose the NamePart!
|
# str is important, because it shouldn't be `Name`!
|
||||||
name = compiled.create(self._evaluator, str(self.name_str))
|
name = compiled.create(self._evaluator, str(self.name_str))
|
||||||
with common.ignored(KeyError):
|
with common.ignored(KeyError):
|
||||||
result = inst.execute_subscope_by_name('__getattr__', [name])
|
result = inst.execute_subscope_by_name('__getattr__', [name])
|
||||||
@@ -219,7 +219,7 @@ class NameFinder(object):
|
|||||||
evaluator = self._evaluator
|
evaluator = self._evaluator
|
||||||
|
|
||||||
# Add isinstance and other if/assert knowledge.
|
# Add isinstance and other if/assert knowledge.
|
||||||
if isinstance(self.name_str, pr.NamePart):
|
if isinstance(self.name_str, pr.Name):
|
||||||
flow_scope = self.name_str.parent.parent
|
flow_scope = self.name_str.parent.parent
|
||||||
# Ignore FunctionExecution parents for now.
|
# Ignore FunctionExecution parents for now.
|
||||||
until = flow_scope.get_parent_until(er.FunctionExecution)
|
until = flow_scope.get_parent_until(er.FunctionExecution)
|
||||||
@@ -455,13 +455,13 @@ def get_names_of_scope(evaluator, scope, position=None, star_search=True, includ
|
|||||||
>>> from jedi.evaluate import Evaluator
|
>>> from jedi.evaluate import Evaluator
|
||||||
>>> pairs = list(get_names_of_scope(Evaluator(), scope))
|
>>> pairs = list(get_names_of_scope(Evaluator(), scope))
|
||||||
>>> pairs[0]
|
>>> pairs[0]
|
||||||
(<Function: func@3-5>, [<NamePart: y@4,4>])
|
(<Function: func@3-5>, [<Name: y@4,4>])
|
||||||
|
|
||||||
Then it yield the names from one level outer scope. For this example, this
|
Then it yield the names from one level outer scope. For this example, this
|
||||||
is the most outer scope.
|
is the most outer scope.
|
||||||
|
|
||||||
>>> pairs[1]
|
>>> pairs[1]
|
||||||
(<ModuleWrapper: <SubModule: None@1-5>>, [<NamePart: x@2,0>, <NamePart: func@3,4>])
|
(<ModuleWrapper: <SubModule: None@1-5>>, [<Name: x@2,0>, <Name: func@3,4>])
|
||||||
|
|
||||||
After that we have a few underscore names that have been defined
|
After that we have a few underscore names that have been defined
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ def deep_ast_copy(obj, new_elements_default=None):
|
|||||||
return key_value[0] not in ('_expression_list', '_assignment_details')
|
return key_value[0] not in ('_expression_list', '_assignment_details')
|
||||||
|
|
||||||
new_elements = new_elements_default or {}
|
new_elements = new_elements_default or {}
|
||||||
accept = (pr.Simple, pr.NamePart, pr.KeywordStatement)
|
accept = (pr.Simple, pr.Name, pr.KeywordStatement)
|
||||||
|
|
||||||
def recursion(obj):
|
def recursion(obj):
|
||||||
# If it's already in the cache, just return it.
|
# If it's already in the cache, just return it.
|
||||||
@@ -242,7 +242,7 @@ def get_module_name_parts(module):
|
|||||||
# token_list anymore, but for now this is the easiest way to get
|
# token_list anymore, but for now this is the easiest way to get
|
||||||
# all the name_parts.
|
# all the name_parts.
|
||||||
for tok in stmt_or_import._token_list:
|
for tok in stmt_or_import._token_list:
|
||||||
if isinstance(tok, pr.NamePart):
|
if isinstance(tok, pr.Name):
|
||||||
name_parts.add(tok)
|
name_parts.add(tok)
|
||||||
|
|
||||||
return name_parts
|
return name_parts
|
||||||
@@ -308,7 +308,7 @@ class FakeImport(pr.Import):
|
|||||||
self.parent = parent
|
self.parent = parent
|
||||||
|
|
||||||
|
|
||||||
class FakeName(pr.NamePart):
|
class FakeName(pr.Name):
|
||||||
def __init__(self, name_str, parent=None, start_pos=(0, 0)):
|
def __init__(self, name_str, parent=None, start_pos=(0, 0)):
|
||||||
super(FakeName, self).__init__(FakeSubModule, name_str, parent, start_pos)
|
super(FakeName, self).__init__(FakeSubModule, name_str, parent, start_pos)
|
||||||
|
|
||||||
|
|||||||
@@ -199,7 +199,7 @@ class ImportWrapper(pr.Base):
|
|||||||
if star_imports:
|
if star_imports:
|
||||||
scopes = [StarImportModule(scopes[0], star_imports)]
|
scopes = [StarImportModule(scopes[0], star_imports)]
|
||||||
|
|
||||||
# goto only accepts Names or NameParts
|
# goto only accepts `Name`
|
||||||
if is_goto and not rest:
|
if is_goto and not rest:
|
||||||
scopes = [s.name for s in scopes]
|
scopes = [s.name for s in scopes]
|
||||||
|
|
||||||
@@ -332,7 +332,7 @@ class _Importer(object):
|
|||||||
self.file_path = os.path.dirname(path) if path is not None else None
|
self.file_path = os.path.dirname(path) if path is not None else None
|
||||||
|
|
||||||
def str_import_path(self):
|
def str_import_path(self):
|
||||||
"""Returns the import path as pure strings instead of NameParts."""
|
"""Returns the import path as pure strings instead of `Name`."""
|
||||||
return tuple(str(name_part) for name_part in self.import_path)
|
return tuple(str(name_part) for name_part in self.import_path)
|
||||||
|
|
||||||
def get_relative_path(self):
|
def get_relative_path(self):
|
||||||
@@ -374,12 +374,12 @@ class _Importer(object):
|
|||||||
pos = (part._line, part._column)
|
pos = (part._line, part._column)
|
||||||
try:
|
try:
|
||||||
self.import_path = (
|
self.import_path = (
|
||||||
pr.NamePart(FakeSubModule, 'flask_' + str(part), part.parent, pos),
|
pr.Name(FakeSubModule, 'flask_' + str(part), part.parent, pos),
|
||||||
) + orig_path[3:]
|
) + orig_path[3:]
|
||||||
return self._real_follow_file_system()
|
return self._real_follow_file_system()
|
||||||
except ModuleNotFound as e:
|
except ModuleNotFound as e:
|
||||||
self.import_path = (
|
self.import_path = (
|
||||||
pr.NamePart(FakeSubModule, 'flaskext', part.parent, pos),
|
pr.Name(FakeSubModule, 'flaskext', part.parent, pos),
|
||||||
) + orig_path[2:]
|
) + orig_path[2:]
|
||||||
return self._real_follow_file_system()
|
return self._real_follow_file_system()
|
||||||
return self._real_follow_file_system()
|
return self._real_follow_file_system()
|
||||||
|
|||||||
@@ -176,7 +176,7 @@ class Array(use_metaclass(CachedMetaClass, IterableWrapper)):
|
|||||||
key = key_expression_list[0]
|
key = key_expression_list[0]
|
||||||
if isinstance(key, pr.Literal):
|
if isinstance(key, pr.Literal):
|
||||||
key = key.value
|
key = key.value
|
||||||
elif isinstance(key, pr.NamePart):
|
elif isinstance(key, pr.Name):
|
||||||
key = str(key)
|
key = str(key)
|
||||||
else:
|
else:
|
||||||
continue
|
continue
|
||||||
@@ -314,7 +314,7 @@ def _check_array_additions(evaluator, compare_array, module, is_list):
|
|||||||
result = []
|
result = []
|
||||||
for c in calls:
|
for c in calls:
|
||||||
call_path = list(c.generate_call_path())
|
call_path = list(c.generate_call_path())
|
||||||
call_path_simple = [unicode(n) if isinstance(n, pr.NamePart) else n
|
call_path_simple = [unicode(n) if isinstance(n, pr.Name) else n
|
||||||
for n in call_path]
|
for n in call_path]
|
||||||
separate_index = call_path_simple.index(add_name)
|
separate_index = call_path_simple.index(add_name)
|
||||||
if add_name == call_path_simple[-1] or separate_index == 0:
|
if add_name == call_path_simple[-1] or separate_index == 0:
|
||||||
|
|||||||
@@ -316,7 +316,7 @@ def _star_star_dict(evaluator, array, expression_list, func):
|
|||||||
for key_stmt, value_stmt in array.items():
|
for key_stmt, value_stmt in array.items():
|
||||||
# first index, is the key if syntactically correct
|
# first index, is the key if syntactically correct
|
||||||
call = key_stmt.expression_list()[0]
|
call = key_stmt.expression_list()[0]
|
||||||
if isinstance(call, pr.NamePart):
|
if isinstance(call, pr.Name):
|
||||||
key = call
|
key = call
|
||||||
elif isinstance(call, pr.Call):
|
elif isinstance(call, pr.Call):
|
||||||
key = call.name
|
key = call.name
|
||||||
|
|||||||
@@ -245,12 +245,12 @@ def get_instance_el(evaluator, instance, var, is_class_var=False):
|
|||||||
untouched.
|
untouched.
|
||||||
"""
|
"""
|
||||||
if isinstance(var, (Instance, compiled.CompiledObject, pr.Operator, Token,
|
if isinstance(var, (Instance, compiled.CompiledObject, pr.Operator, Token,
|
||||||
pr.Module, FunctionExecution, pr.NamePart)):
|
pr.Module, FunctionExecution, pr.Name)):
|
||||||
if isinstance(var, pr.NamePart):
|
if isinstance(var, pr.Name):
|
||||||
# TODO temp solution, remove later, NameParts should never get
|
# TODO temp solution, remove later, Name should never get
|
||||||
# here?
|
# here?
|
||||||
par = get_instance_el(evaluator, instance, var.parent, is_class_var)
|
par = get_instance_el(evaluator, instance, var.parent, is_class_var)
|
||||||
return pr.NamePart(var._sub_module, unicode(var), par, var.start_pos)
|
return pr.Name(var._sub_module, unicode(var), par, var.start_pos)
|
||||||
return var
|
return var
|
||||||
|
|
||||||
var = wrap(evaluator, var)
|
var = wrap(evaluator, var)
|
||||||
@@ -282,8 +282,8 @@ class InstanceElement(use_metaclass(CachedMetaClass, pr.Base)):
|
|||||||
return par
|
return par
|
||||||
|
|
||||||
def get_parent_until(self, *args, **kwargs):
|
def get_parent_until(self, *args, **kwargs):
|
||||||
if isinstance(self.var, pr.NamePart):
|
if isinstance(self.var, pr.Name):
|
||||||
# TODO NameParts should never even be InstanceElements
|
# TODO Name should never even be InstanceElements
|
||||||
return pr.Simple.get_parent_until(self.parent, *args, **kwargs)
|
return pr.Simple.get_parent_until(self.parent, *args, **kwargs)
|
||||||
return pr.Simple.get_parent_until(self, *args, **kwargs)
|
return pr.Simple.get_parent_until(self, *args, **kwargs)
|
||||||
|
|
||||||
@@ -694,7 +694,7 @@ class ModuleWrapper(use_metaclass(CachedMetaClass, pr.Module, Wrapper)):
|
|||||||
@property
|
@property
|
||||||
@memoize_default()
|
@memoize_default()
|
||||||
def name(self):
|
def name(self):
|
||||||
return pr.NamePart(self, unicode(self.base.name), self, (1, 0))
|
return pr.Name(self, unicode(self.base.name), self, (1, 0))
|
||||||
|
|
||||||
@memoize_default()
|
@memoize_default()
|
||||||
def _sub_modules(self):
|
def _sub_modules(self):
|
||||||
|
|||||||
@@ -113,7 +113,7 @@ class Parser(object):
|
|||||||
:return: tuple of Name, next_token
|
:return: tuple of Name, next_token
|
||||||
"""
|
"""
|
||||||
def append(tok):
|
def append(tok):
|
||||||
names.append(pr.NamePart(self.module, tok.string, None, tok.start_pos))
|
names.append(pr.Name(self.module, tok.string, None, tok.start_pos))
|
||||||
self.module.temp_used_names.append(tok.string)
|
self.module.temp_used_names.append(tok.string)
|
||||||
|
|
||||||
names = []
|
names = []
|
||||||
@@ -139,7 +139,7 @@ class Parser(object):
|
|||||||
self.module.temp_used_names.append(tok.string)
|
self.module.temp_used_names.append(tok.string)
|
||||||
if tok.type != tokenize.NAME:
|
if tok.type != tokenize.NAME:
|
||||||
return None, tok
|
return None, tok
|
||||||
return pr.NamePart(self.module, tok.string, None, tok.start_pos), next(self._gen)
|
return pr.Name(self.module, tok.string, None, tok.start_pos), next(self._gen)
|
||||||
|
|
||||||
def _parse_import_list(self):
|
def _parse_import_list(self):
|
||||||
"""
|
"""
|
||||||
@@ -595,7 +595,7 @@ class Parser(object):
|
|||||||
self._scope.add_statement(kw)
|
self._scope.add_statement(kw)
|
||||||
if stmt is not None and tok_str == 'global':
|
if stmt is not None and tok_str == 'global':
|
||||||
for t in stmt._token_list:
|
for t in stmt._token_list:
|
||||||
if isinstance(t, pr.NamePart):
|
if isinstance(t, pr.Name):
|
||||||
# Add the global to the top module, it counts there.
|
# Add the global to the top module, it counts there.
|
||||||
self.module.add_global(t)
|
self.module.add_global(t)
|
||||||
# decorator
|
# decorator
|
||||||
|
|||||||
@@ -485,7 +485,7 @@ class SubModule(Scope, Module):
|
|||||||
string = re.sub('\.[a-z]+-\d{2}[mud]{0,3}$', '', r.group(1))
|
string = re.sub('\.[a-z]+-\d{2}[mud]{0,3}$', '', r.group(1))
|
||||||
# Positions are not real, but a module starts at (1, 0)
|
# Positions are not real, but a module starts at (1, 0)
|
||||||
p = (1, 0)
|
p = (1, 0)
|
||||||
return NamePart(self, string, self.use_as_parent, p)
|
return Name(self, string, self.use_as_parent, p)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def has_explicit_absolute_import(self):
|
def has_explicit_absolute_import(self):
|
||||||
@@ -1240,7 +1240,7 @@ class Statement(Simple, DocstringMixin):
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
is_literal = token_type in (tokenize.STRING, tokenize.NUMBER)
|
is_literal = token_type in (tokenize.STRING, tokenize.NUMBER)
|
||||||
if is_literal or isinstance(tok, NamePart):
|
if is_literal or isinstance(tok, Name):
|
||||||
cls = Literal if is_literal else Call
|
cls = Literal if is_literal else Call
|
||||||
|
|
||||||
call = cls(self._sub_module, tok_str, tok.start_pos, tok.end_pos, self)
|
call = cls(self._sub_module, tok_str, tok.start_pos, tok.end_pos, self)
|
||||||
@@ -1522,7 +1522,7 @@ class Array(StatementElement):
|
|||||||
return "<%s: %s%s>" % (type(self).__name__, typ, self.values)
|
return "<%s: %s%s>" % (type(self).__name__, typ, self.values)
|
||||||
|
|
||||||
|
|
||||||
class NamePart(object):
|
class Name(object):
|
||||||
"""
|
"""
|
||||||
A string. Sometimes it is important to know if the string belongs to a name
|
A string. Sometimes it is important to know if the string belongs to a name
|
||||||
or not.
|
or not.
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ class TestCallAndName():
|
|||||||
def test_call_type(self):
|
def test_call_type(self):
|
||||||
call = self.get_call('hello')
|
call = self.get_call('hello')
|
||||||
assert isinstance(call, pr.Call)
|
assert isinstance(call, pr.Call)
|
||||||
assert type(call.name) == pr.NamePart
|
assert type(call.name) == pr.Name
|
||||||
|
|
||||||
def test_literal_type(self):
|
def test_literal_type(self):
|
||||||
literal = self.get_call('1.0')
|
literal = self.get_call('1.0')
|
||||||
|
|||||||
Reference in New Issue
Block a user