From 03e01631ccd2ce17d78d50b3dde98156a592eb64 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Fri, 26 Sep 2014 16:29:53 +0200 Subject: [PATCH] Remove NamePart from existance and rename it to Name. --- jedi/api/keywords.py | 1 - jedi/api/usages.py | 4 ++-- jedi/cache.py | 2 +- jedi/evaluate/__init__.py | 6 +++--- jedi/evaluate/dynamic.py | 2 +- jedi/evaluate/finder.py | 10 +++++----- jedi/evaluate/helpers.py | 6 +++--- jedi/evaluate/imports.py | 8 ++++---- jedi/evaluate/iterable.py | 4 ++-- jedi/evaluate/param.py | 2 +- jedi/evaluate/representation.py | 14 +++++++------- jedi/parser/__init__.py | 6 +++--- jedi/parser/representation.py | 6 +++--- test/test_parser/test_parser.py | 2 +- 14 files changed, 36 insertions(+), 37 deletions(-) diff --git a/jedi/api/keywords.py b/jedi/api/keywords.py index e5f63836..2a54ba2d 100644 --- a/jedi/api/keywords.py +++ b/jedi/api/keywords.py @@ -1,7 +1,6 @@ import pydoc import keyword -from jedi.parser.representation import NamePart from jedi._compatibility import is_py3 from jedi import common from jedi.evaluate import compiled diff --git a/jedi/api/usages.py b/jedi/api/usages.py index deff43dc..ec0a6c0b 100644 --- a/jedi/api/usages.py +++ b/jedi/api/usages.py @@ -8,7 +8,7 @@ from jedi.evaluate import helpers def usages(evaluator, definitions, mods): """ - :param definitions: list of NameParts + :param definitions: list of Name """ def compare_array(definitions): """ `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 call_path = list(call.generate_call_path()) for i, name in enumerate(call_path): - # name is `pr.NamePart`. + # name is `pr.Name`. if u(name) == search_name: follow.append(call_path[:i + 1]) diff --git a/jedi/cache.py b/jedi/cache.py index dae5c085..088f7cdd 100644 --- a/jedi/cache.py +++ b/jedi/cache.py @@ -243,7 +243,7 @@ def save_parser(path, name, parser, pickling=True): class ParserPickling(object): - version = 17 + version = 18 """ Version number (integer) for file system cache. diff --git a/jedi/evaluate/__init__.py b/jedi/evaluate/__init__.py index 7c6366bf..10315b6e 100644 --- a/jedi/evaluate/__init__.py +++ b/jedi/evaluate/__init__.py @@ -219,7 +219,7 @@ class Evaluator(object): else: types = [iterable.Array(self, current)] else: - if isinstance(current, pr.NamePart): + if isinstance(current, pr.Name): # This is the first global lookup. types = self.find_types(scope, current, position=position, search_global=True) @@ -336,7 +336,7 @@ class Evaluator(object): # statement name definitions. Only return, if it's one name and one # 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. - 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(): # Named params should get resolved to their param definitions. 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): """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)\ and var_name.startswith('__') and not var_name.endswith('__'): s = call_scope.get_parent_until((pr.Class, er.Instance, compiled.CompiledObject)) diff --git a/jedi/evaluate/dynamic.py b/jedi/evaluate/dynamic.py index 81f5ffb6..52a4d3f3 100644 --- a/jedi/evaluate/dynamic.py +++ b/jedi/evaluate/dynamic.py @@ -87,7 +87,7 @@ def search_params(evaluator, param): # Need to take right index, because there could be a # 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] i = listRightIndex(call_path_simple, func_name) before, after = call_path[:i], call_path[i + 1:] diff --git a/jedi/evaluate/finder.py b/jedi/evaluate/finder.py index 563d0c87..54b74302 100644 --- a/jedi/evaluate/finder.py +++ b/jedi/evaluate/finder.py @@ -42,7 +42,7 @@ class NameFinder(object): types = self._names_to_types(names, resolve_decorator) 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)): if not isinstance(self.name_str, (str, unicode)): # TODO Remove? if search_global: @@ -148,7 +148,7 @@ class NameFinder(object): def _check_getattr(self, inst): """Checks for both __getattr__ and __getattribute__ methods""" 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)) with common.ignored(KeyError): result = inst.execute_subscope_by_name('__getattr__', [name]) @@ -219,7 +219,7 @@ class NameFinder(object): evaluator = self._evaluator # 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 # Ignore FunctionExecution parents for now. 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 >>> pairs = list(get_names_of_scope(Evaluator(), scope)) >>> pairs[0] - (, []) + (, []) Then it yield the names from one level outer scope. For this example, this is the most outer scope. >>> pairs[1] - (>, [, ]) + (>, [, ]) After that we have a few underscore names that have been defined diff --git a/jedi/evaluate/helpers.py b/jedi/evaluate/helpers.py index bebd72f4..f8f6b148 100644 --- a/jedi/evaluate/helpers.py +++ b/jedi/evaluate/helpers.py @@ -15,7 +15,7 @@ def deep_ast_copy(obj, new_elements_default=None): return key_value[0] not in ('_expression_list', '_assignment_details') new_elements = new_elements_default or {} - accept = (pr.Simple, pr.NamePart, pr.KeywordStatement) + accept = (pr.Simple, pr.Name, pr.KeywordStatement) def recursion(obj): # 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 # all the name_parts. for tok in stmt_or_import._token_list: - if isinstance(tok, pr.NamePart): + if isinstance(tok, pr.Name): name_parts.add(tok) return name_parts @@ -308,7 +308,7 @@ class FakeImport(pr.Import): self.parent = parent -class FakeName(pr.NamePart): +class FakeName(pr.Name): def __init__(self, name_str, parent=None, start_pos=(0, 0)): super(FakeName, self).__init__(FakeSubModule, name_str, parent, start_pos) diff --git a/jedi/evaluate/imports.py b/jedi/evaluate/imports.py index c508f637..04f11ac5 100644 --- a/jedi/evaluate/imports.py +++ b/jedi/evaluate/imports.py @@ -199,7 +199,7 @@ class ImportWrapper(pr.Base): if star_imports: scopes = [StarImportModule(scopes[0], star_imports)] - # goto only accepts Names or NameParts + # goto only accepts `Name` if is_goto and not rest: 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 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) def get_relative_path(self): @@ -374,12 +374,12 @@ class _Importer(object): pos = (part._line, part._column) try: self.import_path = ( - pr.NamePart(FakeSubModule, 'flask_' + str(part), part.parent, pos), + pr.Name(FakeSubModule, 'flask_' + str(part), part.parent, pos), ) + orig_path[3:] return self._real_follow_file_system() except ModuleNotFound as e: self.import_path = ( - pr.NamePart(FakeSubModule, 'flaskext', part.parent, pos), + pr.Name(FakeSubModule, 'flaskext', part.parent, pos), ) + orig_path[2:] return self._real_follow_file_system() return self._real_follow_file_system() diff --git a/jedi/evaluate/iterable.py b/jedi/evaluate/iterable.py index de18bf7f..d576d848 100644 --- a/jedi/evaluate/iterable.py +++ b/jedi/evaluate/iterable.py @@ -176,7 +176,7 @@ class Array(use_metaclass(CachedMetaClass, IterableWrapper)): key = key_expression_list[0] if isinstance(key, pr.Literal): key = key.value - elif isinstance(key, pr.NamePart): + elif isinstance(key, pr.Name): key = str(key) else: continue @@ -314,7 +314,7 @@ def _check_array_additions(evaluator, compare_array, module, is_list): result = [] for c in calls: 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] separate_index = call_path_simple.index(add_name) if add_name == call_path_simple[-1] or separate_index == 0: diff --git a/jedi/evaluate/param.py b/jedi/evaluate/param.py index c89cb464..3ac8ad9f 100644 --- a/jedi/evaluate/param.py +++ b/jedi/evaluate/param.py @@ -316,7 +316,7 @@ def _star_star_dict(evaluator, array, expression_list, func): for key_stmt, value_stmt in array.items(): # first index, is the key if syntactically correct call = key_stmt.expression_list()[0] - if isinstance(call, pr.NamePart): + if isinstance(call, pr.Name): key = call elif isinstance(call, pr.Call): key = call.name diff --git a/jedi/evaluate/representation.py b/jedi/evaluate/representation.py index f04ff8b6..f5f28d99 100644 --- a/jedi/evaluate/representation.py +++ b/jedi/evaluate/representation.py @@ -245,12 +245,12 @@ def get_instance_el(evaluator, instance, var, is_class_var=False): untouched. """ if isinstance(var, (Instance, compiled.CompiledObject, pr.Operator, Token, - pr.Module, FunctionExecution, pr.NamePart)): - if isinstance(var, pr.NamePart): - # TODO temp solution, remove later, NameParts should never get + pr.Module, FunctionExecution, pr.Name)): + if isinstance(var, pr.Name): + # TODO temp solution, remove later, Name should never get # here? 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 var = wrap(evaluator, var) @@ -282,8 +282,8 @@ class InstanceElement(use_metaclass(CachedMetaClass, pr.Base)): return par def get_parent_until(self, *args, **kwargs): - if isinstance(self.var, pr.NamePart): - # TODO NameParts should never even be InstanceElements + if isinstance(self.var, pr.Name): + # TODO Name should never even be InstanceElements return pr.Simple.get_parent_until(self.parent, *args, **kwargs) return pr.Simple.get_parent_until(self, *args, **kwargs) @@ -694,7 +694,7 @@ class ModuleWrapper(use_metaclass(CachedMetaClass, pr.Module, Wrapper)): @property @memoize_default() 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() def _sub_modules(self): diff --git a/jedi/parser/__init__.py b/jedi/parser/__init__.py index a54d6ef2..1ef45aff 100644 --- a/jedi/parser/__init__.py +++ b/jedi/parser/__init__.py @@ -113,7 +113,7 @@ class Parser(object): :return: tuple of Name, next_token """ 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) names = [] @@ -139,7 +139,7 @@ class Parser(object): self.module.temp_used_names.append(tok.string) if tok.type != tokenize.NAME: 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): """ @@ -595,7 +595,7 @@ class Parser(object): self._scope.add_statement(kw) if stmt is not None and tok_str == 'global': 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. self.module.add_global(t) # decorator diff --git a/jedi/parser/representation.py b/jedi/parser/representation.py index d928a9d7..f1719703 100644 --- a/jedi/parser/representation.py +++ b/jedi/parser/representation.py @@ -485,7 +485,7 @@ class SubModule(Scope, Module): string = re.sub('\.[a-z]+-\d{2}[mud]{0,3}$', '', r.group(1)) # Positions are not real, but a module starts at (1, 0) p = (1, 0) - return NamePart(self, string, self.use_as_parent, p) + return Name(self, string, self.use_as_parent, p) @property def has_explicit_absolute_import(self): @@ -1240,7 +1240,7 @@ class Statement(Simple, DocstringMixin): continue 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 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) -class NamePart(object): +class Name(object): """ A string. Sometimes it is important to know if the string belongs to a name or not. diff --git a/test/test_parser/test_parser.py b/test/test_parser/test_parser.py index ca509434..5916fe6e 100644 --- a/test/test_parser/test_parser.py +++ b/test/test_parser/test_parser.py @@ -38,7 +38,7 @@ class TestCallAndName(): def test_call_type(self): call = self.get_call('hello') assert isinstance(call, pr.Call) - assert type(call.name) == pr.NamePart + assert type(call.name) == pr.Name def test_literal_type(self): literal = self.get_call('1.0')