From c0f12a098c0258eef3e9af982c17f5ef7f6c927d Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Thu, 20 Apr 2017 22:44:15 -0400 Subject: [PATCH] Fixed #28109 -- Corrected the stack level of unordered queryset pagination warnings. Refs #26290. Thanks Tim for the review. --- django/core/paginator.py | 3 ++- docs/releases/1.11.1.txt | 3 +++ tests/pagination/tests.py | 17 +++++++++++++---- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/django/core/paginator.py b/django/core/paginator.py index 85d88eb180..2933f802c6 100644 --- a/django/core/paginator.py +++ b/django/core/paginator.py @@ -100,7 +100,8 @@ class Paginator: warnings.warn( 'Pagination may yield inconsistent results with an unordered ' 'object_list: {!r}'.format(self.object_list), - UnorderedObjectListWarning + UnorderedObjectListWarning, + stacklevel=3 ) diff --git a/docs/releases/1.11.1.txt b/docs/releases/1.11.1.txt index 91d8854424..1ce96347d7 100644 --- a/docs/releases/1.11.1.txt +++ b/docs/releases/1.11.1.txt @@ -49,3 +49,6 @@ Bugfixes * Fixed a regression where ``CheckboxSelectMultiple``, ``NullBooleanSelect``, ``RadioSelect``, ``SelectMultiple``, and ``Select`` localized option values (:ticket:`28075`). + +* Corrected the stack level of unordered queryset pagination warnings + (:ticket:`28109`). diff --git a/tests/pagination/tests.py b/tests/pagination/tests.py index 4aaaa3c3de..30fa310dc0 100644 --- a/tests/pagination/tests.py +++ b/tests/pagination/tests.py @@ -1,4 +1,5 @@ import unittest +import warnings from datetime import datetime from django.core.paginator import ( @@ -318,12 +319,20 @@ class ModelPaginationTests(TestCase): self.assertIsInstance(p.object_list, list) def test_paginating_unordered_queryset_raises_warning(self): - msg = ( + with warnings.catch_warnings(record=True) as warns: + # Prevent the RuntimeWarning subclass from appearing as an + # exception due to the warnings.simplefilter() in runtests.py. + warnings.filterwarnings('always', category=UnorderedObjectListWarning) + Paginator(Article.objects.all(), 5) + self.assertEqual(len(warns), 1) + warning = warns[0] + self.assertEqual(str(warning.message), ( "Pagination may yield inconsistent results with an unordered " "object_list: , " ", , , " ", , , " ", ]>" - ) - with self.assertRaisesMessage(UnorderedObjectListWarning, msg): - Paginator(Article.objects.all(), 5) + )) + # The warning points at the Paginator caller (i.e. the stacklevel + # is appropriate). + self.assertEqual(warning.filename, __file__)