Skip to content

Commit 1c810c0

Browse files
author
Oliver Sauder
committed
Clarify deprecation warning of pagination classes
1 parent 929251f commit 1c810c0

File tree

4 files changed

+36
-100
lines changed

4 files changed

+36
-100
lines changed

CHANGELOG.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ any parts of the framework not mentioned in the documentation should generally b
1616
* Add `HyperlinkedRelatedField` and `SerializerMethodHyperlinkedRelatedField`. See [usage docs](docs/usage.md#related-fields)
1717
* Add related urls support. See [usage docs](docs/usage.md#related-urls)
1818
* Add optional [jsonapi-style](http://jsonapi.org/format/) filter backends. See [usage docs](docs/usage.md#filter-backends)
19-
* Performance improvement when rendering relationships with `ModelSerializer`
2019

2120
### Changed
2221

@@ -25,6 +24,8 @@ any parts of the framework not mentioned in the documentation should generally b
2524
### Fixed
2625

2726
* Performance improvement when rendering relationships with `ModelSerializer`
27+
* Do not show deprecation warning when user has implemented custom pagination class overwriting default values.
28+
2829

2930
## [2.5.0] - 2018-07-11
3031

@@ -41,6 +42,16 @@ any parts of the framework not mentioned in the documentation should generally b
4142
### Deprecated
4243

4344
* Deprecate `PageNumberPagination` and `LimitOffsetPagination`. Use `JsonApiPageNumberPagination` and `JsonApiLimitOffsetPagination` instead.
45+
* To retain deprecated values for `PageNumberPagination` and `LimitOffsetPagination` create new custom class like the following in your code base:
46+
```python
47+
class CustomPageNumberPagination(PageNumberPagination):
48+
page_query_param = "page"
49+
page_size_query_param = "page_size"
50+
51+
class CustomLimitOffsetPagination(LimitOffsetPagination):
52+
max_limit = None
53+
```
54+
and adjust `REST_FRAMEWORK['DEFAULT_PAGINATION_CLASS']` setting accordingly.
4455
* Deprecate `JSON_API_FORMAT_KEYS`, use `JSON_API_FORMAT_FIELD_NAMES`.
4556

4657
### Fixed

docs/usage.md

+1-18
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ by setting `REST_FRAMEWORK['DEFAULT_PAGINATION_CLASS']` and by setting `REST_FRA
6060
You can configure fixed values for the page size or limit -- or allow the client to choose the size or limit
6161
via query parameters.
6262

63-
Two pagination classes are available *but their names will change in release 3.0 in order to implement a more
64-
consistent class naming style*:
63+
Two pagination classes are available:
6564
- `JsonApiPageNumberPagination` breaks a response up into pages that start at a given page number with a given size
6665
(number of items per page). It can be configured with the following attributes:
6766
- `page_query_param` (default `page[number]`)
@@ -82,22 +81,6 @@ consistent class naming style*:
8281
- `max_limit` (default `100`) enforces an upper bound on the limit.
8382
Set it to `None` if you don't want to enforce an upper bound.
8483

85-
##### Preparing for future renaming of paginators with default attributes
86-
87-
In release 3.0, the `JsonApi` prefix will be removed from the paginator names. If you currently use those (deprecated)
88-
names (`PageNumberPagination` or `LimitOffsetPagination`) or would like to prepare now for this change, you will want to
89-
explicitly override several of the class attributes.
90-
91-
To retain deprecated values for `PageNumberPagination` set `page_query_param = "page"` and
92-
`page_size_query_param = "page_size"`.
93-
94-
To prepare for the renaming of `JsonApiPageNumberPagination` use `PageNumberPagination` now with
95-
`page_query_param = "page[number]"` and `page_size_query_param = "page[size]"`.
96-
97-
To retain deprecated vales for `LimitOffsetPagination` set `max_limit = None`.
98-
99-
To prepare for the renaming of `JsonApiLimitOffsetPagination` to `LimitOffsetPagination` set `max_limit = 100`.
100-
10184
##### Examples
10285
These examples show how to configure the parameters to use non-standard names and different limits:
10386

example/tests/unit/test_pagination.py

+8-35
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,10 @@ def test_valid_offset_limit(self):
8282
@pytest.mark.xfail((sys.version_info.major, sys.version_info.minor) == (2, 7),
8383
reason="python2.7 fails to generate DeprecationWarrning for unknown reason")
8484
def test_limit_offset_deprecation(self):
85-
with pytest.warns(PendingDeprecationWarning) as record:
85+
with pytest.warns(DeprecationWarning) as record:
8686
pagination.LimitOffsetPagination()
8787
assert len(record) == 1
88-
assert 'LimitOffsetPagination will change in release 3.0' in str(record[0].message)
89-
90-
@pytest.mark.xfail((sys.version_info.major, sys.version_info.minor) == (2, 7),
91-
reason="python2.7 fails to generate DeprecationWarrning for unknown reason")
92-
def test_jsonapi_limit_offset_deprecation(self):
93-
with pytest.warns(PendingDeprecationWarning) as record:
94-
pagination.JsonApiLimitOffsetPagination()
95-
assert len(record) == 1
96-
assert 'JsonApiLimitOffsetPagination will be renamed to LimitOffsetPagination'\
97-
in str(record[0].message)
88+
assert 'LimitOffsetPagination is deprecated' in str(record[0].message)
9889

9990
class MyInheritedLimitOffsetPagination(pagination.LimitOffsetPagination):
10091
"""
@@ -109,10 +100,10 @@ class MyOverridenLimitOffsetPagination(pagination.LimitOffsetPagination):
109100
max_limit = None
110101

111102
def test_my_limit_offset_deprecation(self):
112-
with pytest.warns(PendingDeprecationWarning) as record:
103+
with pytest.warns(DeprecationWarning) as record:
113104
self.MyInheritedLimitOffsetPagination()
114105
assert len(record) == 1
115-
assert 'LimitOffsetPagination will change in release 3.0' in str(record[0].message)
106+
assert 'LimitOffsetPagination is deprecated' in str(record[0].message)
116107

117108
with pytest.warns(None) as record:
118109
self.MyOverridenLimitOffsetPagination()
@@ -127,19 +118,10 @@ class TestPageNumber:
127118
@pytest.mark.xfail((sys.version_info.major, sys.version_info.minor) == (2, 7),
128119
reason="python2.7 fails to generate DeprecationWarrning for unknown reason")
129120
def test_page_number_deprecation(self):
130-
with pytest.warns(PendingDeprecationWarning) as record:
121+
with pytest.warns(DeprecationWarning) as record:
131122
pagination.PageNumberPagination()
132123
assert len(record) == 1
133-
assert 'PageNumberPagination will change in release 3.0' in str(record[0].message)
134-
135-
@pytest.mark.xfail((sys.version_info.major, sys.version_info.minor) == (2, 7),
136-
reason="python2.7 fails to generate DeprecationWarrning for unknown reason")
137-
def test_jsonapi_page_number_deprecation(self):
138-
with pytest.warns(PendingDeprecationWarning) as record:
139-
pagination.JsonApiPageNumberPagination()
140-
assert len(record) == 1
141-
assert 'JsonApiPageNumberPagination will be renamed to PageNumberPagination' \
142-
in str(record[0].message)
124+
assert 'PageNumberPagination is deprecated' in str(record[0].message)
143125

144126
class MyInheritedPageNumberPagination(pagination.PageNumberPagination):
145127
"""
@@ -157,20 +139,11 @@ class MyOverridenPageNumberPagination(pagination.PageNumberPagination):
157139
@pytest.mark.xfail((sys.version_info.major, sys.version_info.minor) == (2, 7),
158140
reason="python2.7 fails to generate DeprecationWarrning for unknown reason")
159141
def test_my_page_number_deprecation(self):
160-
with pytest.warns(PendingDeprecationWarning) as record:
142+
with pytest.warns(DeprecationWarning) as record:
161143
self.MyInheritedPageNumberPagination()
162144
assert len(record) == 1
163-
assert 'PageNumberPagination will change in release 3.0' in str(record[0].message)
145+
assert 'PageNumberPagination is deprecated' in str(record[0].message)
164146

165147
with pytest.warns(None) as record:
166148
self.MyOverridenPageNumberPagination()
167149
assert len(record) == 0
168-
169-
@pytest.mark.xfail((sys.version_info.major, sys.version_info.minor) == (2, 7),
170-
reason="python2.7 fails to generate DeprecationWarrning for unknown reason")
171-
def test_my_jsonapi_page_number_deprecation(self):
172-
with pytest.warns(PendingDeprecationWarning) as record:
173-
pagination.JsonApiPageNumberPagination()
174-
assert len(record) == 1
175-
assert 'JsonApiPageNumberPagination will be renamed to PageNumberPagination' \
176-
in str(record[0].message)

rest_framework_json_api/pagination.py

+15-46
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from rest_framework.views import Response
1010

1111

12-
class _JsonApiPageNumberPagination(PageNumberPagination):
12+
class JsonApiPageNumberPagination(PageNumberPagination):
1313
"""
1414
A json-api compatible pagination format.
1515
Use a private name for the implementation because the public name is pending deprecation.
@@ -51,21 +51,7 @@ def get_paginated_response(self, data):
5151
})
5252

5353

54-
class JsonApiPageNumberPagination(_JsonApiPageNumberPagination):
55-
"""
56-
current public name to be deprecated soon.
57-
"""
58-
def __init__(self):
59-
if type(self) == JsonApiPageNumberPagination:
60-
warnings.warn(
61-
'JsonApiPageNumberPagination will be renamed to PageNumberPagination in'
62-
' release 3.0. See '
63-
'https://django-rest-framework-json-api.readthedocs.io/en/stable/usage.html#pagination', # noqa: E501
64-
PendingDeprecationWarning)
65-
super(_JsonApiPageNumberPagination, self).__init__()
66-
67-
68-
class _JsonApiLimitOffsetPagination(LimitOffsetPagination):
54+
class JsonApiLimitOffsetPagination(LimitOffsetPagination):
6955
"""
7056
A limit/offset based style. For example:
7157
http://api.example.org/accounts/?page[limit]=100
@@ -117,22 +103,7 @@ def get_paginated_response(self, data):
117103
})
118104

119105

120-
class JsonApiLimitOffsetPagination(_JsonApiLimitOffsetPagination):
121-
"""
122-
current public name to be deprecated soon.
123-
"""
124-
125-
def __init__(self):
126-
warnings.warn(
127-
'JsonApiLimitOffsetPagination will be renamed to LimitOffsetPagination in release 3.0'
128-
' See '
129-
'https://django-rest-framework-json-api.readthedocs.io/en/stable/usage.html#pagination',
130-
PendingDeprecationWarning)
131-
super(JsonApiLimitOffsetPagination, self).__init__()
132-
133-
134-
135-
class PageNumberPagination(_JsonApiPageNumberPagination):
106+
class PageNumberPagination(JsonApiPageNumberPagination):
136107
"""
137108
A soon-to-be-changed paginator that uses non-JSON:API query parameters (default:
138109
'page' and 'page_size' instead of 'page[number]' and 'page[size]').
@@ -148,18 +119,17 @@ def __init__(self):
148119
'page_size_query_param' not in type(self).__dict__)
149120
if warn:
150121
warnings.warn(
151-
'PageNumberPagination will change in release 3.0 to change default values to: '
152-
'`page_query_param = "page[number]"` and `page_size_query_param = "page[size]"`. '
153-
'If you want to retain the current defaults you will need to explicitly set '
154-
'`page_query_param = "page"` and `page_size_query_param = "page_size"`. '
155-
'See '
156-
'https://django-rest-framework-json-api.readthedocs.io/en/stable/usage.html#pagination', # noqa: E501
157-
PendingDeprecationWarning)
122+
'PageNumberPagination is deprecated use JsonApiPageNumberPagination instead. '
123+
'If you want to retain current defaults you will need to implement custom '
124+
'pagination class explicitly setting `page_query_param = "page"` and '
125+
'`page_size_query_param = "page_size"`. '
126+
'See changelog for more details.',
127+
DeprecationWarning)
158128

159129
super(PageNumberPagination, self).__init__()
160130

161131

162-
class LimitOffsetPagination(_JsonApiLimitOffsetPagination):
132+
class LimitOffsetPagination(JsonApiLimitOffsetPagination):
163133
"""
164134
Deprecated paginator that uses a different max_limit
165135
"""
@@ -172,10 +142,9 @@ def __init__(self):
172142
warn = 'max_limit' not in type(self).__dict__
173143
if warn:
174144
warnings.warn(
175-
'LimitOffsetPagination will change in release 3.0 to default to `max_limit=100`. '
176-
'If you want to retain the current default you will need to explicitly set '
177-
'`max_limit = None`.'
178-
'See '
179-
'https://django-rest-framework-json-api.readthedocs.io/en/stable/usage.html#pagination', # noqa: E501
180-
PendingDeprecationWarning)
145+
'LimitOffsetPagination is deprecated use JsonApiLimitOffsetPagination instead. '
146+
'If you want to retain current defaults you will need to implement custom '
147+
'pagination class explicitly setting `max_limit = None`. '
148+
'See changelog for more details.',
149+
DeprecationWarning)
181150
super(LimitOffsetPagination, self).__init__()

0 commit comments

Comments
 (0)