Skip to content

Commit

Permalink
Rebase Pyink to Black 25.1.0
Browse files Browse the repository at this point in the history
Additionally, disable Preview.always_one_newline_after_import
formatting feature as it is not compatible with formatting of line
ranges.

PiperOrigin-RevId: 729501835
  • Loading branch information
AleksMat authored and copybara-github committed Feb 24, 2025
1 parent 1de8968 commit 52e20a6
Show file tree
Hide file tree
Showing 68 changed files with 1,815 additions and 540 deletions.
307 changes: 134 additions & 173 deletions patches/pyink.patch

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ build-backend = "hatchling.build"
[project]
name = "pyink"
description = "Pyink is a python formatter, forked from Black with slightly different behavior."
license = { text = "MIT" }
license = "MIT"
requires-python = ">=3.9"
readme = "README.md"
authors = [{name = "The Pyink Maintainers", email = "[email protected]"}]
Expand Down Expand Up @@ -42,7 +42,7 @@ dependencies = [
"platformdirs>=2",
"tomli>=1.1.0; python_version < '3.11'",
"typing_extensions>=4.0.1; python_version < '3.11'",
"black==24.10.0",
"black==25.1.0",
]
dynamic = ["version"]

Expand Down
26 changes: 15 additions & 11 deletions src/pyink/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,22 @@
import sys
import tokenize
import traceback
from contextlib import contextmanager
from dataclasses import replace
from datetime import datetime, timezone
from enum import Enum
from json.decoder import JSONDecodeError
from pathlib import Path
from typing import (
Any,
from collections.abc import (
Collection,
Generator,
Iterator,
MutableMapping,
Optional,
Pattern,
Sequence,
Sized,
Union,
)
from contextlib import contextmanager
from dataclasses import replace
from datetime import datetime, timezone
from enum import Enum
from json.decoder import JSONDecodeError
from pathlib import Path
from re import Pattern
from typing import Any, Optional, Union

from blib2to3.pgen2 import token
from blib2to3.pytree import Leaf, Node
Expand Down Expand Up @@ -828,6 +826,12 @@ def get_sources(
for s in src:
if s == "-" and stdin_filename:
path = Path(stdin_filename)
if path_is_excluded(stdin_filename, force_exclude):
report.path_ignored(
path,
"--stdin-filename matches the --force-exclude regular expression",
)
continue
is_stdin = True
else:
path = Path(s)
Expand Down
3 changes: 2 additions & 1 deletion src/pyink/brackets.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Builds on top of nodes.py to track brackets."""

from collections.abc import Iterable, Sequence
from dataclasses import dataclass, field
from typing import Final, Iterable, Optional, Sequence, Union
from typing import Final, Optional, Union

from pyink.nodes import (
BRACKET,
Expand Down
3 changes: 2 additions & 1 deletion src/pyink/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
import pickle
import sys
import tempfile
from collections.abc import Iterable
from dataclasses import dataclass, field
from pathlib import Path
from typing import Iterable, NamedTuple
from typing import NamedTuple

from platformdirs import user_cache_dir

Expand Down
11 changes: 4 additions & 7 deletions src/pyink/comments.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import re
from collections.abc import Collection, Iterator
from dataclasses import dataclass
from functools import lru_cache
from typing import Collection, Final, Iterator, Optional, Union
from typing import Final, Optional, Union

from pyink import ink_comments
from pyink.mode import Mode, Preview
from pyink.mode import Mode
from pyink.nodes import (
CLOSING_BRACKETS,
STANDALONE_COMMENT,
Expand Down Expand Up @@ -235,11 +236,7 @@ def convert_one_fmt_off_pair(
standalone_comment_prefix += fmt_off_prefix
hidden_value = comment.value + "\n" + hidden_value
if is_fmt_skip:
hidden_value += (
comment.leading_whitespace
if Preview.no_normalize_fmt_skip_whitespace in mode
else " "
) + comment.value
hidden_value += comment.leading_whitespace + comment.value
if hidden_value.endswith("\n"):
# That happens when one of the `ignored_nodes` ended with a NEWLINE
# leaf (possibly followed by a DEDENT).
Expand Down
3 changes: 2 additions & 1 deletion src/pyink/concurrency.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
import signal
import sys
import traceback
from collections.abc import Iterable
from concurrent.futures import Executor, ProcessPoolExecutor, ThreadPoolExecutor
from multiprocessing import Manager
from pathlib import Path
from typing import Any, Iterable, Optional
from typing import Any, Optional

from mypy_extensions import mypyc_attr

Expand Down
3 changes: 2 additions & 1 deletion src/pyink/debug.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from collections.abc import Iterator
from dataclasses import dataclass, field
from typing import Any, Iterator, TypeVar, Union
from typing import Any, TypeVar, Union

from pyink.nodes import Visitor
from pyink.output import out
Expand Down
13 changes: 3 additions & 10 deletions src/pyink/files.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import io
import os
import sys
from collections.abc import Iterable, Iterator, Sequence
from functools import lru_cache
from pathlib import Path
from typing import (
TYPE_CHECKING,
Any,
Iterable,
Iterator,
Optional,
Pattern,
Sequence,
Union,
)
from re import Pattern
from typing import TYPE_CHECKING, Any, Optional, Union

from mypy_extensions import mypyc_attr
from packaging.specifiers import InvalidSpecifier, Specifier, SpecifierSet
Expand Down
27 changes: 18 additions & 9 deletions src/pyink/handle_ipynb_magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"time",
"timeit",
))
TOKEN_HEX = secrets.token_hex


@dataclasses.dataclass(frozen=True)
Expand Down Expand Up @@ -160,7 +159,7 @@ def mask_cell(src: str) -> tuple[str, list[Replacement]]:
becomes
"25716f358c32750e"
b"25716f358c32750"
'foo'
The replacements are returned, along with the transformed code.
Expand Down Expand Up @@ -192,6 +191,18 @@ def mask_cell(src: str) -> tuple[str, list[Replacement]]:
return transformed, replacements


def create_token(n_chars: int) -> str:
"""Create a randomly generated token that is n_chars characters long."""
assert n_chars > 0
n_bytes = max(n_chars // 2 - 1, 1)
token = secrets.token_hex(n_bytes)
if len(token) + 3 > n_chars:
token = token[:-1]
# We use a bytestring so that the string does not get interpreted
# as a docstring.
return f'b"{token}"'


def get_token(src: str, magic: str) -> str:
"""Return randomly generated token to mask IPython magic with.
Expand All @@ -201,21 +212,19 @@ def get_token(src: str, magic: str) -> str:
not already present anywhere else in the cell.
"""
assert magic
nbytes = max(len(magic) // 2 - 1, 1)
token = TOKEN_HEX(nbytes)
n_chars = len(magic)
token = create_token(n_chars)
counter = 0
while token in src:
token = TOKEN_HEX(nbytes)
token = create_token(n_chars)
counter += 1
if counter > 100:
raise AssertionError(
"INTERNAL ERROR: Black was not able to replace IPython magic. "
"Please report a bug on https://github.com/psf/black/issues. "
f"The magic might be helpful: {magic}"
) from None
if len(token) + 2 < len(magic):
token = f"{token}."
return f'"{token}"'
return token


def replace_cell_magics(src: str) -> tuple[str, list[Replacement]]:
Expand Down Expand Up @@ -306,7 +315,7 @@ def unmask_cell(src: str, replacements: list[Replacement]) -> str:
for replacement in replacements:
src = src.replace(replacement.mask, replacement.src)
# Strings in src might have been reformatted with single quotes.
src = src.replace(f"'{replacement.mask[1:-1]}'", replacement.src)
src = src.replace(f"b'{replacement.mask[2:-1]}'", replacement.src)
return src


Expand Down
13 changes: 8 additions & 5 deletions src/pyink/ink_comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
"""

import re
from typing import Optional

from pyink.mode import Mode


def comment_contains_pragma(comment: str, mode: Mode) -> bool:
"""Check if the given string contains one of the pragma forms.
def comment_contains_pragma(comment: str, mode: Optional[Mode]) -> bool:
"""Check if the given string contains one of the pragma forms.
A pragma form can appear at the beginning of a comment:
# pytype: disable=attribute-error
Expand All @@ -25,6 +26,8 @@ def comment_contains_pragma(comment: str, mode: Mode) -> bool:
Returns:
True if the comment contains one of the pragma forms.
"""
joined_pragma_expression = "|".join(mode.pyink_annotation_pragmas)
pragma_regex = re.compile(rf"([#|;] ?(?:{joined_pragma_expression}))")
return pragma_regex.search(comment) is not None
if mode is None:
mode = Mode()
joined_pragma_expression = "|".join(mode.pyink_annotation_pragmas)
pragma_regex = re.compile(rf"([#|;] ?(?:{joined_pragma_expression}))")
return pragma_regex.search(comment) is not None
Loading

0 comments on commit 52e20a6

Please sign in to comment.