Skip to content

Commit

Permalink
Fix unit tests in Python 3.10 (Closes: #192)
Browse files Browse the repository at this point in the history
  • Loading branch information
jspricke committed Dec 4, 2022
1 parent 6b6b59f commit 18c0f04
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 10 deletions.
53 changes: 43 additions & 10 deletions parso/python/errors.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
import codecs
import sys
import warnings
import re
from contextlib import contextmanager
Expand Down Expand Up @@ -33,7 +34,10 @@ def _get_rhs_name(node, version):
return "literal"
else:
if second.children[1] == ":" or second.children[0] == "**":
return "dict display"
if version < (3, 10):
return "dict display"
else:
return "dict literal"
else:
return "set display"
elif (
Expand All @@ -47,7 +51,10 @@ def _get_rhs_name(node, version):
elif first == "[":
return "list"
elif first == "{" and second == "}":
return "dict display"
if version < (3, 10):
return "dict display"
else:
return "dict literal"
elif first == "{" and len(node.children) > 2:
return "set display"
elif type_ == "keyword":
Expand All @@ -58,7 +65,10 @@ def _get_rhs_name(node, version):
else:
return str(node.value)
elif type_ == "operator" and node.value == "...":
return "Ellipsis"
if version < (3, 10):
return "Ellipsis"
else:
return "ellipsis"
elif type_ == "comparison":
return "comparison"
elif type_ in ("string", "number", "strings"):
Expand All @@ -83,7 +93,10 @@ def _get_rhs_name(node, version):
or "_test" in type_
or type_ in ("term", "factor")
):
return "operator"
if version < (3, 10):
return "operator"
else:
return "expression"
elif type_ == "star_expr":
return "starred"
elif type_ == "testlist_star_expr":
Expand Down Expand Up @@ -610,9 +623,14 @@ def is_issue(self, leaf):

@ErrorFinder.register_rule(type='string')
class _StringChecks(SyntaxRule):
message = "bytes can only contain ASCII literal characters."
if sys.version_info < (3, 10):
message = "bytes can only contain ASCII literal characters."
else:
message = "bytes can only contain ASCII literal characters"

def is_issue(self, leaf):
if self._normalizer.version >= (3, 10):
message = "bytes can only contain ASCII literal characters"
string_prefix = leaf.string_prefix.lower()
if 'b' in string_prefix \
and any(c for c in leaf.value if ord(c) > 127):
Expand Down Expand Up @@ -1043,14 +1061,20 @@ def _check_assignment(self, node, is_deletion=False, is_namedexpr=False, is_aug_
error = 'literal'
else:
if second.children[1] == ':':
error = 'dict display'
if self._normalizer.version < (3, 10):
error = 'dict display'
else:
error = 'dict literal'
else:
error = 'set display'
elif first == "{" and second == "}":
if self._normalizer.version < (3, 8):
error = 'literal'
else:
error = "dict display"
if self._normalizer.version < (3, 10):
error = "dict display"
else:
error = "dict literal"
elif first == "{" and len(node.children) > 2:
if self._normalizer.version < (3, 8):
error = 'literal'
Expand Down Expand Up @@ -1083,7 +1107,10 @@ def _check_assignment(self, node, is_deletion=False, is_namedexpr=False, is_aug_
error = str(node.value)
elif type_ == 'operator':
if node.value == '...':
error = 'Ellipsis'
if self._normalizer.version < (3, 10):
error = 'Ellipsis'
else:
error = 'ellipsis'
elif type_ == 'comparison':
error = 'comparison'
elif type_ in ('string', 'number', 'strings'):
Expand All @@ -1098,7 +1125,10 @@ def _check_assignment(self, node, is_deletion=False, is_namedexpr=False, is_aug_
if node.children[0] == 'await':
error = 'await expression'
elif node.children[-2] == '**':
error = 'operator'
if self._normalizer.version < (3, 10):
error = 'operator'
else:
error = 'expression'
else:
# Has a trailer
trailer = node.children[-1]
Expand All @@ -1120,7 +1150,10 @@ def _check_assignment(self, node, is_deletion=False, is_namedexpr=False, is_aug_
elif ('expr' in type_ and type_ != 'star_expr' # is a substring
or '_test' in type_
or type_ in ('term', 'factor')):
error = 'operator'
if self._normalizer.version < (3, 10):
error = 'operator'
else:
error = 'expression'
elif type_ == "star_expr":
if is_deletion:
if self._normalizer.version >= (3, 9):
Expand Down
15 changes: 15 additions & 0 deletions test/test_python_errors.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Testing if parso finds syntax errors and indentation errors.
"""
import re
import sys
import warnings

Expand Down Expand Up @@ -136,6 +137,20 @@ def _get_actual_exception(code):
wanted = 'SyntaxError: invalid syntax'
elif wanted == "SyntaxError: f-string: single '}' is not allowed":
wanted = 'SyntaxError: invalid syntax'
elif "Maybe you meant '==' instead of '='?" in wanted:
wanted = wanted.removesuffix(" here. Maybe you meant '==' instead of '='?")
elif re.match(r'SyntaxError: unterminated string literal \(detected at line \d+\)', wanted):
wanted = 'SyntaxError: EOL while scanning string literal'
elif re.match(r'SyntaxError: unterminated triple-quoted string literal \(detected at line \d+\)', wanted):
wanted = 'SyntaxError: EOF while scanning triple-quoted string literal'
elif wanted == 'SyntaxError: cannot use starred expression here':
wanted = "SyntaxError: can't use starred expression here"
elif wanted == 'SyntaxError: f-string: cannot use starred expression here':
wanted = "SyntaxError: f-string: can't use starred expression here"
elif re.match(r"IndentationError: expected an indented block after '[^']*' statement on line \d", wanted):
wanted = 'IndentationError: expected an indented block'
elif wanted == 'SyntaxError: unterminated string literal':
wanted = 'SyntaxError: EOL while scanning string literal'
return [wanted], line_nr


Expand Down

0 comments on commit 18c0f04

Please sign in to comment.