Fix the fstring syntax if there's a conversion with exclamation mark

This commit is contained in:
Dave Halter
2018-04-06 09:59:15 +02:00
parent 9d2ce4bcd4
commit 0d96b12566
2 changed files with 13 additions and 3 deletions

View File

@@ -864,7 +864,7 @@ class _FStringRule(SyntaxRule):
cls._fstring_grammar = parso.load_grammar(language='python-f-string')
return cls._fstring_grammar
def _check_type(self, fstring_string):
def _check_string_part(self, fstring_string):
index = -1
value = fstring_string.value
while True:
@@ -877,10 +877,20 @@ class _FStringRule(SyntaxRule):
else:
self.add_issue(fstring_string, message=self.message_single_closing)
def _check_fstring_expr(self, fstring_expr):
conversion = fstring_expr.children[2]
if conversion.type == 'fstring_conversion':
name = conversion.children[1]
if name.value not in ('s', 'r', 'a'):
self.add_issue(name, message=self.message_conversion)
def is_issue(self, fstring):
for fstring_content in fstring.children[1:-1]:
if fstring_content.type == 'fstring_string':
self._check_type(fstring_content)
self._check_string_part(fstring_content)
else:
assert fstring_content.type == 'fstring_expr'
self._check_fstring_expr(fstring_content)
return
print(fstring)
if 'f' not in fstring.string_prefix.lower():

View File

@@ -168,7 +168,7 @@ def _create_token_collection(version_info):
# recognized as two instances of =).
Operator = group(r"\*\*=?", r">>=?", r"<<=?",
r"//=?", r"->",
r"[+\-*/%&@`|^=<>]=?",
r"[+\-*/%&@`|^!=<>]=?",
r"~")
Bracket = '[][(){}]'