Skip to content

Commit 91dbdb6

Browse files
authored
Merge pull request #7698 from bluetech/rm-scopedproperty
fixture: remove `@scopeproperty`
2 parents 877c621 + 12de92c commit 91dbdb6

File tree

1 file changed

+20
-27
lines changed

1 file changed

+20
-27
lines changed

src/_pytest/fixtures.py

+20-27
Original file line numberDiff line numberDiff line change
@@ -117,29 +117,6 @@ def pytest_sessionstart(session: "Session") -> None:
117117

118118
scopename2class = {} # type: Dict[str, Type[nodes.Node]]
119119

120-
scope2props = dict(session=()) # type: Dict[str, Tuple[str, ...]]
121-
scope2props["package"] = ("fspath",)
122-
scope2props["module"] = ("fspath", "module")
123-
scope2props["class"] = scope2props["module"] + ("cls",)
124-
scope2props["instance"] = scope2props["class"] + ("instance",)
125-
scope2props["function"] = scope2props["instance"] + ("function", "keywords")
126-
127-
128-
def scopeproperty(name=None, doc=None):
129-
def decoratescope(func):
130-
scopename = name or func.__name__
131-
132-
def provide(self):
133-
if func.__name__ in scope2props[self.scope]:
134-
return func(self)
135-
raise AttributeError(
136-
"{} not available in {}-scoped context".format(scopename, self.scope)
137-
)
138-
139-
return property(provide, None, None, func.__doc__)
140-
141-
return decoratescope
142-
143120

144121
def get_scope_package(node, fixturedef: "FixtureDef[object]"):
145122
import pytest
@@ -484,14 +461,22 @@ def config(self) -> Config:
484461
"""The pytest config object associated with this request."""
485462
return self._pyfuncitem.config # type: ignore[no-any-return] # noqa: F723
486463

487-
@scopeproperty()
464+
@property
488465
def function(self):
489466
"""Test function object if the request has a per-function scope."""
467+
if self.scope != "function":
468+
raise AttributeError(
469+
"function not available in {}-scoped context".format(self.scope)
470+
)
490471
return self._pyfuncitem.obj
491472

492-
@scopeproperty("class")
473+
@property
493474
def cls(self):
494475
"""Class (can be None) where the test function was collected."""
476+
if self.scope not in ("class", "function"):
477+
raise AttributeError(
478+
"cls not available in {}-scoped context".format(self.scope)
479+
)
495480
clscol = self._pyfuncitem.getparent(_pytest.python.Class)
496481
if clscol:
497482
return clscol.obj
@@ -506,14 +491,22 @@ def instance(self):
506491
function = getattr(self, "function", None)
507492
return getattr(function, "__self__", None)
508493

509-
@scopeproperty()
494+
@property
510495
def module(self):
511496
"""Python module object where the test function was collected."""
497+
if self.scope not in ("function", "class", "module"):
498+
raise AttributeError(
499+
"module not available in {}-scoped context".format(self.scope)
500+
)
512501
return self._pyfuncitem.getparent(_pytest.python.Module).obj
513502

514-
@scopeproperty()
503+
@property
515504
def fspath(self) -> py.path.local:
516505
"""The file system path of the test module which collected this test."""
506+
if self.scope not in ("function", "class", "module", "package"):
507+
raise AttributeError(
508+
"module not available in {}-scoped context".format(self.scope)
509+
)
517510
# TODO: Remove ignore once _pyfuncitem is properly typed.
518511
return self._pyfuncitem.fspath # type: ignore
519512

0 commit comments

Comments
 (0)