Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use parentheses on method access on float and int literals #2799

Merged
merged 20 commits into from Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
e65f935
Use parenthesis on method access on number integerals (float and int)
Shivansh-007 Jan 23, 2022
5ee6627
Add new tests for format feature
Shivansh-007 Jan 23, 2022
51d6be8
Edit exisiting tests to reflect new formatting change
Shivansh-007 Jan 23, 2022
74b7b39
Add changelog entry
Shivansh-007 Jan 24, 2022
0be0352
Update CHANGES.md
Shivansh-007 Jan 24, 2022
9c8ba3e
Don"t format 0x prefixed integers literals and exclusive complex lit…
Shivansh-007 Jan 24, 2022
c2ca825
Don"t add whitespaces after number literal in dot operator access
Shivansh-007 Jan 25, 2022
68ba889
Merge remote-tracking branch 'upstream/main' into feat/use-paren-int-…
Shivansh-007 Jan 25, 2022
7b660ad
Merge branch 'main' into feat/use-paren-int-method-calls
JelleZijlstra Jan 26, 2022
faf131c
Update changelog wording
Shivansh-007 Jan 26, 2022
3dbe39d
Don't wrap binary and octal integers with parentheses
Shivansh-007 Jan 26, 2022
3a06d07
Merge branch 'feat/use-paren-int-method-calls' of https://github.com/…
Shivansh-007 Jan 26, 2022
db0ef32
Remove unnecessary `maxDiff` change to `BlackTestCase`
Shivansh-007 Jan 26, 2022
5368ba5
Update linegen.py
Shivansh-007 Jan 26, 2022
ff4e5c9
Remove extra newline
Shivansh-007 Jan 26, 2022
bf29eea
Move logic for handling whitespace before attributes to nodes.py
Shivansh-007 Jan 26, 2022
08e566b
Make check stricter to ensure that we are in an attribute trailer
Shivansh-007 Jan 26, 2022
9d6a0d4
Add tests to verify previous commit behaviour
Shivansh-007 Jan 26, 2022
a6551df
Merge branch 'main' into feat/use-paren-int-method-calls
JelleZijlstra Jan 27, 2022
385d67f
Remove parentheses from `and not` expression
Shivansh-007 Jan 28, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.md
Expand Up @@ -42,6 +42,9 @@
- Make passing `SRC` or `--code` mandatory and mutually exclusive (#2804)
- Work around bug that causes unstable formatting in some cases in the presence of the
magic trailing comma (#2807)
- Use parentheses for attribute access on decimal float and int literals (#2799)
- Don't add whitespace for attribute access on hexadecimal, binary, octal, and complex
literals (#2799)
- Deprecate the `black-primer` tool (#2809)

### Packaging
Expand Down
22 changes: 22 additions & 0 deletions src/black/linegen.py
Expand Up @@ -197,6 +197,28 @@ def visit_decorators(self, node: Node) -> Iterator[Line]:
yield from self.line()
yield from self.visit(child)

def visit_power(self, node: Node) -> Iterator[Line]:
for idx, leaf in enumerate(node.children[:-1]):
next_leaf = node.children[idx + 1]

if not isinstance(leaf, Leaf):
continue

value = leaf.value.lower()
if (
leaf.type == token.NUMBER
and next_leaf.type == syms.trailer
# Ensure that we are in an attribute trailer
and next_leaf.children[0].type == token.DOT
# It shouldn't wrap hexadecimal, binary and octal literals
and not value.startswith(("0x", "0b", "0o"))
# It shouldn't wrap complex literals
and "j" not in value
):
wrap_in_parentheses(node, leaf)

yield from self.visit_default(node)

def visit_SEMI(self, leaf: Leaf) -> Iterator[Line]:
"""Remove a semicolon and put the other statement on a separate line."""
yield from self.line()
Expand Down
7 changes: 1 addition & 6 deletions src/black/nodes.py
Expand Up @@ -306,12 +306,7 @@ def whitespace(leaf: Leaf, *, complex_subscript: bool) -> str: # noqa: C901
return NO

if not prev:
if t == token.DOT:
prevp = preceding_leaf(p)
if not prevp or prevp.type != token.NUMBER:
return NO

elif t == token.LSQB:
if t == token.DOT or t == token.LSQB:
return NO

elif prev.type != token.COMMA:
Expand Down
47 changes: 47 additions & 0 deletions tests/data/attribute_access_on_number_literals.py
@@ -0,0 +1,47 @@
x = 123456789 .bit_count()
x = (123456).__abs__()
x = .1.is_integer()
x = 1. .imag
x = 1E+1.imag
x = 1E-1.real
x = 123456789.123456789.hex()
x = 123456789.123456789E123456789 .real
x = 123456789E123456789 .conjugate()
x = 123456789J.real
x = 123456789.123456789J.__add__(0b1011.bit_length())
x = 0XB1ACC.conjugate()
x = 0B1011 .conjugate()
x = 0O777 .real
x = 0.000000006 .hex()
x = -100.0000J

if 10 .real:
...

y = 100[no]
y = 100(no)

# output

x = (123456789).bit_count()
x = (123456).__abs__()
x = (0.1).is_integer()
x = (1.0).imag
x = (1e1).imag
x = (1e-1).real
x = (123456789.123456789).hex()
x = (123456789.123456789e123456789).real
x = (123456789e123456789).conjugate()
x = 123456789j.real
x = 123456789.123456789j.__add__(0b1011.bit_length())
x = 0xB1ACC.conjugate()
x = 0b1011.conjugate()
x = 0o777.real
x = (0.000000006).hex()
x = -100.0000j

if (10).real:
...

y = 100[no]
y = 100(no)
9 changes: 6 additions & 3 deletions tests/data/expression.diff
Expand Up @@ -11,7 +11,7 @@
True
False
1
@@ -21,71 +21,104 @@
@@ -21,99 +21,135 @@
Name1 or (Name2 and Name3) or Name4
Name1 or Name2 and Name3 or Name4
v1 << 2
Expand Down Expand Up @@ -144,8 +144,11 @@
call(**self.screen_kwargs)
call(b, **self.screen_kwargs)
lukasz.langa.pl
@@ -94,26 +127,29 @@
1.0 .real
call.me(maybe)
-1 .real
-1.0 .real
+(1).real
+(1.0).real
....__class__
list[str]
dict[str, int]
Expand Down
4 changes: 2 additions & 2 deletions tests/data/expression.py
Expand Up @@ -382,8 +382,8 @@ async def f():
call(b, **self.screen_kwargs)
lukasz.langa.pl
call.me(maybe)
1 .real
1.0 .real
(1).real
(1.0).real
....__class__
list[str]
dict[str, int]
Expand Down
9 changes: 6 additions & 3 deletions tests/data/expression_skip_magic_trailing_comma.diff
Expand Up @@ -11,7 +11,7 @@
True
False
1
@@ -21,71 +21,92 @@
@@ -21,99 +21,118 @@
Name1 or (Name2 and Name3) or Name4
Name1 or Name2 and Name3 or Name4
v1 << 2
Expand Down Expand Up @@ -132,8 +132,11 @@
call(**self.screen_kwargs)
call(b, **self.screen_kwargs)
lukasz.langa.pl
@@ -94,26 +115,24 @@
1.0 .real
call.me(maybe)
-1 .real
-1.0 .real
+(1).real
+(1.0).real
....__class__
list[str]
dict[str, int]
Expand Down
1 change: 1 addition & 0 deletions tests/test_format.py
Expand Up @@ -15,6 +15,7 @@
)

SIMPLE_CASES: List[str] = [
"attribute_access_on_number_literals",
"beginning_backslash",
"bracketmatch",
"class_blank_parentheses",
Expand Down