From 10c72132bf5d3bbd379a2dbef872e0c32d4031e8 Mon Sep 17 00:00:00 2001 From: Xavier Ordoquy Date: Tue, 6 Oct 2015 23:36:02 +0200 Subject: [PATCH] small `deprecated refactor --- rest_framework/compat.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/rest_framework/compat.py b/rest_framework/compat.py index c1a8bcaed..6de823b8a 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -14,24 +14,34 @@ from django.db import connection, transaction from django.utils import six from django.views.generic import View -from rest_framework import VERSION +import rest_framework def deprecated(since, message): - current_version = [int(i) for i in VERSION.split('.')] + """ + Issue a deprecation warning `message`. `since` is a tuple of major, minor + indicating when the deprecation should start. + """ + current_version = [int(i) for i in rest_framework.VERSION.split('.')] - assert current_version[0] == since[0], "Deprecated code must be removed before major version change. Current: {0} vs Deprecated Since: {1}".format(current_version[0], since[0]) + major_message = ( + "Deprecated code must be removed before major version change. " + "Current: {0} vs Deprecated Since: {1}".format( + current_version[0], since[0]) + ) + minor_message = "Deprecated code must be removed within two minor versions" + + assert current_version[0] == since[0], major_message minor_version_difference = current_version[1] - since[1] - assert minor_version_difference in [1,2], "Deprecated code must be removed within two minor versions" - - warning_type = PendingDeprecationWarning if minor_version_difference == 1 else DeprecationWarning + assert minor_version_difference in [1, 2], minor_message + warning_type = DeprecationWarning + if minor_version_difference == 1: + warning_type = PendingDeprecationWarning warnings.warn(message, warning_type) - - try: import importlib # Available in Python 3.1+ except ImportError: