Skip to content

Commit

Permalink
merge remote main
Browse files Browse the repository at this point in the history
  • Loading branch information
kastkeepitjumpinlikekangaroos committed Oct 16, 2024
2 parents 40f47c8 + 484a669 commit 21afa91
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 70 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<!-- Changes that affect Black's stable style -->

- Collapse multiple lines into 1 after import (#4488)
- Fix formatting cells in IPython notebooks with magic methods and starting or trailing
empty lines (#4484)

### Preview style

Expand Down
10 changes: 5 additions & 5 deletions gallery/gallery.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from concurrent.futures import ThreadPoolExecutor
from functools import lru_cache, partial
from pathlib import Path
from typing import Generator, List, NamedTuple, Optional, Tuple, Union, cast
from typing import Generator, NamedTuple, Optional, Union, cast
from urllib.request import urlopen, urlretrieve

PYPI_INSTANCE = "https://pypi.org/pypi"
Expand Down Expand Up @@ -54,7 +54,7 @@ def get_pypi_download_url(package: str, version: Optional[str]) -> str:
return cast(str, source["url"])


def get_top_packages() -> List[str]:
def get_top_packages() -> list[str]:
with urlopen(PYPI_TOP_PACKAGES) as page:
result = json.load(page)

Expand Down Expand Up @@ -150,7 +150,7 @@ def git_switch_branch(
subprocess.run(args, cwd=repo)


def init_repos(options: Namespace) -> Tuple[Path, ...]:
def init_repos(options: Namespace) -> tuple[Path, ...]:
options.output.mkdir(exist_ok=True)

if options.top_packages:
Expand Down Expand Up @@ -206,7 +206,7 @@ def format_repo_with_version(
git_switch_branch(black_version.version, repo=black_repo)
git_switch_branch(current_branch, repo=repo, new=True, from_branch=from_branch)

format_cmd: List[Union[Path, str]] = [
format_cmd: list[Union[Path, str]] = [
black_runner(black_version.version, black_repo),
(black_repo / "black.py").resolve(),
".",
Expand All @@ -222,7 +222,7 @@ def format_repo_with_version(
return current_branch


def format_repos(repos: Tuple[Path, ...], options: Namespace) -> None:
def format_repos(repos: tuple[Path, ...], options: Namespace) -> None:
black_versions = tuple(
BlackVersion(*version.split(":")) for version in options.versions
)
Expand Down
8 changes: 4 additions & 4 deletions scripts/make_width_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@

import sys
from os.path import basename, dirname, join
from typing import Iterable, Tuple
from typing import Iterable

import wcwidth # type: ignore[import-not-found]


def make_width_table() -> Iterable[Tuple[int, int, int]]:
def make_width_table() -> Iterable[tuple[int, int, int]]:
start_codepoint = -1
end_codepoint = -1
range_width = -2
Expand Down Expand Up @@ -53,9 +53,9 @@ def main() -> None:
f.write(f"""# Generated by {basename(__file__)}
# wcwidth {wcwidth.__version__}
# Unicode {wcwidth.list_versions()[-1]}
from typing import Final, List, Tuple
from typing import Final
WIDTH_TABLE: Final[List[Tuple[int, int, int]]] = [
WIDTH_TABLE: Final[list[tuple[int, int, int]]] = [
""")
for triple in make_width_table():
f.write(f" {triple!r},\n")
Expand Down
6 changes: 4 additions & 2 deletions src/black/handle_ipynb_magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,14 @@ def mask_cell(src: str) -> tuple[str, list[Replacement]]:
from IPython.core.inputtransformer2 import TransformerManager

transformer_manager = TransformerManager()
# A side effect of the following transformation is that it also removes any
# empty lines at the beginning of the cell.
transformed = transformer_manager.transform_cell(src)
transformed, cell_magic_replacements = replace_cell_magics(transformed)
replacements += cell_magic_replacements
transformed = transformer_manager.transform_cell(transformed)
transformed, magic_replacements = replace_magics(transformed)
if len(transformed.splitlines()) != len(src.splitlines()):
if len(transformed.strip().splitlines()) != len(src.strip().splitlines()):
# Multi-line magic, not supported.
raise NothingChanged
replacements += magic_replacements
Expand Down Expand Up @@ -269,7 +271,7 @@ def replace_magics(src: str) -> tuple[str, list[Replacement]]:
magic_finder = MagicFinder()
magic_finder.visit(ast.parse(src))
new_srcs = []
for i, line in enumerate(src.splitlines(), start=1):
for i, line in enumerate(src.split("\n"), start=1):
if i in magic_finder.magics:
offsets_and_magics = magic_finder.magics[i]
if len(offsets_and_magics) != 1: # pragma: nocover
Expand Down
12 changes: 6 additions & 6 deletions tests/optional.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import logging
import re
from functools import lru_cache
from typing import TYPE_CHECKING, FrozenSet, List, Set
from typing import TYPE_CHECKING

import pytest

Expand Down Expand Up @@ -46,8 +46,8 @@
from _pytest.nodes import Node


ALL_POSSIBLE_OPTIONAL_MARKERS = StashKey[FrozenSet[str]]()
ENABLED_OPTIONAL_MARKERS = StashKey[FrozenSet[str]]()
ALL_POSSIBLE_OPTIONAL_MARKERS = StashKey[frozenset[str]]()
ENABLED_OPTIONAL_MARKERS = StashKey[frozenset[str]]()


def pytest_addoption(parser: "Parser") -> None:
Expand All @@ -69,7 +69,7 @@ def pytest_configure(config: "Config") -> None:
"""
ot_ini = config.inicfg.get("optional-tests") or []
ot_markers = set()
ot_run: Set[str] = set()
ot_run: set[str] = set()
if isinstance(ot_ini, str):
ot_ini = ot_ini.strip().split("\n")
marker_re = re.compile(r"^\s*(?P<no>no_)?(?P<marker>\w+)(:\s*(?P<description>.*))?")
Expand Down Expand Up @@ -103,7 +103,7 @@ def pytest_configure(config: "Config") -> None:
store[ENABLED_OPTIONAL_MARKERS] = frozenset(ot_run)


def pytest_collection_modifyitems(config: "Config", items: "List[Node]") -> None:
def pytest_collection_modifyitems(config: "Config", items: "list[Node]") -> None:
store = config._store
all_possible_optional_markers = store[ALL_POSSIBLE_OPTIONAL_MARKERS]
enabled_optional_markers = store[ENABLED_OPTIONAL_MARKERS]
Expand All @@ -120,7 +120,7 @@ def pytest_collection_modifyitems(config: "Config", items: "List[Node]") -> None


@lru_cache
def skip_mark(tests: FrozenSet[str]) -> "MarkDecorator":
def skip_mark(tests: frozenset[str]) -> "MarkDecorator":
names = ", ".join(sorted(tests))
return pytest.mark.skip(reason=f"Marked with disabled optional tests ({names})")

Expand Down
32 changes: 10 additions & 22 deletions tests/test_black.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,7 @@
from pathlib import Path, WindowsPath
from platform import system
from tempfile import TemporaryDirectory
from typing import (
Any,
Callable,
Dict,
Iterator,
List,
Optional,
Sequence,
Set,
Type,
TypeVar,
Union,
)
from typing import Any, Callable, Iterator, Optional, Sequence, TypeVar, Union
from unittest.mock import MagicMock, patch

import click
Expand Down Expand Up @@ -107,11 +95,11 @@ class FakeContext(click.Context):
"""A fake click Context for when calling functions that need it."""

def __init__(self) -> None:
self.default_map: Dict[str, Any] = {}
self.params: Dict[str, Any] = {}
self.default_map: dict[str, Any] = {}
self.params: dict[str, Any] = {}
self.command: click.Command = black.main
# Dummy root, since most of the tests don't care about it
self.obj: Dict[str, Any] = {"root": PROJECT_ROOT}
self.obj: dict[str, Any] = {"root": PROJECT_ROOT}


class FakeParameter(click.Parameter):
Expand All @@ -129,7 +117,7 @@ def __init__(self) -> None:


def invokeBlack(
args: List[str], exit_code: int = 0, ignore_config: bool = True
args: list[str], exit_code: int = 0, ignore_config: bool = True
) -> None:
runner = BlackRunner()
if ignore_config:
Expand Down Expand Up @@ -933,7 +921,7 @@ def test_get_features_used(self) -> None:
"with ((a, ((b as c)))): pass", {Feature.PARENTHESIZED_CONTEXT_MANAGERS}
)

def check_features_used(self, source: str, expected: Set[Feature]) -> None:
def check_features_used(self, source: str, expected: set[Feature]) -> None:
node = black.lib2to3_parse(source)
actual = black.get_features_used(node)
msg = f"Expected {expected} but got {actual} for {source!r}"
Expand Down Expand Up @@ -1365,7 +1353,7 @@ def test_reformat_one_with_stdin_empty(self) -> None:
]

def _new_wrapper(
output: io.StringIO, io_TextIOWrapper: Type[io.TextIOWrapper]
output: io.StringIO, io_TextIOWrapper: type[io.TextIOWrapper]
) -> Callable[[Any, Any], io.TextIOWrapper]:
def get_output(*args: Any, **kwargs: Any) -> io.TextIOWrapper:
if args == (sys.stdout.buffer,):
Expand Down Expand Up @@ -2350,7 +2338,7 @@ def test_read_cache_line_lengths(self) -> None:
def test_cache_key(self) -> None:
# Test that all members of the mode enum affect the cache key.
for field in fields(Mode):
values: List[Any]
values: list[Any]
if field.name == "target_versions":
values = [
{TargetVersion.PY312},
Expand Down Expand Up @@ -2463,7 +2451,7 @@ def test_gitignore_exclude(self) -> None:
gitignore = PathSpec.from_lines(
"gitwildmatch", ["exclude/", ".definitely_exclude"]
)
sources: List[Path] = []
sources: list[Path] = []
expected = [
Path(path / "b/dont_exclude/a.py"),
Path(path / "b/dont_exclude/a.pyi"),
Expand Down Expand Up @@ -2491,7 +2479,7 @@ def test_nested_gitignore(self) -> None:
exclude = re.compile(r"")
root_gitignore = black.files.get_gitignore(path)
report = black.Report()
expected: List[Path] = [
expected: list[Path] = [
Path(path / "x.py"),
Path(path / "root/b.py"),
Path(path / "root/c.py"),
Expand Down
4 changes: 2 additions & 2 deletions tests/test_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import re
from itertools import islice
from pathlib import Path
from typing import Optional, Sequence, Set
from typing import Optional, Sequence

import pytest

Expand All @@ -17,7 +17,7 @@


def check_feature_list(
lines: Sequence[str], expected_feature_names: Set[str], label: str
lines: Sequence[str], expected_feature_names: set[str], label: str
) -> Optional[str]:
start_index = lines.index(f"(labels/{label}-features)=\n")
if start_index == -1:
Expand Down
16 changes: 16 additions & 0 deletions tests/test_ipynb.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,22 @@ def test_cell_magic_with_magic() -> None:
assert result == expected


@pytest.mark.parametrize(
"src, expected",
(
("\n\n\n%time \n\n", "%time"),
(" \n\t\n%%timeit -n4 \t \nx=2 \n\r\n", "%%timeit -n4\nx = 2"),
(
" \t\n\n%%capture \nx=2 \n%config \n\n%env\n\t \n \n\n",
"%%capture\nx = 2\n%config\n\n%env",
),
),
)
def test_cell_magic_with_empty_lines(src: str, expected: str) -> None:
result = format_cell(src, fast=True, mode=JUPYTER_MODE)
assert result == expected


@pytest.mark.parametrize(
"mode, expected_output, expectation",
[
Expand Down
14 changes: 6 additions & 8 deletions tests/test_ranges.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Test the black.ranges module."""

from typing import List, Tuple

import pytest

from black.ranges import adjusted_lines, sanitized_lines
Expand All @@ -11,7 +9,7 @@
"lines",
[[(1, 1)], [(1, 3)], [(1, 1), (3, 4)]],
)
def test_no_diff(lines: List[Tuple[int, int]]) -> None:
def test_no_diff(lines: list[tuple[int, int]]) -> None:
source = """\
import re
Expand All @@ -32,7 +30,7 @@ def func():
[(0, 8), (3, 1)],
],
)
def test_invalid_lines(lines: List[Tuple[int, int]]) -> None:
def test_invalid_lines(lines: list[tuple[int, int]]) -> None:
original_source = """\
import re
def foo(arg):
Expand Down Expand Up @@ -83,7 +81,7 @@ def func(arg1, arg2, arg3):
],
)
def test_removals(
lines: List[Tuple[int, int]], adjusted: List[Tuple[int, int]]
lines: list[tuple[int, int]], adjusted: list[tuple[int, int]]
) -> None:
original_source = """\
1. first line
Expand Down Expand Up @@ -118,7 +116,7 @@ def test_removals(
],
)
def test_additions(
lines: List[Tuple[int, int]], adjusted: List[Tuple[int, int]]
lines: list[tuple[int, int]], adjusted: list[tuple[int, int]]
) -> None:
original_source = """\
1. first line
Expand Down Expand Up @@ -154,7 +152,7 @@ def test_additions(
([(9, 10), (1, 1)], [(1, 1), (9, 9)]),
],
)
def test_diffs(lines: List[Tuple[int, int]], adjusted: List[Tuple[int, int]]) -> None:
def test_diffs(lines: list[tuple[int, int]], adjusted: list[tuple[int, int]]) -> None:
original_source = """\
1. import re
2. def foo(arg):
Expand Down Expand Up @@ -231,7 +229,7 @@ def test_diffs(lines: List[Tuple[int, int]], adjusted: List[Tuple[int, int]]) ->
],
)
def test_sanitize(
lines: List[Tuple[int, int]], sanitized: List[Tuple[int, int]]
lines: list[tuple[int, int]], sanitized: list[tuple[int, int]]
) -> None:
source = """\
1. import re
Expand Down
7 changes: 3 additions & 4 deletions tests/test_tokenize.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import sys
import textwrap
from dataclasses import dataclass
from typing import List

import black
from blib2to3.pgen2 import token, tokenize
Expand All @@ -18,10 +17,10 @@ class Token:
end: tokenize.Coord


def get_tokens(text: str) -> List[Token]:
def get_tokens(text: str) -> list[Token]:
"""Return the tokens produced by the tokenizer."""
readline = io.StringIO(text).readline
tokens: List[Token] = []
tokens: list[Token] = []

def tokeneater(
type: int, string: str, start: tokenize.Coord, end: tokenize.Coord, line: str
Expand All @@ -32,7 +31,7 @@ def tokeneater(
return tokens


def assert_tokenizes(text: str, tokens: List[Token]) -> None:
def assert_tokenizes(text: str, tokens: list[Token]) -> None:
"""Assert that the tokenizer produces the expected tokens."""
actual_tokens = get_tokens(text)
assert actual_tokens == tokens
Expand Down
4 changes: 1 addition & 3 deletions tests/test_trans.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from typing import List, Tuple

from black.trans import iter_fexpr_spans


def test_fexpr_spans() -> None:
def check(
string: str, expected_spans: List[Tuple[int, int]], expected_slices: List[str]
string: str, expected_spans: list[tuple[int, int]], expected_slices: list[str]
) -> None:
spans = list(iter_fexpr_spans(string))

Expand Down
Loading

0 comments on commit 21afa91

Please sign in to comment.