@@ -31,7 +31,6 @@ def _is_pytest_mark(decorator):
31
31
32
32
33
33
def _is_pytest_fixture (decorator , fixture = True , yield_fixture = True ):
34
- attr = None
35
34
to_check = set ()
36
35
37
36
if fixture :
@@ -40,17 +39,38 @@ def _is_pytest_fixture(decorator, fixture=True, yield_fixture=True):
40
39
if yield_fixture :
41
40
to_check .add ("yield_fixture" )
42
41
42
+ def _check_attribute (attr ):
43
+ """
44
+ handle astroid.Attribute, i.e., when the fixture function is
45
+ used by importing the pytest module
46
+ """
47
+ return attr .attrname in to_check and attr .expr .name == "pytest"
48
+
49
+ def _check_name (name_ ):
50
+ """
51
+ handle astroid.Name, i.e., when the fixture function is
52
+ directly imported
53
+ """
54
+ function_name = name_ .name
55
+ module = decorator .root ().globals .get (function_name , [None ])[0 ]
56
+ module_name = module .modname if module else None
57
+ return function_name in to_check and module_name == "pytest"
58
+
43
59
try :
60
+ if isinstance (decorator , astroid .Name ):
61
+ # expecting @fixture
62
+ return _check_name (decorator )
44
63
if isinstance (decorator , astroid .Attribute ):
45
64
# expecting @pytest.fixture
46
- attr = decorator
47
-
65
+ return _check_attribute (decorator )
48
66
if isinstance (decorator , astroid .Call ):
67
+ func = decorator .func
68
+ if isinstance (func , astroid .Name ):
69
+ # expecting @fixture(scope=...)
70
+ return _check_name (func )
49
71
# expecting @pytest.fixture(scope=...)
50
- attr = decorator . func
72
+ return _check_attribute ( func )
51
73
52
- if attr and attr .attrname in to_check and attr .expr .name == "pytest" :
53
- return True
54
74
except AttributeError :
55
75
pass
56
76
0 commit comments