From 78b1ea3f4519ef063e94a7c62185c3589fce8510 Mon Sep 17 00:00:00 2001 From: Aaron VanDevender Date: Fri, 4 Oct 2024 10:06:29 -0700 Subject: [PATCH 1/3] add package property --- src/_pytest/fixtures.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index 6b882fa3515..86612f8269e 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -464,6 +464,15 @@ def module(self): assert mod is not None return mod.obj + @property + def package(self): + """Python package object where the test function was collected.""" + if self.scope not in ("function", "class", "module", "package"): + raise AttributeError(f"package not available in {self.scope}-scoped context") + pkg = self._pyfuncitem.getparent(_pytest.python.Package) + assert pkg is not None + return pkg.obj + @property def path(self) -> Path: """Path where the test function was collected.""" From 4bfea1f882b7dcf01af25dc272a76258ad839612 Mon Sep 17 00:00:00 2001 From: Aaron VanDevender Date: Fri, 4 Oct 2024 10:35:15 -0700 Subject: [PATCH 2/3] add package test --- testing/test_doctest.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/testing/test_doctest.py b/testing/test_doctest.py index 4aa4876c711..f0dd544d81a 100644 --- a/testing/test_doctest.py +++ b/testing/test_doctest.py @@ -1366,7 +1366,7 @@ def auto(request): result.stdout.no_fnmatch_line("*FAILURES*") result.stdout.fnmatch_lines(["*=== 1 passed in *"]) - @pytest.mark.parametrize("scope", SCOPES) + @pytest.mark.parametrize("scope", [*SCOPES, "package"]) def test_auto_use_request_attributes(self, pytester, scope): """Check that all attributes of a request in an autouse fixture behave as expected when requested for a doctest item. @@ -1377,6 +1377,8 @@ def test_auto_use_request_attributes(self, pytester, scope): @pytest.fixture(autouse=True, scope="{scope}") def auto(request): + if "{scope}" == 'package': + assert request.package is None if "{scope}" == 'module': assert request.module is None if "{scope}" == 'class': From 5e7459896f700d256e1a7200896743eb12423601 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 4 Oct 2024 18:30:51 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/_pytest/fixtures.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index 86612f8269e..33a4a20ab25 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -468,7 +468,9 @@ def module(self): def package(self): """Python package object where the test function was collected.""" if self.scope not in ("function", "class", "module", "package"): - raise AttributeError(f"package not available in {self.scope}-scoped context") + raise AttributeError( + f"package not available in {self.scope}-scoped context" + ) pkg = self._pyfuncitem.getparent(_pytest.python.Package) assert pkg is not None return pkg.obj