]> git.madduck.net Git - etc/vim.git/commitdiff

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Use parentheses on method access on float and int literals (#2799)
authorShivansh-007 <shivansh-007@outlook.com>
Fri, 28 Jan 2022 05:31:50 +0000 (11:01 +0530)
committerGitHub <noreply@github.com>
Fri, 28 Jan 2022 05:31:50 +0000 (21:31 -0800)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Co-authored-by: Felix Hildén <felix.hilden@gmail.com>
CHANGES.md
src/black/linegen.py
src/black/nodes.py
tests/data/attribute_access_on_number_literals.py [new file with mode: 0644]
tests/data/expression.diff
tests/data/expression.py
tests/data/expression_skip_magic_trailing_comma.diff
tests/test_format.py

index 0dc4952f0693d81326cec53b06ab60b2f9dd876d..6966a91aa119c56a5010f91ae4925627b01d5874 100644 (file)
@@ -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
index ac60ed1986d82cff4fc0309ebf372bdba8912eb1..b572ed0b52fdbe6d546365ffbce2876466b568d0 100644 (file)
@@ -197,6 +197,28 @@ class LineGenerator(Visitor[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()
index 74dfa896295fae424d8bc678f66d06e7094d04c1..51d4cb8618d0a6a2c1f8ac1d55318ed2579fb5d9 100644 (file)
@@ -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:
diff --git a/tests/data/attribute_access_on_number_literals.py b/tests/data/attribute_access_on_number_literals.py
new file mode 100644 (file)
index 0000000..7c16bdf
--- /dev/null
@@ -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)
index 5f29a18dc7f64cd31213ec54bbad70e834993ac0..2eaaeb479f8c7334e75468642c01fa25c5d92f9c 100644 (file)
@@ -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
  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]
index b056841027da189fcc23f33512684e1c9d994240..06096c589f1791f9389f07b6ecef0f6b88299580 100644 (file)
@@ -382,8 +382,8 @@ call(**self.screen_kwargs)
 call(b, **self.screen_kwargs)
 lukasz.langa.pl
 call.me(maybe)
-.real
-1.0 .real
+(1).real
+(1.0).real
 ....__class__
 list[str]
 dict[str, int]
index 5b722c91352e0d3fb597c3a7a7b4085a08c404e2..eba3fd2da7dfeb63a2390f74ffabd1365b09728b 100644 (file)
@@ -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
  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]
index 88f084ea47815da2cf0951a50634253a9e0e8cb5..aef22545f5b4bd128469ed4ddbb51d47832036c3 100644 (file)
@@ -15,6 +15,7 @@ from tests.util import (
 )
 
 SIMPLE_CASES: List[str] = [
+    "attribute_access_on_number_literals",
     "beginning_backslash",
     "bracketmatch",
     "class_blank_parentheses",