From 989e12e8a7546c9cd2c03f856502a546c2c3a942 Mon Sep 17 00:00:00 2001 From: Jean-Louis Fuchs Date: Thu, 5 Dec 2013 23:34:20 +0100 Subject: [PATCH] * Replaced token tuple with token class * Fixed PEP8 where I read code --- jedi/cache.py | 2 +- jedi/common.py | 9 +++++++-- jedi/parser/__init__.py | 32 ++++++++++++++++++++++---------- test/test_parsing.py | 5 ++++- 4 files changed, 34 insertions(+), 14 deletions(-) diff --git a/jedi/cache.py b/jedi/cache.py index 69d5e972..ef9a94b9 100644 --- a/jedi/cache.py +++ b/jedi/cache.py @@ -220,7 +220,7 @@ def save_module(path, name, parser, pickling=True): class _ModulePickling(object): - version = 4 + version = 5 """ Version number (integer) for file system cache. diff --git a/jedi/common.py b/jedi/common.py index a7a4d8b5..929c8497 100644 --- a/jedi/common.py +++ b/jedi/common.py @@ -146,8 +146,13 @@ class NoErrorTokenizer(object): if self.is_fast_parser \ and self.previous[0] in (tokenize.INDENT, tokenize.NL, None, tokenize.NEWLINE, tokenize.DEDENT) \ - and c[0] not in (tokenize.COMMENT, tokenize.INDENT, - tokenize.NL, tokenize.NEWLINE, tokenize.DEDENT): + and c[0] not in ( + tokenize.COMMENT, + tokenize.INDENT, + tokenize.NL, + tokenize.NEWLINE, + tokenize.DEDENT + ): # print c, tokenize.tok_name[c[0]] tok = c[1] diff --git a/jedi/parser/__init__.py b/jedi/parser/__init__.py index 8a656e28..96256186 100644 --- a/jedi/parser/__init__.py +++ b/jedi/parser/__init__.py @@ -24,6 +24,7 @@ from jedi._compatibility import next, StringIO from jedi import debug from jedi import common from jedi.parser import representation as pr +from jedi.parser import token as token_pr class Parser(object): @@ -271,8 +272,11 @@ class Parser(object): first_pos = self.start_pos token_type, cname = self.next() if token_type != tokenize.NAME: - debug.warning("class: syntax err, token is not a name@%s (%s: %s)" - % (self.start_pos[0], tokenize.tok_name[token_type], cname)) + debug.warning( + "class: syntax err, token is not a name@%s (%s: %s)" % ( + self.start_pos[0], tokenize.tok_name[token_type], cname + ) + ) return None cname = pr.Name(self.module, [(cname, self.start_pos)], self.start_pos, @@ -345,11 +349,17 @@ class Parser(object): or tok in breaks and level <= 0): try: # print 'parse_stmt', tok, tokenize.tok_name[token_type] - tok_list.append(self._current + (self.start_pos,)) + tok_list.append( + token_pr.Token.from_tuple( + self._current + (self.start_pos,) + ) + ) if tok == 'as': token_type, tok = self.next() if token_type == tokenize.NAME: - n, token_type, tok = self._parse_dot_name(self._current) + n, token_type, tok = self._parse_dot_name( + self._current + ) if n: set_vars.append(n) as_names.append(n) @@ -396,7 +406,6 @@ class Parser(object): self._scope.statements[-1].add_docstr(first_tok[1]) return None, tok - stmt = stmt_class(self.module, tok_list, first_pos, self.end_pos, as_names=as_names, names_are_set_vars=names_are_set_vars) @@ -435,9 +444,11 @@ class Parser(object): s = s.parent raise - if self.user_position and (self.start_pos[0] == self.user_position[0] - or self.user_scope is None - and self.start_pos[0] >= self.user_position[0]): + if self.user_position and ( + self.start_pos[0] == self.user_position[0] + or self.user_scope is None + and self.start_pos[0] >= self.user_position[0] + ): debug.dbg('user scope found [%s] = %s' % (self.parserline.replace('\n', ''), repr(self._scope))) self.user_scope = self._scope @@ -489,8 +500,9 @@ class Parser(object): and not isinstance(self._scope, pr.SubModule): self._scope = self.module - use_as_parent_scope = self.top_module if isinstance(self._scope, - pr.SubModule) else self._scope + use_as_parent_scope = self.top_module if isinstance( + self._scope, pr.SubModule + ) else self._scope first_pos = self.start_pos if tok == 'def': func = self._parse_function() diff --git a/test/test_parsing.py b/test/test_parsing.py index 2894d5f1..07336afe 100644 --- a/test/test_parsing.py +++ b/test/test_parsing.py @@ -1,6 +1,7 @@ from jedi.parser import Parser from jedi.parser import representation as pr + def test_user_statement_on_import(): """github #285""" s = "from datetime import (\n" \ @@ -9,7 +10,7 @@ def test_user_statement_on_import(): for pos in [(2, 1), (2, 4)]: u = Parser(s, user_position=pos).user_stmt assert isinstance(u, pr.Import) - assert u.defunct == False + assert u.defunct is False assert [str(n) for n in u.get_defined_names()] == ['time'] @@ -47,6 +48,7 @@ class TestCallAndName(): assert isinstance(literal, pr.String) assert literal.value == 'hello' + class TestSubscopes(): def get_sub(self, source): return Parser(source).module.subscopes[0] @@ -62,6 +64,7 @@ class TestSubscopes(): assert name.end_pos == (1, len('def foo')) assert str(name) == 'foo' + class TestImports(): def get_import(self, source): return Parser(source).module.imports[0]