Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
hauntsaninja committed Nov 29, 2021
1 parent 8694740 commit 9aa1af4
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/black/trans.py
Expand Up @@ -947,7 +947,7 @@ def iter_fexpr_spans(s: str) -> Iterator[Tuple[int, int]]:
Yields spans corresponding to expressions in a given f-string.
Assumes the input string is a valid f-string.
"""
stack = [] # our curly paren stack
stack: List[int] = [] # our curly paren stack
i = 0
while i < len(s):
if s[i] == "{":
Expand All @@ -973,17 +973,21 @@ def iter_fexpr_spans(s: str) -> Iterator[Tuple[int, int]]:
# if we're in an expression part of the f-string, fast forward through strings
if stack:
delim = None
if s[i: i + 3] in ("'''", '"""'):
delim = s[i: i + 3]
if s[i : i + 3] in ("'''", '"""'):
delim = s[i : i + 3]
elif s[i] in ("'", '"'):
delim = s[i]
if delim:
i += len(delim)
while i < len(s) and s[i:i + len(delim)] != delim:
while i < len(s) and s[i : i + len(delim)] != delim:
i += 1
i += 1


def fstring_contains_expr(s: str) -> bool:
return any(True for _ in iter_fexpr_spans(s))


class StringSplitter(BaseStringSplitter, CustomSplitMapMixin):
"""
StringTransformer that splits "atom" strings (i.e. strings which exist on
Expand Down Expand Up @@ -1089,9 +1093,8 @@ def do_transform(self, line: Line, string_idx: int) -> Iterator[TResult[Line]]:
# contain any f-expressions, but ONLY if the original f-string
# contains at least one f-expression. Otherwise, we will alter the AST
# of the program.
drop_pointless_f_prefix = (
("f" in prefix)
and any(True for _ in iter_fexpr_spans(LL[string_idx].value))
drop_pointless_f_prefix = ("f" in prefix) and fstring_contains_expr(
LL[string_idx].value
)

first_string_line = True
Expand Down Expand Up @@ -1447,7 +1450,7 @@ def _normalize_f_string(self, string: str, prefix: str) -> str:
"""
assert_is_leaf_string(string)

if "f" in prefix and not any(True for _ in iter_fexpr_spans(string)):
if "f" in prefix and not fstring_contains_expr(string):
new_prefix = prefix.replace("f", "")

temp = string[len(prefix) :]
Expand Down

0 comments on commit 9aa1af4

Please sign in to comment.