Some preparations to get Python 2 tests working.

This commit is contained in:
Dave Halter
2017-07-31 12:33:47 +02:00
parent 5fbbb225dd
commit 58c32591d0
8 changed files with 25 additions and 24 deletions

View File

@@ -2,7 +2,8 @@ from contextlib import contextmanager
class Normalizer(object):
def __init__(self, config):
def __init__(self, grammar, config):
self._grammar = grammar
self._config = config
self.issues = []
@@ -44,11 +45,11 @@ class Normalizer(object):
class NormalizerConfig(object):
normalizer_class = Normalizer
def create_normalizer(self):
def create_normalizer(self, grammar):
if self.normalizer_class is None:
return None
return self.normalizer_class(self)
return self.normalizer_class(grammar, self)
@classmethod
def register_rule(cls, rule):

View File

@@ -224,6 +224,7 @@ class ErrorFinder(Normalizer):
def __init__(self, *args, **kwargs):
super(ErrorFinder, self).__init__(*args, **kwargs)
self._error_dict = {}
self._version = self._grammar._version_int
def initialize(self, node):
from parso.python.tree import search_ancestor
@@ -513,6 +514,9 @@ class ErrorFinder(Normalizer):
self._add_syntax_error('invalid syntax', leaf)
elif leaf.type == 'name':
if leaf.value == '__debug__' and leaf.is_definition():
if self._version <= (2, 7):
message = 'assignment to __debug__'
else:
message = 'assignment to keyword'
self._add_syntax_error(message, leaf)
@@ -657,6 +661,3 @@ class ErrorFinder(Normalizer):
class ErrorFinderConfig(NormalizerConfig):
normalizer_class = ErrorFinder
def __init__(self, grammar):
self.grammar = grammar

View File

@@ -149,8 +149,8 @@ def _is_magic_name(name):
class PEP8Normalizer(ErrorFinder):
def __init__(self, config):
super(PEP8Normalizer, self).__init__(config)
def __init__(self, *args, **kwargs):
super(PEP8Normalizer, self).__init__(*args, **kwargs)
self._previous_part = None
self._previous_leaf = None
self._on_newline = True
@@ -161,10 +161,10 @@ class PEP8Normalizer(ErrorFinder):
self._implicit_indentation_possible = False
# The top of stack of the indentation nodes.
self._indentation_tos = self._last_indentation_tos = \
IndentationNode(config, indentation='')
IndentationNode(self._config, indentation='')
self._in_suite_introducer = False
if ' ' in config.indentation:
if ' ' in self._config.indentation:
self._indentation_type = 'spaces'
self._wrong_indentation_char = '\t'
else:

View File

@@ -100,8 +100,8 @@ class PythonMixin(object):
return None
def _iter_errors(self, grammar):
config = normalizer.ErrorFinderConfig(grammar)
return self._get_normalizer_issues(config)
config = normalizer.ErrorFinderConfig()
return self._get_normalizer_issues(grammar, config)
class PythonLeaf(PythonMixin, Leaf):

View File

@@ -154,24 +154,24 @@ class NodeOrLeaf(object):
e.g. a statement.
"""
def _get_normalizer(self, normalizer_config):
def _get_normalizer(self, normalizer_config, grammar):
if normalizer_config is None:
normalizer_config = self.default_normalizer_config
if normalizer_config is None:
raise ValueError("You need to specify a normalizer, because "
"there's no default normalizer for this tree.")
return normalizer_config.create_normalizer()
return normalizer_config.create_normalizer(grammar)
def _normalize(self, normalizer_config=None):
def _normalize(self, grammar, normalizer_config=None):
"""
TODO this is not public, yet.
The returned code will be normalized, e.g. PEP8 for Python.
"""
normalizer = self._get_normalizer(normalizer_config)
normalizer = self._get_normalizer(normalizer_config, grammar)
return normalizer.walk(self)
def _get_normalizer_issues(self, normalizer_config=None):
normalizer = self._get_normalizer(normalizer_config)
def _get_normalizer_issues(self, grammar, normalizer_config=None):
normalizer = self._get_normalizer(normalizer_config, grammar)
normalizer.walk(self)
return normalizer.issues

View File

@@ -60,7 +60,7 @@ def test_normalizer_issue(normalizer_issue_case):
grammar = parso.load_grammar(version=normalizer_issue_case.python_version)
module = grammar.parse(code)
issues = module._get_normalizer_issues()
issues = module._get_normalizer_issues(grammar)
actual = sort(issues)
diff = '\n'.join(difflib.ndiff(desired, actual))

View File

@@ -2,8 +2,9 @@ import parso
def issues(code):
grammar = parso.load_grammar()
module = parso.parse(code)
return module._get_normalizer_issues()
return module._get_normalizer_issues(grammar)
def test_eof_newline():

View File

@@ -9,7 +9,6 @@ from textwrap import dedent
import pytest
import parso
from parso.python.normalizer import ErrorFinderConfig
FAILING_EXAMPLES = [
@@ -221,8 +220,7 @@ if sys.version_info >= (3, 4):
def _get_error_list(code, version=None):
grammar = parso.load_grammar(version=version)
tree = grammar.parse(code)
config = ErrorFinderConfig(grammar=grammar)
return list(tree._get_normalizer_issues(config))
return list(tree._iter_errors(grammar))
def assert_comparison(code, error_code, positions):