Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pytest plugins: deprecation warnings for renamed fixtures #4900

Closed
blueyed opened this issue Mar 9, 2019 · 2 comments
Closed

pytest plugins: deprecation warnings for renamed fixtures #4900

blueyed opened this issue Mar 9, 2019 · 2 comments
Labels
type: question general question, might be closed after 2 weeks of inactivity

Comments

@blueyed
Copy link
Contributor

blueyed commented Mar 9, 2019

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)
@RonnyPfannschmidt
Copy link
Member

i propose adding a usage_warning parameter to pytest.fixture ensuring it will correctly warn for all sites that request it

@blueyed
Copy link
Contributor Author

blueyed commented Mar 19, 2019

Hmm, usage_warning would still require to wrap the old/new fixture then in some way - not sure how that would really help.

I came up with the following now:

class PytestDjangoPrefixDeprecationWarning(DeprecationWarning):
    pass


def wrap_deprecated_fixture(oldname, newname):
    @pytest.fixture(name=oldname)
    def inner(request):
        msg = "Please use fixture %s instead of %s." % (newname, oldname)
        warnings.warn(PytestDjangoPrefixDeprecationWarning(msg))
        return request.getfixturevalue(newname)
    return inner


admin_client = wrap_deprecated_fixture("admin_client", "django_admin_client")
…

@blueyed blueyed closed this as completed Mar 19, 2019
@blueyed blueyed added the type: question general question, might be closed after 2 weeks of inactivity label Mar 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question general question, might be closed after 2 weeks of inactivity
Projects
None yet
Development

No branches or pull requests

2 participants