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

Code before:
        @pytest.fixture
        def add_attr1(request):
            request.instance.attr1 = object()


        @pytest.fixture
        def add_attr2(request):
            request.instance.attr2 = request.instance.attr1


        @pytest.mark.usefixtures('add_attr1')
        class Parent:
            pass


        @pytest.mark.usefixtures('add_attr2')
        class TestThings(Parent):
            def test_attrs(self):
                assert self.attr1 == self.attr2
        """
    )
    result = pytester.runpytest(foo)
    result.assert_outcomes(passed=1)


# @pytest.mark.issue("https://github.com/pytest-dev/pytest/issues/12863")
def test_mark_parametrize_over_staticmethod(pytester: Pytester) -> None:
    """Check that applying marks works as intended on classmethods and staticmethods.

    Regression test for #12863.
    """
    pytester.makepyfile(
        """
        import pytest

        class TestClass:
            @pytest.mark.parametrize("value", [1, 2])
            @classmethod
            def test_classmethod_wrapper(cls, value: int):
                assert value in [1, 2]

            @classmethod
            @pytest.mark.parametrize("value", [1, 2])
            def test_classmethod_wrapper_on_top(cls, value: int):
                assert value in [1, 2]

            @pytest.mark.parametrize("value", [1, 2])
            @staticmethod
            def test_staticmethod_wrapper(value: int):
                assert value in [1, 2]


Review comment (refactor):
The pytest testsuite doesn't use an `issue` mark, so let's remove this comment.

```suggestion
```

Code after:
        @pytest.fixture
        def add_attr1(request):
            request.instance.attr1 = object()


        @pytest.fixture
        def add_attr2(request):
            request.instance.attr2 = request.instance.attr1


        @pytest.mark.usefixtures('add_attr1')
        class Parent:
            pass


        @pytest.mark.usefixtures('add_attr2')
        class TestThings(Parent):
            def test_attrs(self):
                assert self.attr1 == self.attr2
        """
    )
    result = pytester.runpytest(foo)
    result.assert_outcomes(passed=1)


def test_mark_parametrize_over_staticmethod(pytester: Pytester) -> None:
    """Check that applying marks works as intended on classmethods and staticmethods.

    Regression test for #12863.
    """
    pytester.makepyfile(
        """
        import pytest

        class TestClass:
            @pytest.mark.parametrize("value", [1, 2])
            @classmethod
            def test_classmethod_wrapper(cls, value: int):
                assert value in [1, 2]

            @classmethod
            @pytest.mark.parametrize("value", [1, 2])
            def test_classmethod_wrapper_on_top(cls, value: int):
                assert value in [1, 2]

            @pytest.mark.parametrize("value", [1, 2])
            @staticmethod
            def test_staticmethod_wrapper(value: int):
                assert value in [1, 2]

            @staticmethod
Repository: pytest-dev/pytest
File: testing/test_unraisableexception.py (Python)

Code before:
    # finished.
    # see: https://github.com/pytest-dev/pytest/pull/13057#discussion_r1888396126
    pytester.makepyfile(
        test_it="""
        import pytest

        class BrokenDel:
            def __init__(self):
                self.self = self  # make a reference cycle

            def __del__(self):
                raise ValueError("del is broken")

        def test_it():
            BrokenDel()
        """
    )

    with _disable_gc():
        result = pytester.runpytest_subprocess("-Wdefault")

    assert result.ret == pytest.ExitCode.OK

    result.assert_outcomes(passed=1)
    result.stderr.fnmatch_lines("ValueError: del is broken")


@pytest.mark.filterwarnings("error::pytest.PytestUnraisableExceptionWarning")
def test_possibly_none_excinfo(pytester: Pytester) -> None:
    pytester.makepyfile(
        test_it="""
        import sys
        import types

        def test_it():
            sys.unraisablehook(
                types.SimpleNamespace(
                    exc_type=RuntimeError,
                    exc_value=None,
                    exc_traceback=None,
                    err_msg=None,
                    object=None,
                )
            )
        """
    )

    result = pytester.runpytest()

    # TODO: should be a test failure or error
    assert result.ret == pytest.ExitCode.TESTS_FAILED

Review comment (suggestion):
This should ideally be `result.assert_outcomes(passed=1, warnings=1)`, right? If that's also a limitation then add a comment with a `TODO`

Code after:
    # finished.
    # see: https://github.com/pytest-dev/pytest/pull/13057#discussion_r1888396126
    pytester.makepyfile(
        test_it="""
        import pytest

        class BrokenDel:
            def __init__(self):
                self.self = self  # make a reference cycle

            def __del__(self):
                raise ValueError("del is broken")

        def test_it():
            BrokenDel()
        """
    )

    with _disable_gc():
        result = pytester.runpytest_subprocess("-Wdefault")

    assert result.ret == pytest.ExitCode.OK

    # TODO: should be warnings=1, but the outcome has already come out
    # by the time the warning triggers
    result.assert_outcomes(passed=1)
    result.stderr.fnmatch_lines("ValueError: del is broken")


@pytest.mark.filterwarnings("error::pytest.PytestUnraisableExceptionWarning")
def test_possibly_none_excinfo(pytester: Pytester) -> None:
    pytester.makepyfile(
        test_it="""
        import sys
        import types

        def test_it():
            sys.unraisablehook(
                types.SimpleNamespace(
                    exc_type=RuntimeError,
                    exc_value=None,
                    exc_traceback=None,
                    err_msg=None,
                    object=None,
                )
            )
        """
    )

    result = pytester.runpytest()
Repository: pytest-dev/pytest
File: testing/test_unraisableexception.py (Python)

Code before:
    # finished.
    # see: https://github.com/pytest-dev/pytest/pull/13057#discussion_r1888396126
    pytester.makepyfile(
        test_it="""
        import pytest

        class BrokenDel:
            def __init__(self):
                self.self = self  # make a reference cycle

            def __del__(self):
                raise ValueError("del is broken")

        def test_it():
            BrokenDel()
        """
    )

    with _disable_gc():
        result = pytester.runpytest_subprocess("-Wdefault")

    assert result.ret == pytest.ExitCode.OK

    result.assert_outcomes(passed=1)
    result.stderr.fnmatch_lines("ValueError: del is broken")


@pytest.mark.filterwarnings("error::pytest.PytestUnraisableExceptionWarning")
def test_possibly_none_excinfo(pytester: Pytester) -> None:
    pytester.makepyfile(
        test_it="""
        import sys
        import types

        def test_it():
            sys.unraisablehook(
                types.SimpleNamespace(
                    exc_type=RuntimeError,
                    exc_value=None,
                    exc_traceback=None,
                    err_msg=None,
                    object=None,
                )
            )
        """
    )

    result = pytester.runpytest()

    # TODO: should be a test failure or error
    assert result.ret == pytest.ExitCode.TESTS_FAILED

Review comment (suggestion):
```suggestion
    # TODO: should be warnings=1, but the outcome has already come out
    # by the time the warning triggers
    result.assert_outcomes(passed=1)
```

Code after:
    # finished.
    # see: https://github.com/pytest-dev/pytest/pull/13057#discussion_r1888396126
    pytester.makepyfile(
        test_it="""
        import pytest

        class BrokenDel:
            def __init__(self):
                self.self = self  # make a reference cycle

            def __del__(self):
                raise ValueError("del is broken")

        def test_it():
            BrokenDel()
        """
    )

    with _disable_gc():
        result = pytester.runpytest_subprocess("-Wdefault")

    assert result.ret == pytest.ExitCode.OK

    # TODO: should be warnings=1, but the outcome has already come out
    # by the time the warning triggers
    result.assert_outcomes(passed=1)
    result.stderr.fnmatch_lines("ValueError: del is broken")


@pytest.mark.filterwarnings("error::pytest.PytestUnraisableExceptionWarning")
def test_possibly_none_excinfo(pytester: Pytester) -> None:
    pytester.makepyfile(
        test_it="""
        import sys
        import types

        def test_it():
            sys.unraisablehook(
                types.SimpleNamespace(
                    exc_type=RuntimeError,
                    exc_value=None,
                    exc_traceback=None,
                    err_msg=None,
                    object=None,
                )
            )
        """
    )

    result = pytester.runpytest()
Repository: pytest-dev/pytest
File: testing/test_mark.py (Python)

Code before:
        @pytest.mark.parametrize("empty", [])
        def test():
            pass
        """
    )
    result = pytester.runpytest(str(p1))
    result.stdout.fnmatch_lines(
        [
            "collected 0 items / 1 error",
            "* ERROR collecting test_parameterset_for_fail_at_collect.py *",
            "Empty parameter set in 'test' at line 3",
            "*= 1 error in *",
        ]
    )
    assert result.ret == ExitCode.INTERRUPTED


def test_paramset_empty_no_idfunc(
    pytester: Pytester, monkeypatch: pytest.MonkeyPatch
) -> None:
    p1 = pytester.makepyfile(
        """
        import pytest

        def idfunc(value):
           raise ValueError()
        @pytest.mark.parametrize("param", [], ids=idfunc)
        def test(param):
            pass
        """
    )
    result = pytester.runpytest(p1, "-v", "-rs")
    result.stdout.fnmatch_lines(
        [
            "* collected 1 item",
            "test_paramset_empty_no_idfunc* SKIPPED *",
            "SKIPPED [1] test_paramset_empty_no_idfunc.py:5: got empty parameter set for (param)",
            "*= 1 skipped in *",
        ]
    )


def test_parameterset_for_parametrize_bad_markname(pytester: Pytester) -> None:
    with pytest.raises(pytest.UsageError):
        test_parameterset_for_parametrize_marks(pytester, "bad")


def test_mark_expressions_no_smear(pytester: Pytester) -> None:
    pytester.makepyfile(
        """
        import pytest

Review comment (suggestion):
```suggestion
        def idfunc(value):
            raise ValueError()
```

Code after:
        @pytest.mark.parametrize("empty", [])
        def test():
            pass
        """
    )
    result = pytester.runpytest(str(p1))
    result.stdout.fnmatch_lines(
        [
            "collected 0 items / 1 error",
            "* ERROR collecting test_parameterset_for_fail_at_collect.py *",
            "Empty parameter set in 'test' at line 3",
            "*= 1 error in *",
        ]
    )
    assert result.ret == ExitCode.INTERRUPTED


def test_paramset_empty_no_idfunc(
    pytester: Pytester, monkeypatch: pytest.MonkeyPatch
) -> None:
    """An empty parameter set should not call the user provided id function (#13031)."""
    p1 = pytester.makepyfile(
        """
        import pytest

        def idfunc(value):
            raise ValueError()
        @pytest.mark.parametrize("param", [], ids=idfunc)
        def test(param):
            pass
        """
    )
    result = pytester.runpytest(p1, "-v", "-rs")
    result.stdout.fnmatch_lines(
        [
            "* collected 1 item",
            "test_paramset_empty_no_idfunc* SKIPPED *",
            "SKIPPED [1] test_paramset_empty_no_idfunc.py:5: got empty parameter set for (param)",
            "*= 1 skipped in *",
        ]
    )


def test_parameterset_for_parametrize_bad_markname(pytester: Pytester) -> None:
    with pytest.raises(pytest.UsageError):
        test_parameterset_for_parametrize_marks(pytester, "bad")


def test_mark_expressions_no_smear(pytester: Pytester) -> None:
    pytester.makepyfile(
        """
Repository: pytest-dev/pytest
File: testing/test_mark.py (Python)

Code before:
        get_empty_parameterset_mark(config, ["a"], pytest_configure)

    p1 = pytester.makepyfile(
        """
        import pytest

        @pytest.mark.parametrize("empty", [])
        def test():
            pass
        """
    )
    result = pytester.runpytest(str(p1))
    result.stdout.fnmatch_lines(
        [
            "collected 0 items / 1 error",
            "* ERROR collecting test_parameterset_for_fail_at_collect.py *",
            "Empty parameter set in 'test' at line 3",
            "*= 1 error in *",
        ]
    )
    assert result.ret == ExitCode.INTERRUPTED


def test_paramset_empty_no_idfunc(
    pytester: Pytester, monkeypatch: pytest.MonkeyPatch
) -> None:
    p1 = pytester.makepyfile(
        """
        import pytest

        def idfunc(value):
            raise ValueError()
        @pytest.mark.parametrize("param", [], ids=idfunc)
        def test(param):
            pass
        """
    )
    result = pytester.runpytest(p1, "-v", "-rs")
    result.stdout.fnmatch_lines(
        [
            "* collected 1 item",
            "test_paramset_empty_no_idfunc* SKIPPED *",
            "SKIPPED [1] test_paramset_empty_no_idfunc.py:5: got empty parameter set for (param)",
            "*= 1 skipped in *",
        ]
    )


def test_parameterset_for_parametrize_bad_markname(pytester: Pytester) -> None:
    with pytest.raises(pytest.UsageError):
        test_parameterset_for_parametrize_marks(pytester, "bad")

Review comment (suggestion):
```suggestion
) -> None:
    """An empty parameter set should not call the user provided id function (#13031)."""
```

Code after:
        get_empty_parameterset_mark(config, ["a"], pytest_configure)

    p1 = pytester.makepyfile(
        """
        import pytest

        @pytest.mark.parametrize("empty", [])
        def test():
            pass
        """
    )
    result = pytester.runpytest(str(p1))
    result.stdout.fnmatch_lines(
        [
            "collected 0 items / 1 error",
            "* ERROR collecting test_parameterset_for_fail_at_collect.py *",
            "Empty parameter set in 'test' at line 3",
            "*= 1 error in *",
        ]
    )
    assert result.ret == ExitCode.INTERRUPTED


def test_paramset_empty_no_idfunc(
    pytester: Pytester, monkeypatch: pytest.MonkeyPatch
) -> None:
    """An empty parameter set should not call the user provided id function (#13031)."""
    p1 = pytester.makepyfile(
        """
        import pytest

        def idfunc(value):
            raise ValueError()
        @pytest.mark.parametrize("param", [], ids=idfunc)
        def test(param):
            pass
        """
    )
    result = pytester.runpytest(p1, "-v", "-rs")
    result.stdout.fnmatch_lines(
        [
            "* collected 1 item",
            "test_paramset_empty_no_idfunc* SKIPPED *",
            "SKIPPED [1] test_paramset_empty_no_idfunc.py:5: got empty parameter set for (param)",
            "*= 1 skipped in *",
        ]
    )


def test_parameterset_for_parametrize_bad_markname(pytester: Pytester) -> None:
    with pytest.raises(pytest.UsageError):
Repository: pytest-dev/pytest
File: testing/conftest.py (Python)

Code before:
# mypy: allow-untyped-defs
from __future__ import annotations

from collections.abc import Generator
import dataclasses
import re
import sys

import pygments

from _pytest.monkeypatch import MonkeyPatch
from _pytest.pytester import Pytester
import pytest


if sys.gettrace():

    @pytest.fixture(autouse=True)
    def restore_tracing():
        """Restore tracing function (when run with Coverage.py).

        https://bugs.python.org/issue37011
        """
        orig_trace = sys.gettrace()
        yield
        if sys.gettrace() != orig_trace:
            sys.settrace(orig_trace)


@pytest.fixture(autouse=True)
def set_column_width(monkeypatch: pytest.MonkeyPatch) -> None:
    """
    Force terminal width to 80: some tests check the formatting of --help, which is sensible
    to terminal width.
    """

Review comment (bug):
This makes the testsuite explicitly depend on pygments - but the tests in `test_terminal.py` already fail if `pygments` is not installed, and it's in `optional-dependencies.dev` in `pyproject.toml`, so I suppose that's alright?

Code after:
# mypy: allow-untyped-defs
from __future__ import annotations

from collections.abc import Generator
import dataclasses
import importlib.metadata
import re
import sys

from packaging.version import Version

from _pytest.monkeypatch import MonkeyPatch
from _pytest.pytester import Pytester
import pytest


if sys.gettrace():

    @pytest.fixture(autouse=True)
    def restore_tracing():
        """Restore tracing function (when run with Coverage.py).

        https://bugs.python.org/issue37011
        """
        orig_trace = sys.gettrace()
        yield
        if sys.gettrace() != orig_trace:
            sys.settrace(orig_trace)


@pytest.fixture(autouse=True)
def set_column_width(monkeypatch: pytest.MonkeyPatch) -> None:
    """
    Force terminal width to 80: some tests check the formatting of --help, which is sensible
    to terminal width.
Repository: pytest-dev/pytest
File: testing/_py/test_local.py (Python)

Code before:

    @pytest.mark.parametrize("bin", (False, True))
    def test_dump(self, tmpdir, bin):
        path = tmpdir.join(f"dumpfile{int(bin)}")
        try:
            d = {"answer": 42}
            path.dump(d, bin=bin)
            f = path.open("rb+")
            import pickle

            dnew = pickle.load(f)
            assert d == dnew
        finally:
            f.close()

    def test_setmtime(self):
        import tempfile

        try:
            fd, name = tempfile.mkstemp()
            os.close(fd)
        except AttributeError:
            name = tempfile.mktemp()
            open(name, "w").close()
        try:
            mtime = int(_pytest.timing.perf_counter()) - 100
            path = local(name)
            assert path.mtime() != mtime
            path.setmtime(mtime)
            assert path.mtime() == mtime
            path.setmtime()
            assert path.mtime() != mtime
        finally:
            os.remove(name)

    def test_normpath(self, path1):
        new1 = path1.join("/otherdir")
        new2 = path1.join("otherdir")
        assert str(new1) == str(new2)

    def test_mkdtemp_creation(self):
        d = local.mkdtemp()
        try:
            assert d.check(dir=1)
        finally:
            d.remove(rec=1)

    def test_tmproot(self):
        d = local.mkdtemp()
        tmproot = local.get_temproot()
        try:

Review comment (suggestion):
We are dealing here with an actual timestamp, not a duration, so we should keep using `time`.

```suggestion
            mtime = int(_pytest.timing.time()) - 100
```

Code after:

    @pytest.mark.parametrize("bin", (False, True))
    def test_dump(self, tmpdir, bin):
        path = tmpdir.join(f"dumpfile{int(bin)}")
        try:
            d = {"answer": 42}
            path.dump(d, bin=bin)
            f = path.open("rb+")
            import pickle

            dnew = pickle.load(f)
            assert d == dnew
        finally:
            f.close()

    def test_setmtime(self):
        import tempfile

        try:
            fd, name = tempfile.mkstemp()
            os.close(fd)
        except AttributeError:
            name = tempfile.mktemp()
            open(name, "w").close()
        try:
            mtime = int(_pytest.timing.time()) - 100
            path = local(name)
            assert path.mtime() != mtime
            path.setmtime(mtime)
            assert path.mtime() == mtime
            path.setmtime()
            assert path.mtime() != mtime
        finally:
            os.remove(name)

    def test_normpath(self, path1):
        new1 = path1.join("/otherdir")
        new2 = path1.join("otherdir")
        assert str(new1) == str(new2)

    def test_mkdtemp_creation(self):
        d = local.mkdtemp()
        try:
            assert d.check(dir=1)
        finally:
            d.remove(rec=1)

    def test_tmproot(self):
        d = local.mkdtemp()
        tmproot = local.get_temproot()
        try:
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):
wow, this is .. something.

We should add a comment explaining that the cm is needed for cleanup, otherwise we'll get endless people wanting to clean this up.

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