Skip to content

Introduce RemovedInDRF…Warning classes to simplify deprecations. #6480

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

Merged
merged 1 commit into from
Mar 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/community/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ REST framework releases follow a formal deprecation policy, which is in line wit

The timeline for deprecation of a feature present in version 1.0 would work as follows:

* Version 1.1 would remain **fully backwards compatible** with 1.0, but would raise `PendingDeprecationWarning` warnings if you use the feature that are due to be deprecated. These warnings are **silent by default**, but can be explicitly enabled when you're ready to start migrating any required changes. For example if you start running your tests using `python -Wd manage.py test`, you'll be warned of any API changes you need to make.
* Version 1.1 would remain **fully backwards compatible** with 1.0, but would raise `RemovedInDRF13Warning` warnings, subclassing `PendingDeprecationWarning`, if you use the feature that are due to be deprecated. These warnings are **silent by default**, but can be explicitly enabled when you're ready to start migrating any required changes. For example if you start running your tests using `python -Wd manage.py test`, you'll be warned of any API changes you need to make.

* Version 1.2 would escalate these warnings to `DeprecationWarning`, which is loud by default.
* Version 1.2 would escalate these warnings to subclass `DeprecationWarning`, which is loud by default.

* Version 1.3 would remove the deprecated bits of API entirely.

Expand Down
8 changes: 8 additions & 0 deletions rest_framework/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,11 @@
ISO_8601 = 'iso-8601'

default_app_config = 'rest_framework.apps.RestFrameworkConfig'


class RemovedInDRF310Warning(DeprecationWarning):
pass


class RemovedInDRF311Warning(PendingDeprecationWarning):
pass
5 changes: 3 additions & 2 deletions rest_framework/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from django.forms.utils import pretty_name
from django.utils import six

from rest_framework import RemovedInDRF310Warning
from rest_framework.views import APIView


Expand Down Expand Up @@ -225,7 +226,7 @@ def detail_route(methods=None, **kwargs):
warnings.warn(
"`detail_route` is deprecated and will be removed in 3.10 in favor of "
"`action`, which accepts a `detail` bool. Use `@action(detail=True)` instead.",
DeprecationWarning, stacklevel=2
RemovedInDRF310Warning, stacklevel=2
)

def decorator(func):
Expand All @@ -243,7 +244,7 @@ def list_route(methods=None, **kwargs):
warnings.warn(
"`list_route` is deprecated and will be removed in 3.10 in favor of "
"`action`, which accepts a `detail` bool. Use `@action(detail=False)` instead.",
DeprecationWarning, stacklevel=2
RemovedInDRF310Warning, stacklevel=2
)

def decorator(func):
Expand Down
3 changes: 2 additions & 1 deletion rest_framework/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from django.utils.encoding import force_text
from django.utils.translation import ugettext_lazy as _

from rest_framework import RemovedInDRF310Warning
from rest_framework.compat import (
coreapi, coreschema, distinct, is_guardian_installed
)
Expand Down Expand Up @@ -299,7 +300,7 @@ def __init__(self):
warnings.warn(
"`DjangoObjectPermissionsFilter` has been deprecated and moved to "
"the 3rd-party django-rest-framework-guardian package.",
DeprecationWarning, stacklevel=2
RemovedInDRF310Warning, stacklevel=2
)
assert is_guardian_installed(), 'Using DjangoObjectPermissionsFilter, but django-guardian is not installed'

Expand Down
12 changes: 7 additions & 5 deletions rest_framework/routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
from django.utils import six
from django.utils.deprecation import RenameMethodsBase

from rest_framework import views
from rest_framework import (
RemovedInDRF310Warning, RemovedInDRF311Warning, views
)
from rest_framework.response import Response
from rest_framework.reverse import reverse
from rest_framework.schemas import SchemaGenerator
Expand All @@ -43,7 +45,7 @@ def __new__(cls, url, name, initkwargs):
"`DynamicDetailRoute` is deprecated and will be removed in 3.10 "
"in favor of `DynamicRoute`, which accepts a `detail` boolean. Use "
"`DynamicRoute(url, name, True, initkwargs)` instead.",
DeprecationWarning, stacklevel=2
RemovedInDRF310Warning, stacklevel=2
)
return DynamicRoute(url, name, True, initkwargs)

Expand All @@ -54,7 +56,7 @@ def __new__(cls, url, name, initkwargs):
"`DynamicListRoute` is deprecated and will be removed in 3.10 in "
"favor of `DynamicRoute`, which accepts a `detail` boolean. Use "
"`DynamicRoute(url, name, False, initkwargs)` instead.",
DeprecationWarning, stacklevel=2
RemovedInDRF310Warning, stacklevel=2
)
return DynamicRoute(url, name, False, initkwargs)

Expand All @@ -77,7 +79,7 @@ def flatten(list_of_lists):

class RenameRouterMethods(RenameMethodsBase):
renamed_methods = (
('get_default_base_name', 'get_default_basename', PendingDeprecationWarning),
('get_default_base_name', 'get_default_basename', RemovedInDRF311Warning),
)


Expand All @@ -88,7 +90,7 @@ def __init__(self):
def register(self, prefix, viewset, basename=None, base_name=None):
if base_name is not None:
msg = "The `base_name` argument is pending deprecation in favor of `basename`."
warnings.warn(msg, PendingDeprecationWarning, 2)
warnings.warn(msg, RemovedInDRF311Warning, 2)

assert not (basename and base_name), (
"Do not provide both the `basename` and `base_name` arguments.")
Expand Down
8 changes: 4 additions & 4 deletions tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest
from django.test import TestCase

from rest_framework import status
from rest_framework import RemovedInDRF310Warning, status
from rest_framework.authentication import BasicAuthentication
from rest_framework.decorators import (
action, api_view, authentication_classes, detail_route, list_route,
Expand Down Expand Up @@ -290,7 +290,7 @@ def test_action():
raise NotImplementedError

def test_detail_route_deprecation(self):
with pytest.warns(DeprecationWarning) as record:
with pytest.warns(RemovedInDRF310Warning) as record:
@detail_route()
def view(request):
raise NotImplementedError
Expand All @@ -303,7 +303,7 @@ def view(request):
)

def test_list_route_deprecation(self):
with pytest.warns(DeprecationWarning) as record:
with pytest.warns(RemovedInDRF310Warning) as record:
@list_route()
def view(request):
raise NotImplementedError
Expand All @@ -317,7 +317,7 @@ def view(request):

def test_route_url_name_from_path(self):
# pre-3.8 behavior was to base the `url_name` off of the `url_path`
with pytest.warns(DeprecationWarning):
with pytest.warns(RemovedInDRF310Warning):
@list_route(url_path='foo_bar')
def view(request):
raise NotImplementedError
Expand Down
6 changes: 3 additions & 3 deletions tests/test_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
from django.urls import ResolverMatch

from rest_framework import (
HTTP_HEADER_ENCODING, authentication, generics, permissions, serializers,
status, views
HTTP_HEADER_ENCODING, RemovedInDRF310Warning, authentication, generics,
permissions, serializers, status, views
)
from rest_framework.compat import PY36, is_guardian_installed, mock
from rest_framework.filters import DjangoObjectPermissionsFilter
Expand Down Expand Up @@ -427,7 +427,7 @@ def test_django_object_permissions_filter_deprecated(self):
message = ("`DjangoObjectPermissionsFilter` has been deprecated and moved "
"to the 3rd-party django-rest-framework-guardian package.")
self.assertEqual(len(w), 1)
self.assertIs(w[-1].category, DeprecationWarning)
self.assertIs(w[-1].category, RemovedInDRF310Warning)
self.assertEqual(str(w[-1].message), message)

def test_can_read_list_permissions(self):
Expand Down
8 changes: 5 additions & 3 deletions tests/test_routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
from django.test import TestCase, override_settings
from django.urls import resolve, reverse

from rest_framework import permissions, serializers, viewsets
from rest_framework import (
RemovedInDRF311Warning, permissions, serializers, viewsets
)
from rest_framework.compat import get_regex_pattern
from rest_framework.decorators import action
from rest_framework.response import Response
Expand Down Expand Up @@ -508,7 +510,7 @@ def test_base_name_and_basename_assertion(self):
def test_base_name_argument_deprecation(self):
router = SimpleRouter()

with pytest.warns(PendingDeprecationWarning) as w:
with pytest.warns(RemovedInDRF311Warning) as w:
warnings.simplefilter('always')
router.register('mock', MockViewSet, base_name='mock')

Expand All @@ -535,7 +537,7 @@ def test_get_default_base_name_deprecation(self):
msg = "`CustomRouter.get_default_base_name` method should be renamed `get_default_basename`."

# Class definition should raise a warning
with pytest.warns(PendingDeprecationWarning) as w:
with pytest.warns(RemovedInDRF311Warning) as w:
warnings.simplefilter('always')

class CustomRouter(SimpleRouter):
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy