@@ -117,29 +117,6 @@ def pytest_sessionstart(session: "Session") -> None:
117
117
118
118
scopename2class = {} # type: Dict[str, Type[nodes.Node]]
119
119
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
-
143
120
144
121
def get_scope_package (node , fixturedef : "FixtureDef[object]" ):
145
122
import pytest
@@ -484,14 +461,22 @@ def config(self) -> Config:
484
461
"""The pytest config object associated with this request."""
485
462
return self ._pyfuncitem .config # type: ignore[no-any-return] # noqa: F723
486
463
487
- @scopeproperty ()
464
+ @property
488
465
def function (self ):
489
466
"""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
+ )
490
471
return self ._pyfuncitem .obj
491
472
492
- @scopeproperty ( "class" )
473
+ @property
493
474
def cls (self ):
494
475
"""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
+ )
495
480
clscol = self ._pyfuncitem .getparent (_pytest .python .Class )
496
481
if clscol :
497
482
return clscol .obj
@@ -506,14 +491,22 @@ def instance(self):
506
491
function = getattr (self , "function" , None )
507
492
return getattr (function , "__self__" , None )
508
493
509
- @scopeproperty ()
494
+ @property
510
495
def module (self ):
511
496
"""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
+ )
512
501
return self ._pyfuncitem .getparent (_pytest .python .Module ).obj
513
502
514
- @scopeproperty ()
503
+ @property
515
504
def fspath (self ) -> py .path .local :
516
505
"""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
+ )
517
510
# TODO: Remove ignore once _pyfuncitem is properly typed.
518
511
return self ._pyfuncitem .fspath # type: ignore
519
512
0 commit comments