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

Update framework deprecation warnings #7075

Merged
merged 1 commit into from Dec 5, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions rest_framework/__init__.py
Expand Up @@ -25,9 +25,9 @@
default_app_config = 'rest_framework.apps.RestFrameworkConfig'


class RemovedInDRF311Warning(DeprecationWarning):
class RemovedInDRF312Warning(DeprecationWarning):
pass


class RemovedInDRF312Warning(PendingDeprecationWarning):
class RemovedInDRF313Warning(PendingDeprecationWarning):
pass
24 changes: 3 additions & 21 deletions rest_framework/routers.py
Expand Up @@ -14,15 +14,13 @@
urlpatterns = router.urls
"""
import itertools
import warnings
from collections import OrderedDict, namedtuple

from django.conf.urls import url
from django.core.exceptions import ImproperlyConfigured
from django.urls import NoReverseMatch
from django.utils.deprecation import RenameMethodsBase

from rest_framework import RemovedInDRF311Warning, views
from rest_framework import views
from rest_framework.response import Response
from rest_framework.reverse import reverse
from rest_framework.schemas import SchemaGenerator
Expand All @@ -48,27 +46,11 @@ def flatten(list_of_lists):
return itertools.chain(*list_of_lists)


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


class BaseRouter(metaclass=RenameRouterMethods):
class BaseRouter:
def __init__(self):
self.registry = []

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, RemovedInDRF311Warning, 2)

assert not (basename and base_name), (
"Do not provide both the `basename` and `base_name` arguments.")

if basename is None:
basename = base_name

def register(self, prefix, viewset, basename=None):
if basename is None:
basename = self.get_default_basename(viewset)
self.registry.append((prefix, viewset, basename))
Expand Down
73 changes: 1 addition & 72 deletions tests/test_routers.py
@@ -1,4 +1,3 @@
import warnings
from collections import namedtuple

import pytest
Expand All @@ -8,9 +7,7 @@
from django.test import TestCase, override_settings
from django.urls import resolve, reverse

from rest_framework import (
RemovedInDRF311Warning, permissions, serializers, viewsets
)
from rest_framework import 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 @@ -488,71 +485,3 @@ def test_basename(self):
initkwargs = match.func.initkwargs

assert initkwargs['basename'] == 'routertestmodel'


class TestBaseNameRename(TestCase):

def test_base_name_and_basename_assertion(self):
router = SimpleRouter()

msg = "Do not provide both the `basename` and `base_name` arguments."
with warnings.catch_warnings(record=True) as w, \
self.assertRaisesMessage(AssertionError, msg):
warnings.simplefilter('always')
router.register('mock', MockViewSet, 'mock', base_name='mock')

msg = "The `base_name` argument is pending deprecation in favor of `basename`."
assert len(w) == 1
assert str(w[0].message) == msg

def test_base_name_argument_deprecation(self):
router = SimpleRouter()

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

msg = "The `base_name` argument is pending deprecation in favor of `basename`."
assert len(w) == 1
assert str(w[0].message) == msg
assert router.registry == [
('mock', MockViewSet, 'mock'),
]

def test_basename_argument_no_warnings(self):
router = SimpleRouter()

with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')
router.register('mock', MockViewSet, basename='mock')

assert len(w) == 0
assert router.registry == [
('mock', MockViewSet, 'mock'),
]

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(RemovedInDRF311Warning) as w:
warnings.simplefilter('always')

class CustomRouter(SimpleRouter):
def get_default_base_name(self, viewset):
return 'foo'

assert len(w) == 1
assert str(w[0].message) == msg

# Deprecated method implementation should still be called
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')

router = CustomRouter()
router.register('mock', MockViewSet)

assert len(w) == 0
assert router.registry == [
('mock', MockViewSet, 'foo'),
]