ronantakizawa/codereview-bench huggingface dataset:
Repository: pytest-dev/pytest
File: testing/python/raises.py (Python)

Code before:
        with pytest.raises(
            TypeError,
            match=wrap_escape(
                "expected exception must be a BaseException type, not 'str'"
            ),
        ):
            with pytest.raises(("hello", NotAnException)):  # type: ignore[arg-type]
                pass  # pragma: no cover

    def test_issue_11872(self) -> None:
        """Regression test for #11872.

        urllib.error.HTTPError on Python<=3.9 raises KeyError instead of
        AttributeError on invalid attribute access.

        https://github.com/python/cpython/issues/98778
        """
        from email.message import Message
        from urllib.error import HTTPError

        with pytest.raises(HTTPError, match="Not Found") as exc_info:
            raise HTTPError(
                code=404, msg="Not Found", fp=io.BytesIO(), hdrs=Message(), url=""
            )
        with exc_info.value:
            pass


Review comment (suggestion):
```suggestion
        exc_info.value.close()  # avoid a resource warning
```

Code after:
        with pytest.raises(
            TypeError,
            match=wrap_escape(
                "expected exception must be a BaseException type, not 'str'"
            ),
        ):
            with pytest.raises(("hello", NotAnException)):  # type: ignore[arg-type]
                pass  # pragma: no cover

    def test_issue_11872(self) -> None:
        """Regression test for #11872.

        urllib.error.HTTPError on Python<=3.9 raises KeyError instead of
        AttributeError on invalid attribute access.

        https://github.com/python/cpython/issues/98778
        """
        from email.message import Message
        from urllib.error import HTTPError

        with pytest.raises(HTTPError, match="Not Found") as exc_info:
            raise HTTPError(
                code=404, msg="Not Found", fp=io.BytesIO(), hdrs=Message(), url=""
            )
        exc_info.value.close()  # avoid a resource warning
Repository: pytest-dev/pytest
File: src/_pytest/outcomes.py (Python)

Code before:
        self._use_item_location = _use_item_location


class Failed(OutcomeException):
    """Raised from an explicit call to pytest.fail()."""

    __module__ = "builtins"


class Exit(Exception):
    """Raised for immediate program exits (no tracebacks/summaries)."""

    def __init__(
        self, msg: str = "unknown reason", returncode: int | None = None
    ) -> None:
        self.msg = msg
        self.returncode = returncode
        super().__init__(msg)


class XFailed(Failed):
    """Raised from an explicit call to pytest.xfail()."""


class _Exit:
    Exception: type[Exit] = Exit

    def __call__(self, reason: str = "", returncode: int | None = None) -> NoReturn:
        """Exit testing process.

        :param reason:
            The message to show as the reason for exiting pytest.  reason has a default value
            only because `msg` is deprecated.

        :param returncode:
            Return code to be used when exiting pytest. None means the same as ``0`` (no error),
            same as :func:`sys.exit`.

        :raises pytest.exit.Exception:
            The exception that is raised.
        """
        __tracebackhide__ = True
        raise Exit(msg=reason, returncode=returncode)


class _Skip:
    Exception: type[Skipped] = Skipped

    def __call__(self, reason: str = "", allow_module_level: bool = False) -> NoReturn:
        """Skip an executing test with the given message.


Review comment (suggestion):
Usually we omit an explicit annotation when it is inferred.

Also, here I think a `ClassVar` annotation would be appropriate. So `Exception: ClassVar = Exit`.

Code after:
        # instead of the place that raises the exception/calls skip().
        self._use_item_location = _use_item_location


class Failed(OutcomeException):
    """Raised from an explicit call to pytest.fail()."""

    __module__ = "builtins"


class Exit(Exception):
    """Raised for immediate program exits (no tracebacks/summaries)."""

    def __init__(
        self, msg: str = "unknown reason", returncode: int | None = None
    ) -> None:
        self.msg = msg
        self.returncode = returncode
        super().__init__(msg)


class XFailed(Failed):
    """Raised from an explicit call to pytest.xfail()."""


class _Exit:
    """Exit testing process.

    :param reason:
        The message to show as the reason for exiting pytest.  reason has a default value
        only because `msg` is deprecated.

    :param returncode:
        Return code to be used when exiting pytest. None means the same as ``0`` (no error),
        same as :func:`sys.exit`.

    :raises pytest.exit.Exception:
        The exception that is raised.
    """

    Exception: ClassVar[type[Exit]] = Exit

    def __call__(self, reason: str = "", returncode: int | None = None) -> NoReturn:
        __tracebackhide__ = True
        raise Exit(msg=reason, returncode=returncode)


exit: _Exit = _Exit()


class _Skip:
Repository: pytest-dev/pytest
File: src/_pytest/compat.py (Python)

Code before:
#             return 2
#         else:
#             raise Exception('unreachable')
#
# Now suppose we add a new variant:
#
#     MyUnion = Union[int, str, bytes]
#
# After doing this, we must remember ourselves to go and update the handle
# function to handle the new variant.
#
# With `assert_never` we can do better:
#
#     // raise Exception('unreachable')
#     return assert_never(x)
#
# Now, if we forget to handle the new variant, the type-checker will emit a
# compile-time error, instead of the runtime error we would have gotten
# previously.
#
# This also work for Enums (if you use `is` to compare) and Literals.
def assert_never(value: NoReturn) -> NoReturn:
    assert False, f"Unhandled value: {value} ({type(value).__name__})"


class CallableBool:
    def __init__(self, value: bool) -> None:
        self._value = value

    def __bool__(self) -> bool:
        return self._value

    def __call__(self) -> bool:
        return self._value


Review comment (suggestion):
```suggestion
class CallableBool:
    """
    A bool-like object that can also be callable, returning its true/false value.
    
    Used for backward compatibility in cases where something was supposed to be a method
    but was implemented as a simple attribute by mistake (see `TerminalReporter.isatty`).
    
    Do not use in new code.
    """ 
```

Code after:
#             return 2
#         else:
#             raise Exception('unreachable')
#
# Now suppose we add a new variant:
#
#     MyUnion = Union[int, str, bytes]
#
# After doing this, we must remember ourselves to go and update the handle
# function to handle the new variant.
#
# With `assert_never` we can do better:
#
#     // raise Exception('unreachable')
#     return assert_never(x)
#
# Now, if we forget to handle the new variant, the type-checker will emit a
# compile-time error, instead of the runtime error we would have gotten
# previously.
#
# This also work for Enums (if you use `is` to compare) and Literals.
def assert_never(value: NoReturn) -> NoReturn:
    assert False, f"Unhandled value: {value} ({type(value).__name__})"


class CallableBool:
    """
    A bool-like object that can also be called, returning its true/false value.

    Used for backwards compatibility in cases where something was supposed to be a method
    but was implemented as a simple attribute by mistake (see `TerminalReporter.isatty`).

    Do not use in new code.
    """

    def __init__(self, value: bool) -> None:
        self._value = value

    def __bool__(self) -> bool:
        return self._value

    def __call__(self) -> bool:
        return self._value
Repository: pytest-dev/pytest
File: testing/test_faulthandler.py (Python)

Code before:
def test_disabled(pytester: Pytester) -> None:
    """Test option to disable fault handler in the command line."""
    pytester.makepyfile(
        """
    import faulthandler
    def test_disabled():
        assert not faulthandler.is_enabled()
    """
    )
    result = pytester.runpytest_subprocess("-p", "no:faulthandler")
    result.stdout.fnmatch_lines(["*1 passed*"])
    assert result.ret == 0


@pytest.mark.keep_ci_var
@pytest.mark.parametrize(
    "enabled",
    [
        pytest.param(
            True,
            marks=pytest.mark.skipif(
                "CI" in os.environ, reason="sometimes crashes on CI (#7022)"
            ),
        ),
        False,
    ],
)
def test_timeout(pytester: Pytester, enabled: bool) -> None:
    """Test option to dump tracebacks after a certain timeout.

    If faulthandler is disabled, no traceback will be dumped.
    """
    pytester.makepyfile(
        """
    import os, time
    def test_timeout():
        time.sleep(1 if "CI" in os.environ else 0.1)
    """
    )
    pytester.makeini(
        """
        [pytest]
        faulthandler_timeout = 0.01
        """
    )
    args = ["-p", "no:faulthandler"] if not enabled else []

    result = pytester.runpytest_subprocess(*args)
    tb_output = "most recent call first"
    if enabled:
        result.stderr.fnmatch_lines([f"*{tb_output}*"])

Review comment (suggestion):
I changed the skip mark to a conditional mark to make it easy to check that this test actually passes locally.

Code after:
def test_disabled(pytester: Pytester) -> None:
    """Test option to disable fault handler in the command line."""
    pytester.makepyfile(
        """
    import faulthandler
    def test_disabled():
        assert not faulthandler.is_enabled()
    """
    )
    result = pytester.runpytest_subprocess("-p", "no:faulthandler")
    result.stdout.fnmatch_lines(["*1 passed*"])
    assert result.ret == 0


@pytest.mark.keep_ci_var
@pytest.mark.parametrize(
    "enabled",
    [
        pytest.param(
            True,
            marks=pytest.mark.skipif(
                "CI" in os.environ
                and sys.platform == "linux"
                and sys.version_info >= (3, 14),
                reason="sometimes crashes on CI because of truncated outputs (#7022)",
            ),
        ),
        False,
    ],
)
def test_timeout(pytester: Pytester, enabled: bool) -> None:
    """Test option to dump tracebacks after a certain timeout.

    If faulthandler is disabled, no traceback will be dumped.
    """
    pytester.makepyfile(
        """
    import os, time
    def test_timeout():
        time.sleep(1 if "CI" in os.environ else 0.1)
    """
    )
    pytester.makeini(
        """
        [pytest]
        faulthandler_timeout = 0.01
        """
    )
    args = ["-p", "no:faulthandler"] if not enabled else []

    result = pytester.runpytest_subprocess(*args)
Repository: pytest-dev/pytest
File: testing/test_warnings.py (Python)

Code before:
    else:
        result.stdout.fnmatch_lines(["* 1 failed in*"])


def test_option_precedence_mark(pytester: Pytester) -> None:
    """Filters defined by marks should always take precedence (#3946)."""
    pytester.makeini(
        """
        [pytest]
        filterwarnings = ignore
    """
    )
    pytester.makepyfile(
        """
        import pytest, warnings
        @pytest.mark.filterwarnings('error')
        def test():
            warnings.warn(UserWarning('hello'))
    """
    )
    result = pytester.runpytest("-W", "ignore")
    result.stdout.fnmatch_lines(["* 1 failed in*"])


def test_accept_unknown_category(pytester: Pytester) -> None:
    """Filters defined by marks should always take precedence (#3946)."""
    pytester.makeini(
        """
        [pytest]
        filterwarnings =
            ignore:Failed to import filter module.*:pytest.PytestConfigWarning
            ignore::foobar.Foobar
    """
    )
    pytester.makepyfile(
        """
        def test():
            pass
    """
    )
    result = pytester.runpytest("-W", "ignore::bizbaz.Bizbaz")
    result.stdout.fnmatch_lines(["* 1 passed in*"])


class TestDeprecationWarningsByDefault:
    """
    Note: all pytest runs are executed in a subprocess so we don't inherit warning filters
    from pytest's own test suite
    """

    def create_file(self, pytester: Pytester, mark="") -> None:

Review comment (suggestion):
#3946 is related, but I think it is better to use this PR for future reference.

```suggestion
    """Filters defined by marks should always take precedence (#13732)."""
```

Code after:
    else:
        result.stdout.fnmatch_lines(["* 1 failed in*"])


def test_option_precedence_mark(pytester: Pytester) -> None:
    """Filters defined by marks should always take precedence (#3946)."""
    pytester.makeini(
        """
        [pytest]
        filterwarnings = ignore
    """
    )
    pytester.makepyfile(
        """
        import pytest, warnings
        @pytest.mark.filterwarnings('error')
        def test():
            warnings.warn(UserWarning('hello'))
    """
    )
    result = pytester.runpytest("-W", "ignore")
    result.stdout.fnmatch_lines(["* 1 failed in*"])


def test_accept_unknown_category(pytester: Pytester) -> None:
    """Category types that can't be imported don't cause failure (#13732)."""
    pytester.makeini(
        """
        [pytest]
        filterwarnings =
            always:Failed to import filter module.*:pytest.PytestConfigWarning
            ignore::foobar.Foobar
    """
    )
    pytester.makepyfile(
        """
        def test():
            pass
    """
    )
    result = pytester.runpytest("-W", "ignore::bizbaz.Bizbaz")
    result.stdout.fnmatch_lines(
        [
            f"*== {WARNINGS_SUMMARY_HEADER} ==*",
            "*PytestConfigWarning: Failed to import filter module 'foobar': ignore::foobar.Foobar",
            "*PytestConfigWarning: Failed to import filter module 'bizbaz': ignore::bizbaz.Bizbaz",
            "* 1 passed, * warning*",
        ]
    )
Repository: pytest-dev/pytest
File: testing/test_collection.py (Python)

Code before:
    result = pytester.runpytest("--collect-only", "--pyargs", "pkg.subpkg_regular")
    result.stdout.fnmatch_lines(
        [
            "collected 1 item",
            "<Dir pkg>",
            "  <Package subpkg_regular>",
            "    <Module module3.py>",
            "      <Function test_module3>",
        ]
    )


@pytest.mark.parametrize(
    "parametrize_args, expected_indexs",
    [
        ("[1, 1, 3]", "{'1': [[]0, 1[]]}"),
        ("[1, 1, 3, 2, 2]", "{'1': [[]0, 1[]], '2': [[]3, 4[]]}"),
    ],
)
def test_option_parametrize_require_unique_paramset_ids(
    pytester: Pytester, parametrize_args, expected_indexs
) -> None:
    pytester.makepyfile(
        f"""
        import pytest
        @pytest.mark.parametrize('y', {parametrize_args})
        def test1(y):
            pass
    """
    )
    result = pytester.runpytest("--require-unique-paramset-ids")
    result.stdout.fnmatch_lines(
        [
            "E*Because: require_unique_parameterset_ids is set, pytest won't",
            "E*attempt to generate unique IDs for parameter sets.",
            "E*argument names: [[]'y'[]]",
            "E*function name: test1",
            "E*test name: test_option_parametrize_require_unique_paramset_ids.py::test1",
            f"E*duplicates: {expected_indexs!s}",
        ]
    )


Review comment (refactor):
Could we use another parametrized argument here *without* duplicates to ensure the message handles it correctly?

Code after:
    )

    result = pytester.runpytest("--collect-only", "--pyargs", "pkg.subpkg_regular")
    result.stdout.fnmatch_lines(
        [
            "collected 1 item",
            "<Dir pkg>",
            "  <Package subpkg_regular>",
            "    <Module module3.py>",
            "      <Function test_module3>",
        ]
    )


class TestOverlappingCollectionArguments:
    """Test that overlapping collection arguments (e.g. `pytest a/b a
    a/c::TestIt) are handled correctly (#12083)."""

    @pytest.mark.parametrize("args", [("a", "a/b"), ("a/b", "a")])
    def test_parent_child(self, pytester: Pytester, args: tuple[str, ...]) -> None:
        """Test that 'pytest a a/b' and `pytest a/b a` collects all tests from 'a'."""
        pytester.makepyfile(
            **{
                "a/test_a.py": """
                    def test_a1(): pass
                    def test_a2(): pass
                """,
                "a/b/test_b.py": """
                    def test_b1(): pass
                    def test_b2(): pass
                """,
            }
        )

        result = pytester.runpytest("--collect-only", *args)

        result.stdout.fnmatch_lines(
            [
                "<Dir *>",
                "  <Dir a>",
                "    <Dir b>",
                "      <Module test_b.py>",
                "        <Function test_b1>",
                "        <Function test_b2>",
                "    <Module test_a.py>",
                "      <Function test_a1>",
                "      <Function test_a2>",
                "",
            ],
            consecutive=True,
        )
Repository: pytest-dev/pytest
File: testing/test_collection.py (Python)

Code before:


@pytest.mark.parametrize(
    "parametrize_args, expected_indexs",
    [
        ("[(1, 1), (1, 1)]", {"1-1": [0, 1]}),
        ("[(1, 1), (1, 2), (1, 1)]", {"1-1": [0, 2]}),
        ("[(1, 1), (2, 2), (1, 1)]", {"1-1": [0, 2]}),
        ("[(1, 1), (2, 2), (1, 2), (2, 1), (1, 1)]", {"1-1": [0, 4]}),
    ],
)
def test_option_parametrize_require_unique_paramset_ids(
    pytester: Pytester, parametrize_args, expected_indexs
) -> None:
    pytester.makepyfile(
        f"""
        import pytest
        @pytest.mark.parametrize('y, x', {parametrize_args})
        def test1(y, x):
            pass
    """
    )
    result = pytester.runpytest("--require-unique-paramset-ids")
    result.stdout.fnmatch_lines(
        [
            "E*Because: require_unique_parameterset_ids is set, pytest won't",
            "E*attempt to generate unique IDs for parameter sets.",
            "E*argument names: [[]'y', 'x'[]]",
            "E*function name: test1",
            "E*test name: test_option_parametrize_require_unique_paramset_ids.py::test1",
            f"E*duplicates: {fnmatch_escape_repr(expected_indexs)}",
        ]
    )


def fnmatch_escape_repr(obj):
    return re.sub(r"[*?[\]]", (lambda m: f"[{m.group()}]"), repr(obj))


Review comment (suggestion):
```suggestion
            "E*Because --require-unique-paramset-ids given, pytest won't",
```

Code after:
    )


class TestOverlappingCollectionArguments:
    """Test that overlapping collection arguments (e.g. `pytest a/b a
    a/c::TestIt) are handled correctly (#12083)."""

    @pytest.mark.parametrize("args", [("a", "a/b"), ("a/b", "a")])
    def test_parent_child(self, pytester: Pytester, args: tuple[str, ...]) -> None:
        """Test that 'pytest a a/b' and `pytest a/b a` collects all tests from 'a'."""
        pytester.makepyfile(
            **{
                "a/test_a.py": """
                    def test_a1(): pass
                    def test_a2(): pass
                """,
                "a/b/test_b.py": """
                    def test_b1(): pass
                    def test_b2(): pass
                """,
            }
        )

        result = pytester.runpytest("--collect-only", *args)

        result.stdout.fnmatch_lines(
            [
                "<Dir *>",
                "  <Dir a>",
                "    <Dir b>",
                "      <Module test_b.py>",
                "        <Function test_b1>",
                "        <Function test_b2>",
                "    <Module test_a.py>",
                "      <Function test_a1>",
                "      <Function test_a2>",
                "",
            ],
            consecutive=True,
        )

    def test_multiple_nested_paths(self, pytester: Pytester) -> None:
        """Test that 'pytest a/b a a/b/c' collects all tests from 'a'."""
        pytester.makepyfile(
            **{
                "a/test_a.py": """
                    def test_a(): pass
                """,
                "a/b/test_b.py": """
                    def test_b(): pass
                """,
Repository: pytest-dev/pytest
File: testing/test_collection.py (Python)

Code before:
            """
        )
        result = pytester.runpytest("--require-unique-parameterization-ids")
        self._assert_duplicate_msg(result, expected_indices)

    @pytest.mark.parametrize("parametrize_args, expected_indices", CASES)
    def test_ini_enables(self, pytester: Pytester, parametrize_args, expected_indices):
        pytester.makeini(
            """
            [pytest]
            require_unique_parameterization_ids = true
            """
        )
        pytester.makepyfile(
            f"""
                    import pytest

                    @pytest.mark.parametrize('y, x', {parametrize_args})
                    def test1(y, x):
                        pass
                    """
        )
        result = pytester.runpytest()
        self._assert_duplicate_msg(result, expected_indices)

    def test_cli_overrides_ini_false(self, pytester: Pytester):
        """CLI True should override ini False."""
        pytester.makeini(
            """
            [pytest]
            require_unique_parameterization_ids = false
            """
        )
        pytester.makepyfile(
            """
            import pytest

            @pytest.mark.parametrize('y, x', [(1,1), (1,1)])
            def test1(y, x):
                pass
            """
        )
        result = pytester.runpytest("--require-unique-parameterization-ids")
        self._assert_duplicate_msg(result, {"1-1": [0, 1]})


Review comment (suggestion):
```suggestion
    def test_cli_overrides_ini_false(self, pytester: Pytester) -> None:
```

Code after:
        def test1(x, y):
            pass
        """
    )

    result = pytester.runpytest()

    assert result.ret == ExitCode.INTERRUPTED
    expected_parametersets = ", ".join(str(list(p)) for p in x_y)
    expected_ids = ", ".join(f"{x}-{y}" for x, y in x_y)
    result.stdout.fnmatch_lines(
        [
            "Duplicate parametrization IDs detected*",
            "",
            "Test name:      *::test1",
            "Parameters:     x, y",
            f"Parameter sets: {expected_parametersets}",
            f"IDs:            {expected_ids}",
            f"Duplicates:     {', '.join(expected_duplicates)}",
            "",
            "You can fix this problem using *",
        ]
    )


def test_strict_parametrization_ids_with_hidden_param(pytester: Pytester) -> None:
    pytester.makeini(
        """
        [pytest]
        strict_parametrization_ids = true
        """
    )
    pytester.makepyfile(
        """
        import pytest

        @pytest.mark.parametrize(["x"], ["a", pytest.param("a", id=pytest.HIDDEN_PARAM), "a"])
        def test1(x):
            pass
        """
    )

    result = pytester.runpytest()

    assert result.ret == ExitCode.INTERRUPTED
    result.stdout.fnmatch_lines(
        [
            "Duplicate parametrization IDs detected*",
            "IDs:            a, <hidden>, a",
            "Duplicates:     a",
        ]