Skip to content

Commit 0b86ae9

Browse files
committed
Add django-upgrade to pre-commit hooks
1 parent ea1da76 commit 0b86ae9

14 files changed

+36
-32
lines changed

.pre-commit-config.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,9 @@ repos:
3737
hooks:
3838
- id: pyupgrade
3939
args: ["--py39-plus", "--keep-percent-format"]
40+
41+
- repo: https://github.com/adamchainz/django-upgrade
42+
rev: 1.24.0
43+
hooks:
44+
- id: django-upgrade
45+
args: [--target-version, "4.2"]

rest_framework/authentication.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def get_authorization_header(request):
1717
1818
Hide some test client ickyness where the header can be unicode.
1919
"""
20-
auth = request.META.get('HTTP_AUTHORIZATION', b'')
20+
auth = request.headers.get('authorization', b'')
2121
if isinstance(auth, str):
2222
# Work around django test client oddness
2323
auth = auth.encode(HTTP_HEADER_ENCODING)

rest_framework/authtoken/admin.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def url_for_result(self, result):
2121
current_app=self.model_admin.admin_site.name)
2222

2323

24+
@admin.register(TokenProxy)
2425
class TokenAdmin(admin.ModelAdmin):
2526
list_display = ('key', 'user', 'created')
2627
fields = ('user',)
@@ -49,6 +50,3 @@ def delete_model(self, request, obj):
4950
# Map back to actual Token, since delete() uses pk.
5051
token = Token.objects.get(key=obj.key)
5152
return super().delete_model(request, token)
52-
53-
54-
admin.site.register(TokenProxy, TokenAdmin)

rest_framework/negotiation.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,5 @@ def get_accept_list(self, request):
9393
Given the incoming request, return a tokenized list of media
9494
type strings.
9595
"""
96-
header = request.META.get('HTTP_ACCEPT', '*/*')
96+
header = request.headers.get('accept', '*/*')
9797
return [token.strip() for token in header.split(',')]

rest_framework/throttling.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def get_ident(self, request):
2626
if present and number of proxies is > 0. If not use all of
2727
HTTP_X_FORWARDED_FOR if it is available, if not use REMOTE_ADDR.
2828
"""
29-
xff = request.META.get('HTTP_X_FORWARDED_FOR')
29+
xff = request.headers.get('x-forwarded-for')
3030
remote_addr = request.META.get('REMOTE_ADDR')
3131
num_proxies = api_settings.NUM_PROXIES
3232

tests/test_api_client.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from django.http import HttpResponse
66
from django.test import override_settings
7-
from django.urls import path, re_path
7+
from django.urls import path
88

99
from rest_framework.compat import coreapi, coreschema
1010
from rest_framework.parsers import FileUploadParser
@@ -180,7 +180,7 @@ def get(self, request):
180180
urlpatterns = [
181181
path('', SchemaView.as_view()),
182182
path('example/', ListView.as_view()),
183-
re_path(r'^example/(?P<id>[0-9]+)/$', DetailView.as_view()),
183+
path('example/<int:id>/', DetailView.as_view()),
184184
path('upload/', UploadView.as_view()),
185185
path('download/', DownloadView.as_view()),
186186
path('text/', TextView.as_view()),

tests/test_middleware.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def test_middleware_can_access_user_when_processing_response(self):
102102
key = 'abcd1234'
103103
Token.objects.create(key=key, user=user)
104104

105-
self.client.get('/auth', HTTP_AUTHORIZATION='Token %s' % key)
105+
self.client.get('/auth', headers={"authorization": 'Token %s' % key})
106106

107107
@override_settings(MIDDLEWARE=('tests.test_middleware.RequestPOSTMiddleware',))
108108
def test_middleware_can_access_request_post_when_processing_response(self):

tests/test_relations.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from _pytest.monkeypatch import MonkeyPatch
55
from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist
66
from django.test import override_settings
7-
from django.urls import re_path
7+
from django.urls import path
88
from django.utils.datastructures import MultiValueDict
99

1010
from rest_framework import relations, serializers
@@ -152,7 +152,7 @@ def test_pk_representation(self):
152152

153153

154154
urlpatterns = [
155-
re_path(r'^example/(?P<name>.+)/$', lambda: None, name='example'),
155+
path('example/<path:name>/', lambda: None, name='example'),
156156
]
157157

158158

tests/test_renderers.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -174,30 +174,30 @@ def test_head_method_serializes_no_content(self):
174174

175175
def test_default_renderer_serializes_content_on_accept_any(self):
176176
"""If the Accept header is set to */* the default renderer should serialize the response."""
177-
resp = self.client.get('/', HTTP_ACCEPT='*/*')
177+
resp = self.client.get('/', headers={"accept": '*/*'})
178178
self.assertEqual(resp['Content-Type'], RendererA.media_type + '; charset=utf-8')
179179
self.assertEqual(resp.content, RENDERER_A_SERIALIZER(DUMMYCONTENT))
180180
self.assertEqual(resp.status_code, DUMMYSTATUS)
181181

182182
def test_specified_renderer_serializes_content_default_case(self):
183183
"""If the Accept header is set the specified renderer should serialize the response.
184184
(In this case we check that works for the default renderer)"""
185-
resp = self.client.get('/', HTTP_ACCEPT=RendererA.media_type)
185+
resp = self.client.get('/', headers={"accept": RendererA.media_type})
186186
self.assertEqual(resp['Content-Type'], RendererA.media_type + '; charset=utf-8')
187187
self.assertEqual(resp.content, RENDERER_A_SERIALIZER(DUMMYCONTENT))
188188
self.assertEqual(resp.status_code, DUMMYSTATUS)
189189

190190
def test_specified_renderer_serializes_content_non_default_case(self):
191191
"""If the Accept header is set the specified renderer should serialize the response.
192192
(In this case we check that works for a non-default renderer)"""
193-
resp = self.client.get('/', HTTP_ACCEPT=RendererB.media_type)
193+
resp = self.client.get('/', headers={"accept": RendererB.media_type})
194194
self.assertEqual(resp['Content-Type'], RendererB.media_type + '; charset=utf-8')
195195
self.assertEqual(resp.content, RENDERER_B_SERIALIZER(DUMMYCONTENT))
196196
self.assertEqual(resp.status_code, DUMMYSTATUS)
197197

198198
def test_unsatisfiable_accept_header_on_request_returns_406_status(self):
199199
"""If the Accept header is unsatisfiable we should return a 406 Not Acceptable response."""
200-
resp = self.client.get('/', HTTP_ACCEPT='foo/bar')
200+
resp = self.client.get('/', headers={"accept": 'foo/bar'})
201201
self.assertEqual(resp.status_code, status.HTTP_406_NOT_ACCEPTABLE)
202202

203203
def test_specified_renderer_serializes_content_on_format_query(self):
@@ -228,14 +228,14 @@ def test_specified_renderer_is_used_on_format_query_with_matching_accept(self):
228228
RendererB.format
229229
)
230230
resp = self.client.get('/' + param,
231-
HTTP_ACCEPT=RendererB.media_type)
231+
headers={"accept": RendererB.media_type})
232232
self.assertEqual(resp['Content-Type'], RendererB.media_type + '; charset=utf-8')
233233
self.assertEqual(resp.content, RENDERER_B_SERIALIZER(DUMMYCONTENT))
234234
self.assertEqual(resp.status_code, DUMMYSTATUS)
235235

236236
def test_parse_error_renderers_browsable_api(self):
237237
"""Invalid data should still render the browsable API correctly."""
238-
resp = self.client.post('/parseerror', data='foobar', content_type='application/json', HTTP_ACCEPT='text/html')
238+
resp = self.client.post('/parseerror', data='foobar', content_type='application/json', headers={"accept": 'text/html'})
239239
self.assertEqual(resp['Content-Type'], 'text/html; charset=utf-8')
240240
self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)
241241

@@ -714,13 +714,13 @@ class DummyView:
714714
assert result is None
715715

716716
def test_extra_actions_dropdown(self):
717-
resp = self.client.get('/api/examples/', HTTP_ACCEPT='text/html')
717+
resp = self.client.get('/api/examples/', headers={"accept": 'text/html'})
718718
assert 'id="extra-actions-menu"' in resp.content.decode()
719719
assert '/api/examples/list_action/' in resp.content.decode()
720720
assert '>Extra list action<' in resp.content.decode()
721721

722722
def test_extra_actions_dropdown_not_authed(self):
723-
resp = self.client.get('/api/unauth-examples/', HTTP_ACCEPT='text/html')
723+
resp = self.client.get('/api/unauth-examples/', headers={"accept": 'text/html'})
724724
assert 'id="extra-actions-menu"' not in resp.content.decode()
725725
assert '/api/examples/list_action/' not in resp.content.decode()
726726
assert '>Extra list action<' not in resp.content.decode()

tests/test_requests_client.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def post(self, request):
2828
}
2929
post = request.POST
3030
json = None
31-
if request.META.get('CONTENT_TYPE') == 'application/json':
31+
if request.headers.get('content-type') == 'application/json':
3232
json = request.data
3333

3434
return Response({

tests/test_response.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -151,23 +151,23 @@ def test_head_method_serializes_no_content(self):
151151

152152
def test_default_renderer_serializes_content_on_accept_any(self):
153153
"""If the Accept header is set to */* the default renderer should serialize the response."""
154-
resp = self.client.get('/', HTTP_ACCEPT='*/*')
154+
resp = self.client.get('/', headers={"accept": '*/*'})
155155
self.assertEqual(resp['Content-Type'], RendererA.media_type + '; charset=utf-8')
156156
self.assertEqual(resp.content, RENDERER_A_SERIALIZER(DUMMYCONTENT))
157157
self.assertEqual(resp.status_code, DUMMYSTATUS)
158158

159159
def test_specified_renderer_serializes_content_default_case(self):
160160
"""If the Accept header is set the specified renderer should serialize the response.
161161
(In this case we check that works for the default renderer)"""
162-
resp = self.client.get('/', HTTP_ACCEPT=RendererA.media_type)
162+
resp = self.client.get('/', headers={"accept": RendererA.media_type})
163163
self.assertEqual(resp['Content-Type'], RendererA.media_type + '; charset=utf-8')
164164
self.assertEqual(resp.content, RENDERER_A_SERIALIZER(DUMMYCONTENT))
165165
self.assertEqual(resp.status_code, DUMMYSTATUS)
166166

167167
def test_specified_renderer_serializes_content_non_default_case(self):
168168
"""If the Accept header is set the specified renderer should serialize the response.
169169
(In this case we check that works for a non-default renderer)"""
170-
resp = self.client.get('/', HTTP_ACCEPT=RendererB.media_type)
170+
resp = self.client.get('/', headers={"accept": RendererB.media_type})
171171
self.assertEqual(resp['Content-Type'], RendererB.media_type + '; charset=utf-8')
172172
self.assertEqual(resp.content, RENDERER_B_SERIALIZER(DUMMYCONTENT))
173173
self.assertEqual(resp.status_code, DUMMYSTATUS)
@@ -192,7 +192,7 @@ def test_specified_renderer_is_used_on_format_query_with_matching_accept(self):
192192
"""If both a 'format' query and a matching Accept header specified,
193193
the renderer with the matching format attribute should serialize the response."""
194194
resp = self.client.get('/?format=%s' % RendererB.format,
195-
HTTP_ACCEPT=RendererB.media_type)
195+
headers={"accept": RendererB.media_type})
196196
self.assertEqual(resp['Content-Type'], RendererB.media_type + '; charset=utf-8')
197197
self.assertEqual(resp.content, RENDERER_B_SERIALIZER(DUMMYCONTENT))
198198
self.assertEqual(resp.status_code, DUMMYSTATUS)

tests/test_testing.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
@api_view(['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'])
2323
def view(request):
24-
data = {'auth': request.META.get('HTTP_AUTHORIZATION', b'')}
24+
data = {'auth': request.headers.get('authorization', b'')}
2525
if request.user:
2626
data['user'] = request.user.username
2727
if request.auth:
@@ -347,7 +347,7 @@ def test_empty_request_content_type(self):
347347
data=None,
348348
content_type='application/json',
349349
)
350-
assert request.META['CONTENT_TYPE'] == 'application/json'
350+
assert request.headers['content-type'] == 'application/json'
351351

352352

353353
class TestUrlPatternTestCase(URLPatternsTestCase):

tests/test_urlpatterns.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from collections import namedtuple
22

33
from django.test import TestCase
4-
from django.urls import Resolver404, URLResolver, include, path, re_path
4+
from django.urls import Resolver404, URLResolver, include, path
55
from django.urls.resolvers import RegexPattern
66

77
from rest_framework.test import APIRequestFactory
@@ -93,7 +93,7 @@ def test_format_suffix_django2(self):
9393
def test_format_suffix_django2_args(self):
9494
urlpatterns = [
9595
path('convtest/<int:pk>', dummy_view),
96-
re_path(r'^retest/(?P<pk>[0-9]+)$', dummy_view),
96+
path('retest/<int:pk>', dummy_view),
9797
]
9898
test_paths = [
9999
URLTestPath('/convtest/42', (), {'pk': 42}),
@@ -145,10 +145,10 @@ def test_included_urls(self):
145145
def test_included_urls_mixed(self):
146146
nested_patterns = [
147147
path('path/<int:child>', dummy_view),
148-
re_path(r'^re_path/(?P<child>[0-9]+)$', dummy_view)
148+
path('re_path/<int:child>', dummy_view)
149149
]
150150
urlpatterns = [
151-
re_path(r'^pre_path/(?P<parent>[0-9]+)/', include(nested_patterns), {'foo': 'bar'}),
151+
path('pre_path/<int:parent>/', include(nested_patterns), {'foo': 'bar'}),
152152
path('ppath/<int:parent>/', include(nested_patterns), {'foo': 'bar'}),
153153
]
154154
test_paths = [
@@ -185,7 +185,7 @@ def _test_allowed_formats(self, urlpatterns):
185185

186186
def test_allowed_formats_re_path(self):
187187
urlpatterns = [
188-
re_path(r'^test$', dummy_view),
188+
path('test', dummy_view),
189189
]
190190
self._test_allowed_formats(urlpatterns)
191191

tests/test_versioning.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class TestURLReversing(URLPatternsTestCase, APITestCase):
152152
path('v1/', include((included, 'v1'), namespace='v1')),
153153
path('another/', dummy_view, name='another'),
154154
re_path(r'^(?P<version>[v1|v2]+)/another/$', dummy_view, name='another'),
155-
re_path(r'^(?P<foo>.+)/unversioned/$', dummy_view, name='unversioned'),
155+
path('<path:foo>/unversioned/', dummy_view, name='unversioned'),
156156

157157
]
158158

0 commit comments

Comments
 (0)