@@ -31,7 +31,6 @@ def _is_pytest_mark(decorator):
3131
3232
3333def _is_pytest_fixture (decorator , fixture = True , yield_fixture = True ):
34- attr = None
3534 to_check = set ()
3635
3736 if fixture :
@@ -40,17 +39,38 @@ def _is_pytest_fixture(decorator, fixture=True, yield_fixture=True):
4039 if yield_fixture :
4140 to_check .add ("yield_fixture" )
4241
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+
4359 try :
60+ if isinstance (decorator , astroid .Name ):
61+ # expecting @fixture
62+ return _check_name (decorator )
4463 if isinstance (decorator , astroid .Attribute ):
4564 # expecting @pytest.fixture
46- attr = decorator
47-
65+ return _check_attribute (decorator )
4866 if isinstance (decorator , astroid .Call ):
67+ func = decorator .func
68+ if isinstance (func , astroid .Name ):
69+ # expecting @fixture(scope=...)
70+ return _check_name (func )
4971 # expecting @pytest.fixture(scope=...)
50- attr = decorator . func
72+ return _check_attribute ( func )
5173
52- if attr and attr .attrname in to_check and attr .expr .name == "pytest" :
53- return True
5474 except AttributeError :
5575 pass
5676
0 commit comments