Skip to content

Commit

Permalink
Merge 32e5544 into 9bd4134
Browse files Browse the repository at this point in the history
  • Loading branch information
Shivansh-007 authored Jan 20, 2022
2 parents 9bd4134 + 32e5544 commit 328c055
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
- Fix handling of standalone `match()` or `case()` when there is a trailing newline or a
comment inside of the parentheses. (#2760)
- Black now normalizes string prefix order (#2297)
- Use parentheses with equality check in walrus/assigment statements (#2770)

### Packaging

Expand Down
2 changes: 1 addition & 1 deletion src/black/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,7 @@ def format_ipynb_string(src_contents: str, *, fast: bool, mode: Mode) -> FileCon
Operate cell-by-cell, only on code cells, only for Python notebooks.
If the ``.ipynb`` originally had a trailing newline, it'll be preserved.
"""
trailing_newline = src_contents[-1] == "\n"
trailing_newline = (src_contents[-1] == "\n")
modified = False
nb = json.loads(src_contents)
validate_metadata(nb)
Expand Down
21 changes: 21 additions & 0 deletions src/black/linegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,27 @@ def visit_default(self, node: LN) -> Iterator[Line]:
self.current_line.append(node)
yield from super().visit_default(node)

def visit_comparison(self, node: Node) -> Iterator[Line]:
parent: Optional[Node] = node.parent
grandparent: Optional[Node] = parent.parent if parent else None
if (
parent is not None
and Leaf(token.EQEQUAL, "==") in node.children
and (
parent.type == syms.namedexpr_test
or (
grandparent is not None
and grandparent.type in (syms.expr_stmt, syms.annassign)
)
)
):
lpar = Leaf(token.LPAR, "(")
rpar = Leaf(token.RPAR, ")")
node.insert_child(0, lpar)
node.insert_child(len(node.children), rpar)

yield from self.visit_default(node)

def visit_INDENT(self, node: Leaf) -> Iterator[Line]:
"""Increase indentation level, maybe yield a line."""
# In blib2to3 INDENT never holds comments.
Expand Down
2 changes: 1 addition & 1 deletion src/black/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

ast3: Any

_IS_PYPY = platform.python_implementation() == "PyPy"
_IS_PYPY = (platform.python_implementation() == "PyPy")

try:
from typed_ast import ast3
Expand Down
2 changes: 1 addition & 1 deletion src/black/trans.py
Original file line number Diff line number Diff line change
Expand Up @@ -1397,7 +1397,7 @@ def passes_all_checks(i: Index) -> bool:
True iff ALL of the conditions listed in the 'Transformations'
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
Expand Down
6 changes: 3 additions & 3 deletions src/black_primer/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@


TEN_MINUTES_SECONDS = 600
WINDOWS = system() == "Windows"
WINDOWS = (system() == "Windows")
BLACK_BINARY = "black.exe" if WINDOWS else "black"
GIT_BINARY = "git.exe" if WINDOWS else "git"
LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -158,7 +158,7 @@ async def black_run(
)
return

stdin_test = project_name.upper() == "STDIN"
stdin_test = (project_name.upper() == "STDIN")
cmd = [str(which(BLACK_BINARY))]
if "cli_arguments" in project_config and project_config["cli_arguments"]:
cmd.extend(_flatten_cli_args(project_config["cli_arguments"]))
Expand Down Expand Up @@ -348,7 +348,7 @@ async def project_runner(
continue

repo_path: Optional[Path] = Path(__file__)
stdin_project = project_name.upper() == "STDIN"
stdin_project = (project_name.upper() == "STDIN")
if not stdin_project:
repo_path = await git_checkout_or_rebase(work_path, project_config, rebase)
if not repo_path:
Expand Down
2 changes: 1 addition & 1 deletion src/blackd/middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
def cors(allow_headers: Iterable[str]) -> Middleware:
@middleware
async def impl(request: Request, handler: Handler) -> StreamResponse:
is_options = request.method == "OPTIONS"
is_options = (request.method == "OPTIONS")
is_preflight = is_options and "Access-Control-Request-Method" in request.headers
if is_preflight:
resp = StreamResponse()
Expand Down
83 changes: 83 additions & 0 deletions tests/data/paren_eq_check_in_assigments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
match_count += new_value == old_value

if on_windows := os.name == "nt":
...

on_windows: bool = (os.name == "nt")

implementation_version = (
platform.python_version() if platform.python_implementation() == "CPython" else "Unknown"
)

is_mac = platform.system() == 'Darwin'

s = y == 2 + y == 4

name1 = name2 = name3

name1 == name2 == name3

check_sockets(on_windows=os.name == "nt")

if a := b == c:
pass

a = [y := f(x) == True, y ** 2, y ** 3]

a = lambda line: (m := re.match(pattern, line) == True)

a = b in c and b == d

a = b == c == d

a = b == c in d

a = b >= c == True

a = b in c

a = b > c

# output

match_count += (new_value == old_value)

if on_windows := (os.name == "nt"):
...

on_windows: bool = (os.name == "nt")

implementation_version = (
platform.python_version()
if platform.python_implementation() == "CPython"
else "Unknown"
)

is_mac = (platform.system() == "Darwin")

s = (y == 2 + y == 4)

name1 = name2 = name3

name1 == name2 == name3

check_sockets(on_windows=os.name == "nt")

if a := (b == c):
pass

a = [y := (f(x) == True), y ** 2, y ** 3]

a = lambda line: (m := (re.match(pattern, line) == True))

a = b in c and b == d

a = (b == c == d)

a = (b == c in d)

a = (b >= c == True)

a = b in c

a = b > c
1 change: 1 addition & 0 deletions tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"function2",
"function_trailing_comma",
"import_spacing",
"parenthesized_context_managers",
"remove_parens",
"slices",
"string_prefixes",
Expand Down

0 comments on commit 328c055

Please sign in to comment.