Fix bug where black tries to split string on escaped space (#1799)
Closes #1505.
This commit is contained in:
parent
8c8af4f161
commit
6c3f818185
@ -3590,7 +3590,8 @@ class StringSplitter(CustomSplitMapMixin, BaseStringSplitter):
|
|||||||
MIN_SUBSTR_SIZE characters.
|
MIN_SUBSTR_SIZE characters.
|
||||||
|
|
||||||
The string will ONLY be split on spaces (i.e. each new substring should
|
The string will ONLY be split on spaces (i.e. each new substring should
|
||||||
start with a space).
|
start with a space). Note that the string will NOT be split on a space
|
||||||
|
which is escaped with a backslash.
|
||||||
|
|
||||||
If the string is an f-string, it will NOT be split in the middle of an
|
If the string is an f-string, it will NOT be split in the middle of an
|
||||||
f-expression (e.g. in f"FooBar: {foo() if x else bar()}", {foo() if x
|
f-expression (e.g. in f"FooBar: {foo() if x else bar()}", {foo() if x
|
||||||
@ -3930,11 +3931,23 @@ def passes_all_checks(i: Index) -> bool:
|
|||||||
section of this classes' docstring would be be met by returning @i.
|
section of this classes' docstring would be be met by returning @i.
|
||||||
"""
|
"""
|
||||||
is_space = string[i] == " "
|
is_space = string[i] == " "
|
||||||
|
|
||||||
|
is_not_escaped = True
|
||||||
|
j = i - 1
|
||||||
|
while is_valid_index(j) and string[j] == "\\":
|
||||||
|
is_not_escaped = not is_not_escaped
|
||||||
|
j -= 1
|
||||||
|
|
||||||
is_big_enough = (
|
is_big_enough = (
|
||||||
len(string[i:]) >= self.MIN_SUBSTR_SIZE
|
len(string[i:]) >= self.MIN_SUBSTR_SIZE
|
||||||
and len(string[:i]) >= self.MIN_SUBSTR_SIZE
|
and len(string[:i]) >= self.MIN_SUBSTR_SIZE
|
||||||
)
|
)
|
||||||
return is_space and is_big_enough and not breaks_fstring_expression(i)
|
return (
|
||||||
|
is_space
|
||||||
|
and is_not_escaped
|
||||||
|
and is_big_enough
|
||||||
|
and not breaks_fstring_expression(i)
|
||||||
|
)
|
||||||
|
|
||||||
# First, we check all indices BELOW @max_break_idx.
|
# First, we check all indices BELOW @max_break_idx.
|
||||||
break_idx = max_break_idx
|
break_idx = max_break_idx
|
||||||
|
@ -353,6 +353,24 @@ def xxxxxxx_xxxxxx(xxxx):
|
|||||||
key
|
key
|
||||||
] = "test" # set some Thrift field to non-None in the struct aa bb cc dd ee
|
] = "test" # set some Thrift field to non-None in the struct aa bb cc dd ee
|
||||||
|
|
||||||
|
RE_ONE_BACKSLASH = {
|
||||||
|
"asdf_hjkl_jkl": re.compile(
|
||||||
|
r"(?<!([0-9]\ ))(?<=(^|\ ))([A-Z]+(\ )?|[0-9](\ )|[a-z](\ )){4,7}([A-Z]|[0-9]|[a-z])($|\b)(?!(\ ?([0-9]\ )|(\.)))"
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
|
RE_TWO_BACKSLASHES = {
|
||||||
|
"asdf_hjkl_jkl": re.compile(
|
||||||
|
r"(?<!([0-9]\ ))(?<=(^|\ ))([A-Z]+(\ )?|[0-9](\ )|[a-z](\\ )){4,7}([A-Z]|[0-9]|[a-z])($|\b)(?!(\ ?([0-9]\ )|(\.)))"
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
|
RE_THREE_BACKSLASHES = {
|
||||||
|
"asdf_hjkl_jkl": re.compile(
|
||||||
|
r"(?<!([0-9]\ ))(?<=(^|\ ))([A-Z]+(\ )?|[0-9](\ )|[a-z](\\\ )){4,7}([A-Z]|[0-9]|[a-z])($|\b)(?!(\ ?([0-9]\ )|(\.)))"
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
# output
|
# output
|
||||||
|
|
||||||
|
|
||||||
@ -793,3 +811,22 @@ def xxxxxxx_xxxxxx(xxxx):
|
|||||||
value.__dict__[
|
value.__dict__[
|
||||||
key
|
key
|
||||||
] = "test" # set some Thrift field to non-None in the struct aa bb cc dd ee
|
] = "test" # set some Thrift field to non-None in the struct aa bb cc dd ee
|
||||||
|
|
||||||
|
RE_ONE_BACKSLASH = {
|
||||||
|
"asdf_hjkl_jkl": re.compile(
|
||||||
|
r"(?<!([0-9]\ ))(?<=(^|\ ))([A-Z]+(\ )?|[0-9](\ )|[a-z](\ )){4,7}([A-Z]|[0-9]|[a-z])($|\b)(?!(\ ?([0-9]\ )|(\.)))"
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
|
RE_TWO_BACKSLASHES = {
|
||||||
|
"asdf_hjkl_jkl": re.compile(
|
||||||
|
r"(?<!([0-9]\ ))(?<=(^|\ ))([A-Z]+(\ )?|[0-9](\ )|[a-z](\\"
|
||||||
|
r" )){4,7}([A-Z]|[0-9]|[a-z])($|\b)(?!(\ ?([0-9]\ )|(\.)))"
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
|
RE_THREE_BACKSLASHES = {
|
||||||
|
"asdf_hjkl_jkl": re.compile(
|
||||||
|
r"(?<!([0-9]\ ))(?<=(^|\ ))([A-Z]+(\ )?|[0-9](\ )|[a-z](\\\ )){4,7}([A-Z]|[0-9]|[a-z])($|\b)(?!(\ ?([0-9]\ )|(\.)))"
|
||||||
|
),
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user