Closed
Description
For pytest-django I was looking into having a prefix of "django_" for all fixtures, and adding compat warning for the old ones.
I came up with the following, but it has some issues:
- needs proper stacklevel
- is the generator test sane?
Can this be done easier?
I think it might make sense for pytest to provide some help in this regard, and there is lilkely something that I have not found..?
class PytestDjangoPrefixDeprecationWarning(DeprecationWarning):
pass
# Copied from pytest.
def iscoroutinefunction(func):
return getattr(func, "_is_coroutine", False) or (
hasattr(inspect, "iscoroutinefunction") and inspect.iscoroutinefunction(func)
)
# Copied from pytest.
def is_generator(func):
genfunc = inspect.isgeneratorfunction(func)
return genfunc and not iscoroutinefunction(func)
def compat_prefix_warning(new_fixture):
# TODO: get stacklevel for warning to point at test..
# Non-complete code via pytest-pdb:
# https://github.com/fschulze/pytest-pdb/blob/88bb88a20c6e69ec4e3da4a2d6cabac9cfb2cd86/pytest_pdb.py#L7-L16
# For handling fixture setup: https://github.com/fschulze/pytest-pdb/issues/7
new_name = new_fixture.__name__
assert new_name.startswith("django_")
orig_func = new_fixture.__pytest_wrapped__.obj
if is_generator(orig_func):
@pytest.fixture
@wraps(orig_func)
def wrapper(**kwargs):
msg = "Please use fixture %s instead of %s." % (new_name, new_name[7:])
warnings.warn(PytestDjangoPrefixDeprecationWarning(msg), stacklevel=2)
yield from orig_func(**kwargs)
else:
@pytest.fixture
@wraps(orig_func)
def wrapper(**kwargs):
msg = "Please use fixture %s instead of %s." % (new_name, new_name[7:])
warnings.warn(PytestDjangoPrefixDeprecationWarning(msg), stacklevel=2)
return orig_func(**kwargs)
return wrapper
# For backward compatibility.
settings = compat_prefix_warning(django_settings)
db = compat_prefix_warning(django_db)