Advanced pending deprecation warnings.

Also added stacklevel argument, fixed #18127.
This commit is contained in:
Aymeric Augustin 2012-12-25 21:07:27 +01:00
parent 130829334c
commit ef017a5f00
12 changed files with 24 additions and 23 deletions

View File

@ -15,5 +15,5 @@ if __name__ == "__main__":
warnings.warn(
"The `daily_cleanup` script has been deprecated "
"in favor of `django-admin.py clearsessions`.",
PendingDeprecationWarning)
DeprecationWarning)
management.call_command('clearsessions')

View File

@ -139,7 +139,7 @@ class Settings(BaseSettings):
isinstance(setting_value, six.string_types):
warnings.warn("The %s setting must be a tuple. Please fix your "
"settings, as auto-correction is now deprecated." % setting,
PendingDeprecationWarning)
DeprecationWarning, stacklevel=2)
setting_value = (setting_value,) # In case the user forgot the comma.
setattr(self, setting, setting_value)

View File

@ -427,7 +427,7 @@ class AbstractUser(AbstractBaseUser, PermissionsMixin):
SiteProfileNotAvailable if this site does not allow profiles.
"""
warnings.warn("The use of AUTH_PROFILE_MODULE to define user profiles has been deprecated.",
PendingDeprecationWarning)
DeprecationWarning, stacklevel=2)
if not hasattr(self, '_profile_cache'):
from django.conf import settings
if not getattr(settings, 'AUTH_PROFILE_MODULE', False):

View File

@ -7,5 +7,5 @@ class Command(clearsessions.Command):
def handle_noargs(self, **options):
warnings.warn(
"The `cleanup` command has been deprecated in favor of `clearsessions`.",
PendingDeprecationWarning)
DeprecationWarning)
super(Command, self).handle_noargs(**options)

View File

@ -703,7 +703,7 @@ class QuerySet(object):
"""
if 'depth' in kwargs:
warnings.warn('The "depth" keyword argument has been deprecated.\n'
'Use related field names instead.', PendingDeprecationWarning)
'Use related field names instead.', DeprecationWarning, stacklevel=2)
depth = kwargs.pop('depth', 0)
if kwargs:
raise TypeError('Unexpected keyword arguments to select_related: %s'

View File

@ -42,7 +42,8 @@ class HttpResponseBase(six.Iterator):
self._closable_objects = []
if mimetype:
warnings.warn("Using mimetype keyword argument is deprecated, use"
" content_type instead", PendingDeprecationWarning)
" content_type instead",
DeprecationWarning, stacklevel=2)
content_type = mimetype
if not content_type:
content_type = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE,
@ -296,7 +297,7 @@ class HttpResponse(HttpResponseBase):
'Creating streaming responses with `HttpResponse` is '
'deprecated. Use `StreamingHttpResponse` instead '
'if you need the streaming behavior.',
PendingDeprecationWarning, stacklevel=2)
DeprecationWarning, stacklevel=2)
if not hasattr(self, '_iterator'):
self._iterator = iter(self._container)
return self
@ -352,14 +353,14 @@ class CompatibleStreamingHttpResponse(StreamingHttpResponse):
These responses will stream only if no middleware attempts to access the
`content` attribute. Otherwise, they will behave like a regular response,
and raise a `PendingDeprecationWarning`.
and raise a `DeprecationWarning`.
"""
@property
def content(self):
warnings.warn(
'Accessing the `content` attribute on a streaming response is '
'deprecated. Use the `streaming_content` attribute instead.',
PendingDeprecationWarning)
DeprecationWarning, stacklevel=2)
content = b''.join(self)
self.streaming_content = [content]
return content
@ -369,7 +370,7 @@ class CompatibleStreamingHttpResponse(StreamingHttpResponse):
warnings.warn(
'Accessing the `content` attribute on a streaming response is '
'deprecated. Use the `streaming_content` attribute instead.',
PendingDeprecationWarning)
DeprecationWarning, stacklevel=2)
self.streaming_content = [content]

View File

@ -217,7 +217,7 @@ class SortedDict(dict):
# using collections.OrderedDict (Python 2.7 and up), which we'll
# eventually switch to
warnings.warn(
"SortedDict.value_for_index is deprecated", PendingDeprecationWarning,
"SortedDict.value_for_index is deprecated", DeprecationWarning,
stacklevel=2
)
return self[self.keyOrder[index]]
@ -225,7 +225,7 @@ class SortedDict(dict):
def insert(self, index, key, value):
"""Inserts the key, value pair before the item with the given index."""
warnings.warn(
"SortedDict.insert is deprecated", PendingDeprecationWarning,
"SortedDict.insert is deprecated", DeprecationWarning,
stacklevel=2
)
if key in self.keyOrder:

View File

@ -36,7 +36,7 @@ class StrAndUnicode(object):
def __init__(self, *args, **kwargs):
warnings.warn("StrAndUnicode is deprecated. Define a __str__ method "
"and apply the @python_2_unicode_compatible decorator "
"instead.", PendingDeprecationWarning, stacklevel=2)
"instead.", DeprecationWarning, stacklevel=2)
super(StrAndUnicode, self).__init__(*args, **kwargs)
if six.PY3:

View File

@ -19,5 +19,5 @@ def is_iterable(x):
def product(*args, **kwds):
warnings.warn("django.utils.itercompat.product is deprecated; use the native version instead",
PendingDeprecationWarning)
DeprecationWarning, stacklevel=2)
return itertools.product(*args, **kwds)

View File

@ -9,7 +9,7 @@ from __future__ import absolute_import
import warnings
warnings.warn("django.utils.simplejson is deprecated; use json instead.",
PendingDeprecationWarning)
DeprecationWarning, stacklevel=2)
try:
import simplejson

View File

@ -326,13 +326,13 @@ class HttpResponseTests(unittest.TestCase):
r = HttpResponse()
r.content = ['1', '2', 3, '\u079e']
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always", PendingDeprecationWarning)
warnings.simplefilter("always", DeprecationWarning)
my_iter = iter(r)
self.assertEqual(w[0].category, PendingDeprecationWarning)
self.assertEqual(w[0].category, DeprecationWarning)
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always", PendingDeprecationWarning)
warnings.simplefilter("always", DeprecationWarning)
result = list(my_iter)
self.assertEqual(w[0].category, PendingDeprecationWarning)
self.assertEqual(w[0].category, DeprecationWarning)
#'\xde\x9e' == unichr(1950).encode('utf-8')
self.assertEqual(result, [b'1', b'2', b'3', b'\xde\x9e'])
self.assertEqual(r.content, b'123\xde\x9e')
@ -360,7 +360,7 @@ class HttpResponseTests(unittest.TestCase):
# XXX change this when the deprecation completes in HttpResponse
r = HttpResponse(iter(['hello', 'world']))
with warnings.catch_warnings():
warnings.simplefilter("ignore", PendingDeprecationWarning)
warnings.simplefilter("ignore", DeprecationWarning)
self.assertEqual(b''.join(r), b'helloworld')
self.assertEqual(r.content, b'') # not the expected result!
@ -497,7 +497,7 @@ class FileCloseTests(TestCase):
r = HttpResponse(file1)
self.assertFalse(file1.closed)
with warnings.catch_warnings():
warnings.simplefilter("ignore", PendingDeprecationWarning)
warnings.simplefilter("ignore", DeprecationWarning)
list(r)
self.assertFalse(file1.closed)
r.close()

View File

@ -139,14 +139,14 @@ class SortedDictTests(SimpleTestCase):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
d.insert(0, "hello", "world")
assert w[0].category is PendingDeprecationWarning
assert w[0].category is DeprecationWarning
def test_value_for_index(self):
d = SortedDict({"a": 3})
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
self.assertEqual(d.value_for_index(0), 3)
assert w[0].category is PendingDeprecationWarning
assert w[0].category is DeprecationWarning
class MergeDictTests(SimpleTestCase):