ronantakizawa/codereview-bench huggingface dataset:
Repository: psf/black
File: scripts/generate_schema.py (Python)

Code before:
                raise TypeError(msg)

        if param.multiple:
            result[name] = {"type": "array", "items": result[name]}

        result[name]["description"] = param.help

        if param.default is not None and not param.multiple:
            result[name]["default"] = param.default

    return result


@click.command(context_settings={"help_option_names": ["-h", "--help"]})
@click.option("--schemastore", is_flag=True, help="SchemaStore format")
@click.option("--outfile", type=click.File(mode="w"), help="Write to file")
def main(schemastore: bool, outfile: IO[str]) -> None:
    properties = generate_schema_from_click(black.main)
    del properties["line-ranges"]

    schema: dict[str, Any] = {
        "$schema": "http://json-schema.org/draft-07/schema#",
        "$id": (
            "https://github.com/psf/black/blob/main/black/resources/black.schema.json"
        ),
        "$comment": "black table in pyproject.toml",
        "type": "object",
        "additionalProperties": False,
        "properties": properties,
    }

    if schemastore:
        schema["$id"] = ("https://json.schemastore.org/partial-black.json",)
        schema["properties"]["enable-unstable-feature"]["items"] = {"type": "string"}

    print(json.dumps(schema, indent=2), file=outfile)


if __name__ == "__main__":
    main()


Review comment (suggestion):
```suggestion
        "$comment": "tool.black table in pyproject.toml",
```

Code after:
                raise TypeError(msg)

        if param.multiple:
            result[name] = {"type": "array", "items": result[name]}

        result[name]["description"] = param.help

        if param.default is not None and not param.multiple:
            result[name]["default"] = param.default

    return result


@click.command(context_settings={"help_option_names": ["-h", "--help"]})
@click.option("--schemastore", is_flag=True, help="SchemaStore format")
@click.option("--outfile", type=click.File(mode="w"), help="Write to file")
def main(schemastore: bool, outfile: IO[str]) -> None:
    properties = generate_schema_from_click(black.main)
    del properties["line-ranges"]

    schema: dict[str, Any] = {
        "$schema": "http://json-schema.org/draft-07/schema#",
        "$id": (
            "https://github.com/psf/black/blob/main/black/resources/black.schema.json"
        ),
        "$comment": "tool.black table in pyproject.toml",
        "type": "object",
        "additionalProperties": False,
        "properties": properties,
    }

    if schemastore:
        schema["$id"] = ("https://json.schemastore.org/partial-black.json",)
        # The precise list of unstable features may change frequently, so don't
        # bother putting it in SchemaStore
        schema["properties"]["enable-unstable-feature"]["items"] = {"type": "string"}

    print(json.dumps(schema, indent=2), file=outfile)


if __name__ == "__main__":
    main()
Repository: psf/black
File: src/black/schema.py (Python)

Code before:
import importlib.resources
import json
import sys
from typing import Any


def get_schema(tool_name: str = "cibuildwheel") -> Any:
    "Get the stored complete schema for black's settings."
    assert tool_name == "black", "Only black is supported."

    loc = "resources/black.schema.json"

    if sys.version_info < (3, 9):
        with importlib.resources.open_text("black", loc, encoding="utf-8") as f:
            return json.load(f)

    schema = importlib.resources.files("black").joinpath(loc)  # type: ignore[unreachable]
    with schema.open(encoding="utf-8") as f:
        return json.load(f)


Review comment (suggestion):
```suggestion
    """Get the stored complete schema for black's settings."""
```

Code after:
import importlib.resources
import json
import sys
from typing import Any


def get_schema(tool_name: str = "black") -> Any:
    """Get the stored complete schema for black's settings."""
    assert tool_name == "black", "Only black is supported."

    pkg = "black.resources"
    fname = "black.schema.json"

    if sys.version_info < (3, 9):
        with importlib.resources.open_text(pkg, fname, encoding="utf-8") as f:
            return json.load(f)

    schema = importlib.resources.files(pkg).joinpath(fname)  # type: ignore[unreachable]
    with schema.open(encoding="utf-8") as f:
        return json.load(f)
Repository: psf/black
File: src/black/schema.py (Python)

Code before:
import importlib.resources
import json
import sys
from typing import Any


def get_schema(tool_name: str = "cibuildwheel") -> Any:
    "Get the stored complete schema for black's settings."
    assert tool_name == "black", "Only black is supported."

    loc = "resources/black.schema.json"

    if sys.version_info < (3, 9):
        with importlib.resources.open_text("black", loc, encoding="utf-8") as f:
            return json.load(f)

    schema = importlib.resources.files("black").joinpath(loc)  # type: ignore[unreachable]
    with schema.open(encoding="utf-8") as f:
        return json.load(f)


Review comment (suggestion):
```suggestion
def get_schema(tool_name: str = "black") -> Any:
```

Code after:
import importlib.resources
import json
import sys
from typing import Any


def get_schema(tool_name: str = "black") -> Any:
    """Get the stored complete schema for black's settings."""
    assert tool_name == "black", "Only black is supported."

    pkg = "black.resources"
    fname = "black.schema.json"

    if sys.version_info < (3, 9):
        with importlib.resources.open_text(pkg, fname, encoding="utf-8") as f:
            return json.load(f)

    schema = importlib.resources.files(pkg).joinpath(fname)  # type: ignore[unreachable]
    with schema.open(encoding="utf-8") as f:
        return json.load(f)
Repository: psf/black
File: src/black/nodes.py (Python)

Code before:
        leaf.value = "("
    elif leaf.type == token.RPAR:
        leaf.value = ")"


def is_name_token(nl: NL) -> TypeGuard[Leaf]:
    return nl.type == token.NAME


def is_lpar_token(nl: NL) -> TypeGuard[Leaf]:
    return nl.type == token.LPAR


def is_rpar_token(nl: NL) -> TypeGuard[Leaf]:
    return nl.type == token.RPAR


def is_string_token(nl: NL) -> TypeGuard[Leaf]:
    return nl.type == token.STRING


def is_number_token(nl: NL) -> TypeGuard[Leaf]:
    return nl.type == token.NUMBER


def get_annotation_type(leaf: Leaf) -> Optional[Literal["return", "param"]]:
    """Returns the type of annotation this leaf is part of, if any."""
    ancestor = leaf.parent
    while ancestor is not None:
        if ancestor.prev_sibling and ancestor.prev_sibling.type == token.RARROW:
            return "return"
        if ancestor.parent and ancestor.parent.type == syms.tname:
            return "param"
        ancestor = ancestor.parent
    return None


def is_part_of_annotation(leaf: Leaf) -> bool:
    """Returns whether this leaf is part of a type annotation."""
    return get_annotation_type(leaf) is not None


def first_leaf(node: LN) -> Optional[Leaf]:
    """Returns the first leaf of the ancestor node."""
    if isinstance(node, Leaf):
        return node
    elif not node.children:
        return None
    else:
        return first_leaf(node.children[0])


Review comment (suggestion):
```suggestion
def get_annotation_type(leaf: Leaf) -> Literal["return", "param", None]:
```

Code after:
        leaf.value = "("
    elif leaf.type == token.RPAR:
        leaf.value = ")"


def is_name_token(nl: NL) -> TypeGuard[Leaf]:
    return nl.type == token.NAME


def is_lpar_token(nl: NL) -> TypeGuard[Leaf]:
    return nl.type == token.LPAR


def is_rpar_token(nl: NL) -> TypeGuard[Leaf]:
    return nl.type == token.RPAR


def is_string_token(nl: NL) -> TypeGuard[Leaf]:
    return nl.type == token.STRING


def is_number_token(nl: NL) -> TypeGuard[Leaf]:
    return nl.type == token.NUMBER


def get_annotation_type(leaf: Leaf) -> Literal["return", "param", None]:
    """Returns the type of annotation this leaf is part of, if any."""
    ancestor = leaf.parent
    while ancestor is not None:
        if ancestor.prev_sibling and ancestor.prev_sibling.type == token.RARROW:
            return "return"
        if ancestor.parent and ancestor.parent.type == syms.tname:
            return "param"
        ancestor = ancestor.parent
    return None


def is_part_of_annotation(leaf: Leaf) -> bool:
    """Returns whether this leaf is part of a type annotation."""
    return get_annotation_type(leaf) is not None


def first_leaf(node: LN) -> Optional[Leaf]:
    """Returns the first leaf of the ancestor node."""
    if isinstance(node, Leaf):
        return node
    elif not node.children:
        return None
    else:
        return first_leaf(node.children[0])
Repository: psf/black
File: tests/data/cases/pattern_matching_with_if_stmt.py (Python)

Code before:
# flags: --preview --minimum-version=3.10
match "test":
    case "test" if "first long condition" != "some loooooooooooooooooooooooooooooooooooooog condition":
        print("Test")

match match:
    case "test" if case != "not very loooooooooooooog condition":
        print("No format change")

match "test":
    case "test" if "any long condition" != "another long condition" and "this is a long condition":
        print("Test")

match "test":
    case "test" if "any long condition" != "another long condition" and "this is a looooong condition":
        print("Test")

# case black_test_patma_052 (originally in the pattern_matching_complex test case)
match x:
    case [1, 0] if x := x[:0]:
        y = 1
    case [1, 0] if (x := x[:0]):
        y = 1

# output

match "test":

Review comment (suggestion):
Can you add some test cases where there are comments inside existing parentheses? That often causes us grief. Something like this:

```
match match:
    case case if (True # some comment
    ):
        pass
```

Code after:
# flags: --preview --minimum-version=3.10
match match:
    case "test" if case != "not very loooooooooooooog condition":  # comment
        pass

match smth:
    case "test" if "any long condition" != "another long condition" and "this is a long condition":
        pass
    case test if "any long condition" != "another long condition" and "this is a looooong condition":
        pass
    case test if "any long condition" != "another long condition" and "this is a looooong condition":  # some additional comments
        pass
    case test if (True): # some comment
        pass
    case test if (False
        ): # some comment
        pass
    case test if (True  # some comment
        ):
        pass  # some comment
    case cases if (True  # some comment
                   ): # some other comment
        pass  # some comment
    case match if (True  # some comment
                   ):
        pass  # some comment
Repository: psf/black
File: src/black/parsing.py (Python)

Code before:
            yield from _stringify_ast_with_new_parent(value, parent_stack, node)

        else:
            normalized: object
            if (
                isinstance(node, ast.Constant)
                and field == "value"
                and isinstance(value, str)
                and len(parent_stack) >= 2
                and isinstance(parent_stack[-1], ast.Expr)
                and isinstance(
                    parent_stack[-2],
                    (ast.FunctionDef, ast.AsyncFunctionDef, ast.Module, ast.ClassDef),
                )
            ):
                # Constant strings may be indented across newlines, if they are
                # docstrings; fold spaces after newlines when comparing. Similarly,
                # trailing and leading space may be removed.
                normalized = _normalize("\n", value)
            elif field == "type_comment" and isinstance(value, str):
                # Trailing whitespace in type comments is removed.
                normalized = value.rstrip()
            else:
                normalized = value
            yield (
                f"{'    ' * (len(parent_stack) + 2)}{normalized!r},  #"
                f" {value.__class__.__name__}"
            )

    yield f"{'    ' * len(parent_stack)})  # /{node.__class__.__name__}"


Review comment (suggestion):
nit: iiuc
```suggestion
                f"{'    ' * (len(parent_stack) + 1)}{normalized!r},  #"
```

Code after:
            yield from _stringify_ast_with_new_parent(value, parent_stack, node)

        else:
            normalized: object
            if (
                isinstance(node, ast.Constant)
                and field == "value"
                and isinstance(value, str)
                and len(parent_stack) >= 2
                and isinstance(parent_stack[-1], ast.Expr)
                and isinstance(
                    parent_stack[-2],
                    (ast.FunctionDef, ast.AsyncFunctionDef, ast.Module, ast.ClassDef),
                )
            ):
                # Constant strings may be indented across newlines, if they are
                # docstrings; fold spaces after newlines when comparing. Similarly,
                # trailing and leading space may be removed.
                normalized = _normalize("\n", value)
            elif field == "type_comment" and isinstance(value, str):
                # Trailing whitespace in type comments is removed.
                normalized = value.rstrip()
            else:
                normalized = value
            yield (
                f"{'    ' * (len(parent_stack) + 1)}{normalized!r},  #"
                f" {value.__class__.__name__}"
            )

    yield f"{'    ' * len(parent_stack)})  # /{node.__class__.__name__}"
Repository: psf/black
File: tests/data/cases/preview_remove_lone_list_item_parens.py (Python)

Code before:
# flags: --unstable
items = [(123)]
items = [(True)]
items = [(((((True)))))]
items = [(((((True,)))))]
items = [((((()))))]

items = [
    (
        {"key1": "val1", "key2": "val2", "key3": "val3"}
        if some_var == "long strings"
        else {"key": "val"}
    )
]
items = [
    (
        {"key1": "val1", "key2": "val2", "key3": "val3"}
        if some_var == "long strings"
        else {"key": "val"}
    ),
]
items = [
    {"key1": "val1", "key2": "val2", "key3": "val3"}
    if some_var == "long strings"
    else {"key": "val"}
]
items = [

Review comment (bug):
Add a test containing a generator expression, to make sure we don't turn it into a listcomp:

```
>>> [(x for x in [1])]
[<generator object <genexpr> at 0x104277400>]
>>> [x for x in [1]]
[1]
```

Also add tests with comments in various places (e.g., after the opening paren, before the closing paren). Such cases tend to expose bugs in Black.

Code after:
# flags: --preview
items = [(123)]
items = [(True)]
items = [(((((True)))))]
items = [(((((True,)))))]
items = [((((()))))]
items = [(x for x in [1])]
items = {(123)}
items = {(True)}
items = {(((((True)))))}

# Requires `hug_parens_with_braces_and_square_brackets` unstable style to remove parentheses
# around multiline values
items = [
    (
        {"key1": "val1", "key2": "val2", "key3": "val3"}
        if some_var == "long strings"
        else {"key": "val"}
    )
]
items = [
    (
        {"key1": "val1", "key2": "val2"}
        if some_var == ""
        else {"key": "val"}
    )
]
Repository: psf/black
File: tests/test_schema.py (Python)

Code before:
import importlib.metadata
import sys


def test_schema_entrypoint() -> None:
    if sys.version_info < (3, 10):
        eps = importlib.metadata.entry_points()["validate_pyproject.tool_schema"]
        (black_ep,) = (ep for ep in eps if ep.name == "black")
    else:
        (black_ep,) = importlib.metadata.entry_points(
            group="validate_pyproject.tool_schema", name="black"
        )

    black_fn = black_ep.load()
    schema = black_fn()
    assert schema == black_fn("black")
    assert schema["properties"]["line-length"]["type"] == "integer"


Review comment (performance):
I know pyupgrade does this but I don't like this change. It feels harder to understand (generator expressions are rarer than listcomps). The genexp might sometimes be faster, but it's unlikely to matter.

In this case, the new code appears to be slower for me (benchmark on Linux on 3.9):

```
In [8]: eps = importlib.metadata.entry_points()["validate_pyproject.tool_schema"]

In [9]: eps
Out[9]: (EntryPoint(name='black', value='black.schema:get_schema', group='validate_pyproject.tool_schema'),)

In [10]: %timeit (black_ep,) = [ep for ep in eps if ep.name == "black"]
170 ns ± 1.32 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

In [11]: %timeit (black_ep,) = (ep for ep in eps if ep.name == "black")
232 ns ± 1.61 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
```

Code after:
import importlib.metadata
import sys


def test_schema_entrypoint() -> None:
    if sys.version_info < (3, 10):
        eps = importlib.metadata.entry_points()["validate_pyproject.tool_schema"]
        (black_ep,) = [ep for ep in eps if ep.name == "black"]
    else:
        (black_ep,) = importlib.metadata.entry_points(
            group="validate_pyproject.tool_schema", name="black"
        )

    black_fn = black_ep.load()
    schema = black_fn()
    assert schema == black_fn("black")
    assert schema["properties"]["line-length"]["type"] == "integer"
Repository: psf/black
File: docs/conf.py (Python)

Code before:
import re
import string
from importlib.metadata import version
from pathlib import Path

from sphinx.application import Sphinx

CURRENT_DIR = Path(__file__).parent


def make_pypi_svg(version: str) -> None:
    template: Path = CURRENT_DIR / "_static" / "pypi_template.svg"
    target: Path = CURRENT_DIR / "_static" / "pypi.svg"
    with open(str(template), encoding="utf8") as f:
        svg: str = string.Template(f.read()).substitute(version=version)
    with open(str(target), "w", encoding="utf8") as f:
        f.write(svg)


def replace_pr_numbers_with_links(content: str) -> str:
    """Replaces all PR numbers with the corresponding GitHub link."""

    base_url = "https://github.com/psf/black/pull/"
    pr_num_regex = re.compile(r"\(#(\d+)\)")

    def num_to_link(match: re.Match) -> str:
        full_match, number = match.group(0, 1)
        url = f"{base_url}{number}"
        return f"[{full_match}]({url})"

    return pr_num_regex.sub(num_to_link, content)


def handle_include_read(
        app: Sphinx,
        relative_path: Path,
        parent_docname: str,
        content: list[str],
) -> None:
    """Handler for the include-read sphinx event."""
    if parent_docname == "change_log":
        content[0] = replace_pr_numbers_with_links(content[0])


def setup(app: Sphinx) -> None:
    """Sets up a minimal sphinx extension."""
    app.connect("include-read", handle_include_read)


# Necessary so Click doesn't hit an encode error when called by
# sphinxcontrib-programoutput on Windows.

Review comment (suggestion):
```suggestion
    def num_to_link(match: re.Match[str]) -> str:
```

Code after:
import re
import string
from importlib.metadata import version
from pathlib import Path

from sphinx.application import Sphinx

CURRENT_DIR = Path(__file__).parent


def make_pypi_svg(version: str) -> None:
    template: Path = CURRENT_DIR / "_static" / "pypi_template.svg"
    target: Path = CURRENT_DIR / "_static" / "pypi.svg"
    with open(str(template), encoding="utf8") as f:
        svg: str = string.Template(f.read()).substitute(version=version)
    with open(str(target), "w", encoding="utf8") as f:
        f.write(svg)


def replace_pr_numbers_with_links(content: str) -> str:
    """Replaces all PR numbers with the corresponding GitHub link."""

    base_url = "https://github.com/psf/black/pull/"
    pr_num_regex = re.compile(r"\(#(\d+)\)")

    def num_to_link(match: re.Match[str]) -> str:
        number = match.group(1)
        url = f"{base_url}{number}"
        return f"([#{number}]({url}))"

    return pr_num_regex.sub(num_to_link, content)


def handle_include_read(
    app: Sphinx,
    relative_path: Path,
    parent_docname: str,
    content: list[str],
) -> None:
    """Handler for the include-read sphinx event."""
    if parent_docname == "change_log":
        content[0] = replace_pr_numbers_with_links(content[0])


def setup(app: Sphinx) -> None:
    """Sets up a minimal sphinx extension."""
    app.connect("include-read", handle_include_read)


# Necessary so Click doesn't hit an encode error when called by
# sphinxcontrib-programoutput on Windows.
Repository: psf/black
File: tests/data/cases/type_params.py (Python)

Code before:
# flags: --minimum-version=3.12
def func  [T ](): pass
async def func [ T ] (): pass
class C[ T ] : pass

def all_in[T   :   int,U : (bytes, str),*   Ts,**P](): pass

def really_long[WhatIsTheLongestTypeVarNameYouCanThinkOfEnoughToMakeBlackSplitThisLine](): pass

def even_longer[WhatIsTheLongestTypeVarNameYouCanThinkOfEnoughToMakeBlackSplitThisLine: WhatIfItHadABound](): pass

def it_gets_worse[WhatIsTheLongestTypeVarNameYouCanThinkOfEnoughToMakeBlackSplitThisLine, ItCouldBeGenericOverMultipleTypeVars](): pass

def magic[Trailing, Comma,](): pass

class Foo:
    def foo(self):
        if True:
            content_ids: Mapping[
                str, Optional[ContentId]
            ] = self.publisher_content_store.store_config_contents(files)

# output


def func[T]():
    pass


async def func[T]():
    pass


class C[T]:
    pass


def all_in[T: int, U: (bytes, str), *Ts, **P]():
    pass



Review comment (suggestion):
This file is meant for PEP 695 syntax, which your new test doesn't use. Therefore, it's also run in 3.12+ mode. I couldn't find a better file to put it, so I'd suggest adding a new test file, maybe `annotation.py`.

Code after:
# flags: --minimum-version=3.12
def func  [T ](): pass
async def func [ T ] (): pass
class C[ T ] : pass

def all_in[T   :   int,U : (bytes, str),*   Ts,**P](): pass

def really_long[WhatIsTheLongestTypeVarNameYouCanThinkOfEnoughToMakeBlackSplitThisLine](): pass

def even_longer[WhatIsTheLongestTypeVarNameYouCanThinkOfEnoughToMakeBlackSplitThisLine: WhatIfItHadABound](): pass

def it_gets_worse[WhatIsTheLongestTypeVarNameYouCanThinkOfEnoughToMakeBlackSplitThisLine, ItCouldBeGenericOverMultipleTypeVars](): pass

def magic[Trailing, Comma,](): pass

# output


def func[T]():
    pass


async def func[T]():
    pass


class C[T]:
    pass


def all_in[T: int, U: (bytes, str), *Ts, **P]():
    pass


def really_long[
    WhatIsTheLongestTypeVarNameYouCanThinkOfEnoughToMakeBlackSplitThisLine
]():
    pass


def even_longer[