mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-09 23:34:45 +08:00
Get some pytest fixtures working with some side effects
This commit is contained in:
62
jedi/plugins/pytest.py
Normal file
62
jedi/plugins/pytest.py
Normal file
@@ -0,0 +1,62 @@
|
||||
from jedi._compatibility import FileNotFoundError
|
||||
from jedi.inference.cache import inference_state_method_cache
|
||||
from jedi.inference.imports import load_module_from_path
|
||||
from jedi.inference.filters import ParserTreeFilter
|
||||
from jedi.inference.base_value import NO_VALUES, ValueSet
|
||||
|
||||
|
||||
def execute(callback):
|
||||
def wrapper(value, arguments):
|
||||
if value.py__name__() == 'fixture' \
|
||||
and value.parent_context.py__name__() == '_pytest.fixtures':
|
||||
return NO_VALUES
|
||||
|
||||
return callback(value, arguments)
|
||||
return wrapper
|
||||
|
||||
|
||||
def infer_anonymous_param(func):
|
||||
def get_returns(value):
|
||||
function_context = value.as_context()
|
||||
return function_context.get_return_values()
|
||||
|
||||
def wrapper(param):
|
||||
module = param.get_root_context()
|
||||
fixtures = _goto_pytest_fixture(module, param.string_name)
|
||||
if fixtures:
|
||||
return ValueSet.from_sets(
|
||||
get_returns(value)
|
||||
for fixture in fixtures
|
||||
for value in fixture.infer()
|
||||
)
|
||||
return func(param)
|
||||
return wrapper
|
||||
|
||||
|
||||
def _goto_pytest_fixture(module_context, name):
|
||||
for module_context in _iter_pytest_modules(module_context):
|
||||
names = FixtureFilter(module_context).get(name)
|
||||
if names:
|
||||
return names
|
||||
|
||||
|
||||
@inference_state_method_cache()
|
||||
def _iter_pytest_modules(module_context):
|
||||
yield module_context
|
||||
|
||||
folder = module_context.get_value().file_io.get_parent_folder()
|
||||
sys_path = module_context.inference_state.get_sys_path()
|
||||
while any(folder.path.startswith(p) for p in sys_path):
|
||||
file_io = folder.get_file_io('conftest.py')
|
||||
try:
|
||||
m = load_module_from_path(module_context.inference_state, file_io)
|
||||
yield m.as_context()
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
folder = folder.get_parent_folder()
|
||||
|
||||
|
||||
class FixtureFilter(ParserTreeFilter):
|
||||
def _filter(self, names):
|
||||
for name in super(FixtureFilter, self)._filter(names):
|
||||
yield name
|
||||
Reference in New Issue
Block a user