Skip to content

Commit 9ec49c6

Browse files
The Pyink Maintainerscopybara-github
authored andcommitted
Rebase Pyink to Black v24.8.0.
PiperOrigin-RevId: 670995688
1 parent eebe6ee commit 9ec49c6

37 files changed

+1282
-275
lines changed

patches/pyink.patch

Lines changed: 42 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,15 @@
4949
from pyink.mode import Mode as Mode # re-exported
5050
-from pyink.mode import Preview, TargetVersion, supports_feature
5151
+from pyink.mode import Preview, QuoteStyle, TargetVersion, supports_feature
52-
from pyink.nodes import (
53-
STARS,
54-
is_number_token,
55-
@@ -90,12 +91,11 @@ from pyink.ranges import (
52+
from pyink.nodes import STARS, is_number_token, is_simple_decorator_expression, syms
53+
from pyink.output import color_diff, diff, dump_to_file, err, ipynb_diff, out
54+
from pyink.parsing import ( # noqa F401
55+
@@ -90,9 +91,8 @@ from pyink.ranges import (
5656
parse_line_ranges,
5757
sanitized_lines,
5858
)
5959
+from pyink import ink
6060
from pyink.report import Changed, NothingChanged, Report
61-
from pyink.trans import iter_fexpr_spans
62-
63-
from google3.devtools.python.pyformat import import_sorting
6461
-from blib2to3.pgen2 import token
6562
-from blib2to3.pytree import Leaf, Node
6663

@@ -471,17 +468,6 @@
471468
yield complete_line
472469

473470
def visit_default(self, node: LN) -> Iterator[Line]:
474-
@@ -156,7 +182,9 @@ class LineGenerator(Visitor[Line]):
475-
node.prefix = ""
476-
if self.mode.string_normalization and node.type == token.STRING:
477-
node.value = normalize_string_prefix(node.value)
478-
- node.value = normalize_string_quotes(node.value)
479-
+ node.value = normalize_string_quotes(
480-
+ node.value, preferred_quote=self.mode.preferred_quote
481-
+ )
482-
if node.type == token.NUMBER:
483-
normalize_numeric_literal(node)
484-
if node.type not in WHITESPACE:
485471
@@ -166,26 +194,27 @@ class LineGenerator(Visitor[Line]):
486472
def visit_test(self, node: Node) -> Iterator[Line]:
487473
"""Visit an `x if y else z` test"""
@@ -562,15 +548,15 @@
562548
+ yield from self.line(_DEDENT)
563549

564550
else:
565-
- if not node.parent or not is_stub_suite(node.parent):
551+
- if node.parent and is_stub_suite(node.parent):
566552
+ if (
567-
+ not (self.mode.is_pyi or not self.mode.is_pyink)
568-
+ or not node.parent
569-
+ or not is_stub_suite(node.parent, self.mode)
553+
+ (self.mode.is_pyi or not self.mode.is_pyink)
554+
+ and node.parent
555+
+ and is_stub_suite(node.parent, self.mode)
570556
+ ):
571-
yield from self.line()
572-
yield from self.visit_default(node)
573-
557+
node.prefix = ""
558+
yield from self.visit_default(node)
559+
return
574560
@@ -414,7 +453,10 @@ class LineGenerator(Visitor[Line]):
575561
yield from self.visit_default(node)
576562

@@ -584,9 +570,9 @@
584570

585571
if is_docstring(leaf, self.mode) and not re.search(r"\\\s*\n", leaf.value):
586572
@@ -428,7 +470,9 @@ class LineGenerator(Visitor[Line]):
587-
# formatting as visit_default() is called *after*. To avoid a
588-
# situation where this function formats a docstring differently on
589-
# the second pass, normalize it early.
573+
# see padding logic below), there's a possibility for unstable
574+
# formatting. To avoid a situation where this function formats a
575+
# docstring differently on the second pass, normalize it early.
590576
- docstring = normalize_string_quotes(docstring)
591577
+ docstring = normalize_string_quotes(
592578
+ docstring, preferred_quote=self.mode.preferred_quote
@@ -628,6 +614,17 @@
628614
self.visit_del_stmt = partial(v, keywords=Ø, parens={"del"})
629615
self.visit_async_funcdef = self.visit_async_stmt
630616
self.visit_decorated = self.visit_decorators
617+
@@ -499,7 +556,9 @@ def visit_STRING(
618+
619+
if self.mode.string_normalization and leaf.type == token.STRING:
620+
leaf.value = normalize_string_prefix(leaf.value)
621+
- leaf.value = normalize_string_quotes(leaf.value)
622+
+ leaf.value = normalize_string_quotes(
623+
+ leaf.value, preferred_quote=self.mode.preferred_quote
624+
+ )
625+
yield from self.visit_default(leaf)
626+
627+
def visit_NUMBER(self, leaf: Leaf) -> Iterator[Line]:
631628
@@ -577,10 +628,19 @@ def transform_line(
632629

633630
ll = mode.line_length
@@ -1012,70 +1009,6 @@
10121009
+ return Quote.DOUBLE
10131010
--- a/nodes.py
10141011
+++ b/nodes.py
1015-
@@ -532,8 +532,8 @@ def first_leaf_of(node: LN) -> Optional[
1016-
1017-
1018-
def is_arith_like(node: LN) -> bool:
1019-
- """Whether node is an arithmetic or a binary arithmetic expression"""
1020-
- return node.type in {
1021-
+ """Whether node is an arithmetic or a binary arithmetic expression"""
1022-
+ return node.type in {
1023-
syms.arith_expr,
1024-
syms.shift_expr,
1025-
syms.xor_expr,
1026-
@@ -542,14 +542,14 @@ def is_arith_like(node: LN) -> bool:
1027-
1028-
1029-
def is_docstring(leaf: Leaf, mode: Mode) -> bool:
1030-
- if leaf.type != token.STRING:
1031-
- return False
1032-
+ if leaf.type != token.STRING:
1033-
+ return False
1034-
1035-
- prefix = get_string_prefix(leaf.value)
1036-
- if set(prefix).intersection("bBfF"):
1037-
- return False
1038-
+ prefix = get_string_prefix(leaf.value)
1039-
+ if set(prefix).intersection("bBfF"):
1040-
+ return False
1041-
1042-
- if (
1043-
+ if (
1044-
Preview.unify_docstring_detection in mode
1045-
and leaf.parent
1046-
and leaf.parent.type == syms.simple_stmt
1047-
@@ -557,20 +557,22 @@ def is_docstring(leaf: Leaf, mode: Mode)
1048-
and leaf.parent.parent
1049-
and leaf.parent.parent.type == syms.file_input
1050-
):
1051-
- return True
1052-
+ return True
1053-
1054-
- if prev_siblings_are(
1055-
+ if prev_siblings_are(
1056-
leaf.parent, [None, token.NEWLINE, token.INDENT, syms.simple_stmt]
1057-
):
1058-
- return True
1059-
+ return True
1060-
1061-
- # Multiline docstring on the same line as the `def`.
1062-
- if prev_siblings_are(leaf.parent, [syms.parameters, token.COLON, syms.simple_stmt]):
1063-
- # `syms.parameters` is only used in funcdefs and async_funcdefs in the Python
1064-
- # grammar. We're safe to return True without further checks.
1065-
- return True
1066-
+ # Multiline docstring on the same line as the `def`.
1067-
+ if prev_siblings_are(
1068-
+ leaf.parent, [syms.parameters, token.COLON, syms.simple_stmt]
1069-
+ ):
1070-
+ # `syms.parameters` is only used in funcdefs and async_funcdefs in the Python
1071-
+ # grammar. We're safe to return True without further checks.
1072-
+ return True
1073-
1074-
- return False
1075-
+ return False
1076-
1077-
1078-
def is_empty_tuple(node: LN) -> bool:
10791012
@@ -763,9 +765,13 @@ def is_function_or_class(node: Node) ->
10801013
return node.type in {syms.funcdef, syms.classdef, syms.async_funcdef}
10811014

@@ -1094,7 +1027,7 @@
10941027
# If there is a comment, we want to keep it.
10951028
--- a/pyproject.toml
10961029
+++ b/pyproject.toml
1097-
@@ -1,51 +1,23 @@
1030+
@@ -1,52 +1,23 @@
10981031
-# Example configuration for Black.
10991032
-
11001033
-# NOTE: you have to use single-quoted strings in TOML for regular expressions.
@@ -1112,9 +1045,10 @@
11121045
-extend-exclude = '''
11131046
-/(
11141047
- # The following are specific to Black, you probably don't want those.
1115-
- tests/data
1116-
- | profiling
1117-
-)/
1048+
- tests/data/
1049+
- | profiling/
1050+
- | scripts/generate_schema.py # Uses match syntax
1051+
-)
11181052
-'''
11191053
-# We use the unstable style for formatting Black itself. If you
11201054
-# want bug-free formatting, you should keep this off. If you want
@@ -1155,11 +1089,11 @@
11551089
classifiers = [
11561090
"Development Status :: 5 - Production/Stable",
11571091
"Environment :: Console",
1158-
@@ -70,51 +42,34 @@ dependencies = [
1092+
@@ -70,53 +42,35 @@ dependencies = [
11591093
"platformdirs>=2",
11601094
"tomli>=1.1.0; python_version < '3.11'",
11611095
"typing_extensions>=4.0.1; python_version < '3.11'",
1162-
+ "black==24.3.0",
1096+
+ "black==24.8.0",
11631097
]
11641098
-dynamic = ["readme", "version"]
11651099
+dynamic = ["version"]
@@ -1185,8 +1119,10 @@
11851119
+pyink = "pyink:patched_main"
11861120

11871121
[project.urls]
1122+
-Documentation = "https://black.readthedocs.io/"
11881123
-Changelog = "https://github.com/psf/black/blob/main/CHANGES.md"
1189-
-Homepage = "https://github.com/psf/black"
1124+
-Repository = "https://github.com/psf/black"
1125+
-Issues = "https://github.com/psf/black/issues"
11901126
-
11911127
-[tool.hatch.metadata.hooks.fancy-pypi-readme]
11921128
-content-type = "text/markdown"
@@ -1195,7 +1131,8 @@
11951131
- { path = "CHANGES.md" },
11961132
-]
11971133
+Changelog = "https://github.com/google/pyink/blob/pyink/CHANGES.md"
1198-
+Homepage = "https://github.com/google/pyink"
1134+
+Repository = "https://github.com/google/pyink"
1135+
+Issues = "https://github.com/google/pyink/issues"
11991136

12001137
[tool.hatch.version]
12011138
source = "vcs"
@@ -1261,7 +1198,7 @@
12611198
--- a/strings.py
12621199
+++ b/strings.py
12631200
@@ -8,6 +8,7 @@ from functools import lru_cache
1264-
from typing import Final, List, Match, Pattern
1201+
from typing import Final, List, Match, Pattern, Tuple
12651202

12661203
from pyink._width_table import WIDTH_TABLE
12671204
+from pyink.mode import Quote
@@ -1279,8 +1216,8 @@
12791216
+
12801217
+ For three quotes strings, always use double-quote.
12811218

1282-
Adds or removes backslashes as appropriate. Doesn't parse and fix
1283-
strings nested in f-strings.
1219+
Adds or removes backslashes as appropriate.
1220+
"""
12841221
@@ -234,8 +237,8 @@ def normalize_string_quotes(s: str) -> s
12851222
if new_escape_count > orig_escape_count:
12861223
return s # Do not introduce more escaping
@@ -1302,27 +1239,6 @@
13021239
+pyink = false
13031240
--- a/tests/test_black.py
13041241
+++ b/tests/test_black.py
1305-
@@ -1666,9 +1666,9 @@ class BlackTestCase(BlackBaseTestCase):
1306-
src_dir.mkdir()
1307-
1308-
root_pyproject = root / "pyproject.toml"
1309-
- root_pyproject.write_text("[tool.black]", encoding="utf-8")
1310-
+ root_pyproject.write_text("[tool.pyink]", encoding="utf-8")
1311-
src_pyproject = src_dir / "pyproject.toml"
1312-
- src_pyproject.write_text("[tool.black]", encoding="utf-8")
1313-
+ src_pyproject.write_text("[tool.pyink]", encoding="utf-8")
1314-
src_python = src_dir / "foo.py"
1315-
src_python.touch()
1316-
1317-
@@ -1699,7 +1699,7 @@ class BlackTestCase(BlackBaseTestCase):
1318-
1319-
src_sub_python = src_sub / "bar.py"
1320-
1321-
- # we skip src_sub_pyproject since it is missing the [tool.black] section
1322-
+ # we skip src_sub_pyproject since it is missing the [tool.pyink] section
1323-
self.assertEqual(
1324-
pyink.find_project_root((src_sub_python,)),
1325-
(src_dir.resolve(), "pyproject.toml"),
13261242
@@ -2772,6 +2772,82 @@ class TestFileCollection:
13271243
stdin_filename=stdin_filename,
13281244
)
@@ -1432,7 +1348,7 @@
14321348
skip_install = True
14331349
commands =
14341350
pip install -e .
1435-
- black --check {toxinidir}/src {toxinidir}/tests
1351+
- black --check {toxinidir}/src {toxinidir}/tests {toxinidir}/docs {toxinidir}/scripts
14361352
-
14371353
-[testenv:generate_schema]
14381354
-setenv = PYTHONWARNDEFAULTENCODING =
@@ -1441,7 +1357,7 @@
14411357
-commands =
14421358
- pip install -e .
14431359
- python {toxinidir}/scripts/generate_schema.py --outfile {toxinidir}/src/black/resources/black.schema.json
1444-
+ pyink --check {toxinidir}/src {toxinidir}/tests
1360+
+ pyink --check {toxinidir}/src {toxinidir}/tests {toxinidir}/docs {toxinidir}/scripts
14451361
--- a/trans.py
14461362
+++ b/trans.py
14471363
@@ -28,8 +28,8 @@ from typing import (

pyproject.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ dependencies = [
4242
"platformdirs>=2",
4343
"tomli>=1.1.0; python_version < '3.11'",
4444
"typing_extensions>=4.0.1; python_version < '3.11'",
45-
"black==24.3.0",
45+
"black==24.8.0",
4646
]
4747
dynamic = ["version"]
4848

@@ -59,7 +59,8 @@ pyink = "pyink:patched_main"
5959

6060
[project.urls]
6161
Changelog = "https://github.com/google/pyink/blob/pyink/CHANGES.md"
62-
Homepage = "https://github.com/google/pyink"
62+
Repository = "https://github.com/google/pyink"
63+
Issues = "https://github.com/google/pyink/issues"
6364

6465
[tool.hatch.version]
6566
source = "vcs"

src/pyink/__init__.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,7 @@
7070
from pyink.mode import FUTURE_FLAG_TO_FEATURE, Feature, VERSION_TO_FEATURES
7171
from pyink.mode import Mode as Mode # re-exported
7272
from pyink.mode import Preview, QuoteStyle, TargetVersion, supports_feature
73-
from pyink.nodes import (
74-
STARS,
75-
is_number_token,
76-
is_simple_decorator_expression,
77-
is_string_token,
78-
syms,
79-
)
73+
from pyink.nodes import STARS, is_number_token, is_simple_decorator_expression, syms
8074
from pyink.output import color_diff, diff, dump_to_file, err, ipynb_diff, out
8175
from pyink.parsing import ( # noqa F401
8276
ASTSafetyError,
@@ -93,7 +87,6 @@
9387
)
9488
from pyink import ink
9589
from pyink.report import Changed, NothingChanged, Report
96-
from pyink.trans import iter_fexpr_spans
9790

9891
COMPILED = Path(__file__).suffix in (".pyd", ".so")
9992

@@ -1308,7 +1301,10 @@ def _format_str_once(
13081301
elt = EmptyLineTracker(mode=mode)
13091302
split_line_features = {
13101303
feature
1311-
for feature in {Feature.TRAILING_COMMA_IN_CALL, Feature.TRAILING_COMMA_IN_DEF}
1304+
for feature in {
1305+
Feature.TRAILING_COMMA_IN_CALL,
1306+
Feature.TRAILING_COMMA_IN_DEF,
1307+
}
13121308
if supports_feature(versions, feature)
13131309
}
13141310
block: Optional[LinesBlock] = None
@@ -1380,15 +1376,14 @@ def get_features_used( # noqa: C901
13801376
}
13811377

13821378
for n in node.pre_order():
1383-
if is_string_token(n):
1384-
value_head = n.value[:2]
1385-
if value_head in {'f"', 'F"', "f'", "F'", "rf", "fr", "RF", "FR"}:
1386-
features.add(Feature.F_STRINGS)
1387-
if Feature.DEBUG_F_STRINGS not in features:
1388-
for span_beg, span_end in iter_fexpr_spans(n.value):
1389-
if n.value[span_beg : span_end - 1].rstrip().endswith("="):
1390-
features.add(Feature.DEBUG_F_STRINGS)
1391-
break
1379+
if n.type == token.FSTRING_START:
1380+
features.add(Feature.F_STRINGS)
1381+
elif (
1382+
n.type == token.RBRACE
1383+
and n.parent is not None
1384+
and any(child.type == token.EQUAL for child in n.parent.children)
1385+
):
1386+
features.add(Feature.DEBUG_F_STRINGS)
13921387

13931388
elif is_number_token(n):
13941389
if "_" in n.value:
@@ -1484,6 +1479,12 @@ def get_features_used( # noqa: C901
14841479
elif n.type in (syms.type_stmt, syms.typeparams):
14851480
features.add(Feature.TYPE_PARAMS)
14861481

1482+
elif (
1483+
n.type in (syms.typevartuple, syms.paramspec, syms.typevar)
1484+
and n.children[-2].type == token.EQUAL
1485+
):
1486+
features.add(Feature.TYPE_PARAM_DEFAULTS)
1487+
14871488
return features
14881489

14891490

0 commit comments

Comments
 (0)