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( warnings.warn(
"The `daily_cleanup` script has been deprecated " "The `daily_cleanup` script has been deprecated "
"in favor of `django-admin.py clearsessions`.", "in favor of `django-admin.py clearsessions`.",
PendingDeprecationWarning) DeprecationWarning)
management.call_command('clearsessions') management.call_command('clearsessions')

View File

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

View File

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

View File

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

View File

@ -703,7 +703,7 @@ class QuerySet(object):
""" """
if 'depth' in kwargs: if 'depth' in kwargs:
warnings.warn('The "depth" keyword argument has been deprecated.\n' 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) depth = kwargs.pop('depth', 0)
if kwargs: if kwargs:
raise TypeError('Unexpected keyword arguments to select_related: %s' raise TypeError('Unexpected keyword arguments to select_related: %s'

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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