Simplify black code by using generator expressions

This commit is contained in:
Yurii Karabas 2020-06-24 00:33:05 +03:00 committed by Łukasz Langa
parent 20f74c20f7
commit 1ebe9b70c5

View File

@ -641,8 +641,7 @@ def path_empty(
""" """
Exit if there is no `src` provided for formatting Exit if there is no `src` provided for formatting
""" """
if len(src) == 0: if not src and (verbose or not quiet):
if verbose or not quiet:
out(msg) out(msg)
ctx.exit(0) ctx.exit(0)
@ -928,7 +927,7 @@ def format_file_contents(src_contents: str, *, fast: bool, mode: Mode) -> FileCo
valid by calling :func:`assert_equivalent` and :func:`assert_stable` on it. valid by calling :func:`assert_equivalent` and :func:`assert_stable` on it.
`mode` is passed to :func:`format_str`. `mode` is passed to :func:`format_str`.
""" """
if src_contents.strip() == "": if not src_contents.strip():
raise NothingChanged raise NothingChanged
dst_contents = format_str(src_contents, mode=mode) dst_contents = format_str(src_contents, mode=mode)
@ -1062,7 +1061,7 @@ def get_grammars(target_versions: Set[TargetVersion]) -> List[Grammar]:
def lib2to3_parse(src_txt: str, target_versions: Iterable[TargetVersion] = ()) -> Node: def lib2to3_parse(src_txt: str, target_versions: Iterable[TargetVersion] = ()) -> Node:
"""Given a string with source, return the lib2to3 Node.""" """Given a string with source, return the lib2to3 Node."""
if src_txt[-1:] != "\n": if not src_txt.endswith("\n"):
src_txt += "\n" src_txt += "\n"
for grammar in get_grammars(set(target_versions)): for grammar in get_grammars(set(target_versions)):
@ -1547,11 +1546,10 @@ def is_triple_quoted_string(self) -> bool:
def contains_standalone_comments(self, depth_limit: int = sys.maxsize) -> bool: def contains_standalone_comments(self, depth_limit: int = sys.maxsize) -> bool:
"""If so, needs to be split before emitting.""" """If so, needs to be split before emitting."""
for leaf in self.leaves: return any(
if leaf.type == STANDALONE_COMMENT and leaf.bracket_depth <= depth_limit: leaf.type == STANDALONE_COMMENT and leaf.bracket_depth <= depth_limit
return True for leaf in self.leaves
)
return False
def contains_uncollapsable_type_comments(self) -> bool: def contains_uncollapsable_type_comments(self) -> bool:
ignored_ids = set() ignored_ids = set()
@ -3992,12 +3990,13 @@ class StringParenWrapper(CustomSplitMapMixin, BaseStringSplitter):
def do_splitter_match(self, line: Line) -> TMatchResult: def do_splitter_match(self, line: Line) -> TMatchResult:
LL = line.leaves LL = line.leaves
string_idx = None string_idx = (
string_idx = string_idx or self._return_match(LL) self._return_match(LL)
string_idx = string_idx or self._else_match(LL) or self._else_match(LL)
string_idx = string_idx or self._assert_match(LL) or self._assert_match(LL)
string_idx = string_idx or self._assign_match(LL) or self._assign_match(LL)
string_idx = string_idx or self._dict_match(LL) or self._dict_match(LL)
)
if string_idx is not None: if string_idx is not None:
string_value = line.leaves[string_idx].value string_value = line.leaves[string_idx].value
@ -4196,7 +4195,7 @@ def do_transform(self, line: Line, string_idx: int) -> Iterator[TResult[Line]]:
is_valid_index = is_valid_index_factory(LL) is_valid_index = is_valid_index_factory(LL)
insert_str_child = insert_str_child_factory(LL[string_idx]) insert_str_child = insert_str_child_factory(LL[string_idx])
comma_idx = len(LL) - 1 comma_idx = -1
ends_with_comma = False ends_with_comma = False
if LL[comma_idx].type == token.COMMA: if LL[comma_idx].type == token.COMMA:
ends_with_comma = True ends_with_comma = True
@ -4444,11 +4443,10 @@ def contains_pragma_comment(comment_list: List[Leaf]) -> bool:
of the more common static analysis tools for python (e.g. mypy, flake8, of the more common static analysis tools for python (e.g. mypy, flake8,
pylint). pylint).
""" """
for comment in comment_list: return any(
if comment.value.startswith(("# type:", "# noqa", "# pylint:")): comment.value.startswith(("# type:", "# noqa", "# pylint:"))
return True for comment in comment_list
)
return False
def insert_str_child_factory(string_leaf: Leaf) -> Callable[[LN], None]: def insert_str_child_factory(string_leaf: Leaf) -> Callable[[LN], None]: