Skip to content

Commit

Permalink
Fix nits, chain comparisons, unused params, hyphens (#4114)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anupya committed Dec 28, 2023
1 parent 1b831f2 commit 5178614
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/black/brackets.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def mark(self, leaf: Leaf) -> None:
if delim and self.previous is not None:
self.delimiters[id(self.previous)] = delim
else:
delim = is_split_after_delimiter(leaf, self.previous)
delim = is_split_after_delimiter(leaf)
if delim:
self.delimiters[id(leaf)] = delim
if leaf.type in OPENING_BRACKETS:
Expand Down Expand Up @@ -215,7 +215,7 @@ def get_open_lsqb(self) -> Optional[Leaf]:
return self.bracket_match.get((self.depth - 1, token.RSQB))


def is_split_after_delimiter(leaf: Leaf, previous: Optional[Leaf] = None) -> Priority:
def is_split_after_delimiter(leaf: Leaf) -> Priority:
"""Return the priority of the `leaf` delimiter, given a line break after it.
The delimiter priorities returned here are from those delimiters that would
Expand Down
4 changes: 2 additions & 2 deletions src/black/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ class Cache:

@classmethod
def read(cls, mode: Mode) -> Self:
"""Read the cache if it exists and is well formed.
"""Read the cache if it exists and is well-formed.
If it is not well formed, the call to write later should
If it is not well-formed, the call to write later should
resolve the issue.
"""
cache_file = get_cache_file(mode)
Expand Down
3 changes: 1 addition & 2 deletions src/black/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,7 @@ def convert_one_fmt_off_pair(
if comment.value in FMT_OFF:
fmt_off_prefix = ""
if len(lines) > 0 and not any(
comment_lineno >= line[0] and comment_lineno <= line[1]
for line in lines
line[0] <= comment_lineno <= line[1] for line in lines
):
# keeping indentation of comment by preserving original whitespaces.
fmt_off_prefix = prefix.split(comment.value)[0]
Expand Down
2 changes: 1 addition & 1 deletion src/black/linegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1635,7 +1635,7 @@ def generate_trailers_to_omit(line: Line, line_length: int) -> Iterator[Set[Leaf
opening_bracket: Optional[Leaf] = None
closing_bracket: Optional[Leaf] = None
inner_brackets: Set[LeafID] = set()
for index, leaf, leaf_length in line.enumerate_with_length(reversed=True):
for index, leaf, leaf_length in line.enumerate_with_length(is_reversed=True):
length += leaf_length
if length > line_length:
break
Expand Down
4 changes: 2 additions & 2 deletions src/black/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,15 +451,15 @@ def is_complex_subscript(self, leaf: Leaf) -> bool:
)

def enumerate_with_length(
self, reversed: bool = False
self, is_reversed: bool = False
) -> Iterator[Tuple[Index, Leaf, int]]:
"""Return an enumeration of leaves with their length.
Stops prematurely on multiline strings and standalone comments.
"""
op = cast(
Callable[[Sequence[Leaf]], Iterator[Tuple[Index, Leaf]]],
enumerate_reversed if reversed else enumerate,
enumerate_reversed if is_reversed else enumerate,
)
for index, leaf in op(self.leaves):
length = len(leaf.prefix) + len(leaf.value)
Expand Down
5 changes: 1 addition & 4 deletions src/black/ranges.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,10 +487,7 @@ def _find_lines_mapping_index(
index = start_index
while index < len(lines_mappings):
mapping = lines_mappings[index]
if (
mapping.original_start <= original_line
and original_line <= mapping.original_end
):
if mapping.original_start <= original_line <= mapping.original_end:
return index
index += 1
return index
21 changes: 10 additions & 11 deletions src/black/trans.py
Original file line number Diff line number Diff line change
Expand Up @@ -1273,7 +1273,7 @@ def iter_fexpr_spans(s: str) -> Iterator[Tuple[int, int]]:
i += 1
continue

# if we're in an expression part of the f-string, fast forward through strings
# if we're in an expression part of the f-string, fast-forward through strings
# note that backslashes are not legal in the expression portion of f-strings
if stack:
delim = None
Expand Down Expand Up @@ -1740,7 +1740,7 @@ def passes_all_checks(i: Index) -> bool:
"""
Returns:
True iff ALL of the conditions listed in the 'Transformations'
section of this classes' docstring would be be met by returning @i.
section of this classes' docstring would be met by returning @i.
"""
is_space = string[i] == " "
is_split_safe = is_valid_index(i - 1) and string[i - 1] in SPLIT_SAFE_CHARS
Expand Down Expand Up @@ -1932,7 +1932,7 @@ def _return_match(LL: List[Leaf]) -> Optional[int]:
OR
None, otherwise.
"""
# If this line is apart of a return/yield statement and the first leaf
# If this line is a part of a return/yield statement and the first leaf
# contains either the "return" or "yield" keywords...
if parent_type(LL[0]) in [syms.return_stmt, syms.yield_expr] and LL[
0
Expand All @@ -1957,7 +1957,7 @@ def _else_match(LL: List[Leaf]) -> Optional[int]:
OR
None, otherwise.
"""
# If this line is apart of a ternary expression and the first leaf
# If this line is a part of a ternary expression and the first leaf
# contains the "else" keyword...
if (
parent_type(LL[0]) == syms.test
Expand All @@ -1984,7 +1984,7 @@ def _assert_match(LL: List[Leaf]) -> Optional[int]:
OR
None, otherwise.
"""
# If this line is apart of an assert statement and the first leaf
# If this line is a part of an assert statement and the first leaf
# contains the "assert" keyword...
if parent_type(LL[0]) == syms.assert_stmt and LL[0].value == "assert":
is_valid_index = is_valid_index_factory(LL)
Expand Down Expand Up @@ -2019,7 +2019,7 @@ def _assign_match(LL: List[Leaf]) -> Optional[int]:
OR
None, otherwise.
"""
# If this line is apart of an expression statement or is a function
# If this line is a part of an expression statement or is a function
# argument AND the first leaf contains a variable name...
if (
parent_type(LL[0]) in [syms.expr_stmt, syms.argument, syms.power]
Expand All @@ -2040,7 +2040,7 @@ def _assign_match(LL: List[Leaf]) -> Optional[int]:
string_parser = StringParser()
idx = string_parser.parse(LL, string_idx)

# The next leaf MAY be a comma iff this line is apart
# The next leaf MAY be a comma iff this line is a part
# of a function argument...
if (
parent_type(LL[0]) == syms.argument
Expand Down Expand Up @@ -2187,8 +2187,7 @@ def do_transform(
if opening_bracket is not None and opening_bracket in left_leaves:
index = left_leaves.index(opening_bracket)
if (
index > 0
and index < len(left_leaves) - 1
0 < index < len(left_leaves) - 1
and left_leaves[index - 1].type == token.COLON
and left_leaves[index + 1].value == "lambda"
):
Expand Down Expand Up @@ -2297,7 +2296,7 @@ def parse(self, leaves: List[Leaf], string_idx: int) -> int:
* @leaves[@string_idx].type == token.STRING
Returns:
The index directly after the last leaf which is apart of the string
The index directly after the last leaf which is a part of the string
trailer, if a "trailer" exists.
OR
@string_idx + 1, if no string "trailer" exists.
Expand All @@ -2320,7 +2319,7 @@ def _next_state(self, leaf: Leaf) -> bool:
MUST be the leaf directly following @leaf.
Returns:
True iff @leaf is apart of the string's trailer.
True iff @leaf is a part of the string's trailer.
"""
# We ignore empty LPAR or RPAR leaves.
if is_empty_par(leaf):
Expand Down

0 comments on commit 5178614

Please sign in to comment.