Skip to content

Commit 0c179d7

Browse files
authored
Adjusted to only use f-strings for slight performance improvement (#1028)
1 parent b37cec0 commit 0c179d7

File tree

10 files changed

+57
-63
lines changed

10 files changed

+57
-63
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ any parts of the framework not mentioned in the documentation should generally b
1010

1111
## [Unreleased]
1212

13+
### Changed
14+
15+
* Adjusted to only use f-strings for slight performance improvement.
16+
1317
### Removed
1418

1519
* Removed support for Django 3.0.

example/tests/integration/test_polymorphism.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ def test_polymorphism_on_polymorphic_model_detail_patch(single_art_project, clie
6464
url = reverse("project-detail", kwargs={"pk": single_art_project.pk})
6565
response = client.get(url)
6666
content = response.json()
67-
test_topic = "test-{}".format(random.randint(0, 999999))
68-
test_artist = "test-{}".format(random.randint(0, 999999))
67+
test_topic = f"test-{random.randint(0, 999999)}"
68+
test_artist = f"test-{random.randint(0, 999999)}"
6969
content["data"]["attributes"]["topic"] = test_topic
7070
content["data"]["attributes"]["artist"] = test_artist
7171
response = client.patch(url, data=content)
@@ -92,8 +92,8 @@ def test_patch_on_polymorphic_model_without_including_required_field(
9292

9393

9494
def test_polymorphism_on_polymorphic_model_list_post(client):
95-
test_topic = "New test topic {}".format(random.randint(0, 999999))
96-
test_artist = "test-{}".format(random.randint(0, 999999))
95+
test_topic = f"New test topic {random.randint(0, 999999)}"
96+
test_artist = f"test-{random.randint(0, 999999)}"
9797
test_project_type = ProjectTypeFactory()
9898
url = reverse("project-list")
9999
data = {
@@ -152,8 +152,8 @@ def test_polymorphic_model_without_any_instance(client):
152152

153153

154154
def test_invalid_type_on_polymorphic_model(client):
155-
test_topic = "New test topic {}".format(random.randint(0, 999999))
156-
test_artist = "test-{}".format(random.randint(0, 999999))
155+
test_topic = f"New test topic {random.randint(0, 999999)}"
156+
test_artist = f"test-{random.randint(0, 999999)}"
157157
url = reverse("project-list")
158158
data = {
159159
"data": {

example/tests/test_serializers.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ def setUp(self):
4040
rating=3,
4141
)
4242
for i in range(1, 6):
43-
name = "some_author{}".format(i)
43+
name = f"some_author{i}"
4444
self.entry.authors.add(
45-
Author.objects.create(name=name, email="{}@example.org".format(name))
45+
Author.objects.create(name=name, email=f"{name}@example.org")
4646
)
4747

4848
def test_forward_relationship_not_loaded_when_not_included(self):

example/tests/test_views.py

+26-30
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,13 @@ def test_get_entry_relationship_blog(self):
7676

7777
def test_get_entry_relationship_invalid_field(self):
7878
response = self.client.get(
79-
"/entries/{}/relationships/invalid_field".format(self.first_entry.id)
79+
f"/entries/{self.first_entry.id}/relationships/invalid_field"
8080
)
8181

8282
assert response.status_code == 404
8383

8484
def test_get_blog_relationship_entry_set(self):
85-
response = self.client.get(
86-
"/blogs/{}/relationships/entry_set".format(self.blog.id)
87-
)
85+
response = self.client.get(f"/blogs/{self.blog.id}/relationships/entry_set")
8886
expected_data = [
8987
{"type": format_resource_type("Entry"), "id": str(self.first_entry.id)},
9088
{"type": format_resource_type("Entry"), "id": str(self.second_entry.id)},
@@ -94,9 +92,7 @@ def test_get_blog_relationship_entry_set(self):
9492

9593
@override_settings(JSON_API_FORMAT_RELATED_LINKS="dasherize")
9694
def test_get_blog_relationship_entry_set_with_formatted_link(self):
97-
response = self.client.get(
98-
"/blogs/{}/relationships/entry-set".format(self.blog.id)
99-
)
95+
response = self.client.get(f"/blogs/{self.blog.id}/relationships/entry-set")
10096
expected_data = [
10197
{"type": format_resource_type("Entry"), "id": str(self.first_entry.id)},
10298
{"type": format_resource_type("Entry"), "id": str(self.second_entry.id)},
@@ -105,17 +101,17 @@ def test_get_blog_relationship_entry_set_with_formatted_link(self):
105101
assert response.data == expected_data
106102

107103
def test_put_entry_relationship_blog_returns_405(self):
108-
url = "/entries/{}/relationships/blog".format(self.first_entry.id)
104+
url = f"/entries/{self.first_entry.id}/relationships/blog"
109105
response = self.client.put(url, data={})
110106
assert response.status_code == 405
111107

112108
def test_patch_invalid_entry_relationship_blog_returns_400(self):
113-
url = "/entries/{}/relationships/blog".format(self.first_entry.id)
109+
url = f"/entries/{self.first_entry.id}/relationships/blog"
114110
response = self.client.patch(url, data={"data": {"invalid": ""}})
115111
assert response.status_code == 400
116112

117113
def test_relationship_view_errors_format(self):
118-
url = "/entries/{}/relationships/blog".format(self.first_entry.id)
114+
url = f"/entries/{self.first_entry.id}/relationships/blog"
119115
response = self.client.patch(url, data={"data": {"invalid": ""}})
120116
assert response.status_code == 400
121117

@@ -125,14 +121,14 @@ def test_relationship_view_errors_format(self):
125121
assert "errors" in result
126122

127123
def test_get_empty_to_one_relationship(self):
128-
url = "/comments/{}/relationships/author".format(self.first_entry.id)
124+
url = f"/comments/{self.first_entry.id}/relationships/author"
129125
response = self.client.get(url)
130126
expected_data = None
131127

132128
assert response.data == expected_data
133129

134130
def test_get_to_many_relationship_self_link(self):
135-
url = "/authors/{}/relationships/comments".format(self.author.id)
131+
url = f"/authors/{self.author.id}/relationships/comments"
136132

137133
response = self.client.get(url)
138134
expected_data = {
@@ -147,7 +143,7 @@ def test_get_to_many_relationship_self_link(self):
147143
assert json.loads(response.content.decode("utf-8")) == expected_data
148144

149145
def test_patch_to_one_relationship(self):
150-
url = "/entries/{}/relationships/blog".format(self.first_entry.id)
146+
url = f"/entries/{self.first_entry.id}/relationships/blog"
151147
request_data = {
152148
"data": {
153149
"type": format_resource_type("Blog"),
@@ -162,7 +158,7 @@ def test_patch_to_one_relationship(self):
162158
assert response.data == request_data["data"]
163159

164160
def test_patch_one_to_many_relationship(self):
165-
url = "/blogs/{}/relationships/entry_set".format(self.first_entry.id)
161+
url = f"/blogs/{self.first_entry.id}/relationships/entry_set"
166162
request_data = {
167163
"data": [
168164
{"type": format_resource_type("Entry"), "id": str(self.first_entry.id)},
@@ -184,7 +180,7 @@ def test_patch_one_to_many_relationship(self):
184180
assert response.data == request_data["data"]
185181

186182
def test_patch_one_to_many_relaitonship_with_none(self):
187-
url = "/blogs/{}/relationships/entry_set".format(self.first_entry.id)
183+
url = f"/blogs/{self.first_entry.id}/relationships/entry_set"
188184
request_data = {"data": None}
189185
response = self.client.patch(url, data=request_data)
190186
assert response.status_code == 200, response.content.decode()
@@ -194,7 +190,7 @@ def test_patch_one_to_many_relaitonship_with_none(self):
194190
assert response.data == []
195191

196192
def test_patch_many_to_many_relationship(self):
197-
url = "/entries/{}/relationships/authors".format(self.first_entry.id)
193+
url = f"/entries/{self.first_entry.id}/relationships/authors"
198194
request_data = {
199195
"data": [
200196
{"type": format_resource_type("Author"), "id": str(self.author.id)},
@@ -216,7 +212,7 @@ def test_patch_many_to_many_relationship(self):
216212
assert response.data == request_data["data"]
217213

218214
def test_post_to_one_relationship_should_fail(self):
219-
url = "/entries/{}/relationships/blog".format(self.first_entry.id)
215+
url = f"/entries/{self.first_entry.id}/relationships/blog"
220216
request_data = {
221217
"data": {
222218
"type": format_resource_type("Blog"),
@@ -227,7 +223,7 @@ def test_post_to_one_relationship_should_fail(self):
227223
assert response.status_code == 405, response.content.decode()
228224

229225
def test_post_to_many_relationship_with_no_change(self):
230-
url = "/entries/{}/relationships/comments".format(self.first_entry.id)
226+
url = f"/entries/{self.first_entry.id}/relationships/comments"
231227
request_data = {
232228
"data": [
233229
{
@@ -241,7 +237,7 @@ def test_post_to_many_relationship_with_no_change(self):
241237
assert len(response.rendered_content) == 0, response.rendered_content.decode()
242238

243239
def test_post_to_many_relationship_with_change(self):
244-
url = "/entries/{}/relationships/comments".format(self.first_entry.id)
240+
url = f"/entries/{self.first_entry.id}/relationships/comments"
245241
request_data = {
246242
"data": [
247243
{
@@ -256,7 +252,7 @@ def test_post_to_many_relationship_with_change(self):
256252
assert request_data["data"][0] in response.data
257253

258254
def test_delete_to_one_relationship_should_fail(self):
259-
url = "/entries/{}/relationships/blog".format(self.first_entry.id)
255+
url = f"/entries/{self.first_entry.id}/relationships/blog"
260256
request_data = {
261257
"data": {
262258
"type": format_resource_type("Blog"),
@@ -267,7 +263,7 @@ def test_delete_to_one_relationship_should_fail(self):
267263
assert response.status_code == 405, response.content.decode()
268264

269265
def test_delete_relationship_overriding_with_none(self):
270-
url = "/comments/{}".format(self.second_comment.id)
266+
url = f"/comments/{self.second_comment.id}"
271267
request_data = {
272268
"data": {
273269
"type": "comments",
@@ -280,7 +276,7 @@ def test_delete_relationship_overriding_with_none(self):
280276
assert response.data["author"] is None
281277

282278
def test_delete_to_many_relationship_with_no_change(self):
283-
url = "/entries/{}/relationships/comments".format(self.first_entry.id)
279+
url = f"/entries/{self.first_entry.id}/relationships/comments"
284280
request_data = {
285281
"data": [
286282
{
@@ -294,7 +290,7 @@ def test_delete_to_many_relationship_with_no_change(self):
294290
assert len(response.rendered_content) == 0, response.rendered_content.decode()
295291

296292
def test_delete_one_to_many_relationship_with_not_null_constraint(self):
297-
url = "/entries/{}/relationships/comments".format(self.first_entry.id)
293+
url = f"/entries/{self.first_entry.id}/relationships/comments"
298294
request_data = {
299295
"data": [
300296
{
@@ -307,7 +303,7 @@ def test_delete_one_to_many_relationship_with_not_null_constraint(self):
307303
assert response.status_code == 409, response.content.decode()
308304

309305
def test_delete_to_many_relationship_with_change(self):
310-
url = "/authors/{}/relationships/comments".format(self.author.id)
306+
url = f"/authors/{self.author.id}/relationships/comments"
311307
request_data = {
312308
"data": [
313309
{
@@ -323,7 +319,7 @@ def test_new_comment_data_patch_to_many_relationship(self):
323319
entry = EntryFactory(blog=self.blog, authors=(self.author,))
324320
comment = CommentFactory(entry=entry)
325321

326-
url = "/authors/{}/relationships/comments".format(self.author.id)
322+
url = f"/authors/{self.author.id}/relationships/comments"
327323
request_data = {
328324
"data": [
329325
{"type": format_resource_type("Comment"), "id": str(comment.id)},
@@ -597,7 +593,7 @@ def setUp(self):
597593
self.blog = Blog.objects.create(name="Some Blog", tagline="It's a blog")
598594

599595
def test_no_content_response(self):
600-
url = "/blogs/{}".format(self.blog.pk)
596+
url = f"/blogs/{self.blog.pk}"
601597
response = self.client.delete(url)
602598
assert response.status_code == 204, response.rendered_content.decode()
603599
assert len(response.rendered_content) == 0, response.rendered_content.decode()
@@ -618,8 +614,8 @@ def test_get_object_gives_correct_blog(self):
618614
expected = {
619615
"data": {
620616
"attributes": {"name": self.blog.name},
621-
"id": "{}".format(self.blog.id),
622-
"links": {"self": "http://testserver/blogs/{}".format(self.blog.id)},
617+
"id": f"{self.blog.id}",
618+
"links": {"self": f"http://testserver/blogs/{self.blog.id}"},
623619
"meta": {"copyright": datetime.now().year},
624620
"relationships": {"tags": {"data": [], "meta": {"count": 0}}},
625621
"type": "blogs",
@@ -656,13 +652,13 @@ def test_get_object_gives_correct_entry(self):
656652
"modDate": self.second_entry.mod_date,
657653
"pubDate": self.second_entry.pub_date,
658654
},
659-
"id": "{}".format(self.second_entry.id),
655+
"id": f"{self.second_entry.id}",
660656
"meta": {"bodyFormat": "text"},
661657
"relationships": {
662658
"authors": {"data": [], "meta": {"count": 0}},
663659
"blog": {
664660
"data": {
665-
"id": "{}".format(self.second_entry.blog_id),
661+
"id": f"{self.second_entry.blog_id}",
666662
"type": "blogs",
667663
}
668664
},

example/tests/unit/test_default_drf_serializers.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ def test_blog_create(client):
9393
expected = {
9494
"data": {
9595
"attributes": {"name": blog.name, "tags": []},
96-
"id": "{}".format(blog.id),
97-
"links": {"self": "http://testserver/blogs/{}".format(blog.id)},
96+
"id": f"{blog.id}",
97+
"links": {"self": f"http://testserver/blogs/{blog.id}"},
9898
"meta": {"copyright": datetime.now().year},
9999
"type": "blogs",
100100
},
@@ -113,8 +113,8 @@ def test_get_object_gives_correct_blog(client, blog, entry):
113113
expected = {
114114
"data": {
115115
"attributes": {"name": blog.name, "tags": []},
116-
"id": "{}".format(blog.id),
117-
"links": {"self": "http://testserver/blogs/{}".format(blog.id)},
116+
"id": f"{blog.id}",
117+
"links": {"self": f"http://testserver/blogs/{blog.id}"},
118118
"meta": {"copyright": datetime.now().year},
119119
"type": "blogs",
120120
},
@@ -134,8 +134,8 @@ def test_get_object_patches_correct_blog(client, blog, entry):
134134
request_data = {
135135
"data": {
136136
"attributes": {"name": new_name},
137-
"id": "{}".format(blog.id),
138-
"links": {"self": "http://testserver/blogs/{}".format(blog.id)},
137+
"id": f"{blog.id}",
138+
"links": {"self": f"http://testserver/blogs/{blog.id}"},
139139
"meta": {"copyright": datetime.now().year},
140140
"relationships": {"tags": {"data": []}},
141141
"type": "blogs",
@@ -150,8 +150,8 @@ def test_get_object_patches_correct_blog(client, blog, entry):
150150
expected = {
151151
"data": {
152152
"attributes": {"name": new_name, "tags": []},
153-
"id": "{}".format(blog.id),
154-
"links": {"self": "http://testserver/blogs/{}".format(blog.id)},
153+
"id": f"{blog.id}",
154+
"links": {"self": f"http://testserver/blogs/{blog.id}"},
155155
"meta": {"copyright": datetime.now().year},
156156
"type": "blogs",
157157
},

rest_framework_json_api/django_filters/backends.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def _validate_filter(self, keys, filterset_class):
7878
"""
7979
for k in keys:
8080
if (not filterset_class) or (k not in filterset_class.base_filters):
81-
raise ValidationError("invalid filter[{}]".format(k))
81+
raise ValidationError(f"invalid filter[{k}]")
8282

8383
def get_filterset(self, request, queryset, view):
8484
"""
@@ -111,12 +111,10 @@ def get_filterset_kwargs(self, request, queryset, view):
111111
or m.groupdict()["ldelim"] != "["
112112
or m.groupdict()["rdelim"] != "]"
113113
):
114-
raise ValidationError("invalid query parameter: {}".format(qp))
114+
raise ValidationError(f"invalid query parameter: {qp}")
115115
if m and qp != self.search_param:
116116
if not all(val):
117-
raise ValidationError(
118-
"missing value for query parameter {}".format(qp)
119-
)
117+
raise ValidationError(f"missing value for query parameter {qp}")
120118
# convert JSON:API relationship path to Django ORM's __ notation
121119
key = m.groupdict()["assoc"].replace(".", "__")
122120
key = undo_format_field_name(key)

rest_framework_json_api/filters.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,12 @@ def validate_query_params(self, request):
9393
for qp in request.query_params.keys():
9494
m = self.query_regex.match(qp)
9595
if not m:
96-
raise ValidationError("invalid query parameter: {}".format(qp))
96+
raise ValidationError(f"invalid query parameter: {qp}")
9797
if (
9898
not m.group("type") == "filter"
9999
and len(request.query_params.getlist(qp)) > 1
100100
):
101-
raise ValidationError(
102-
"repeated query parameter not allowed: {}".format(qp)
103-
)
101+
raise ValidationError(f"repeated query parameter not allowed: {qp}")
104102

105103
def filter_queryset(self, request, queryset, view):
106104
"""

rest_framework_json_api/relations.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ class PolymorphicResourceRelatedField(ResourceRelatedField):
324324
"Incorrect relation type. Expected one of [{relation_type}], "
325325
"received {received_type}."
326326
),
327-
}
327+
},
328328
)
329329

330330
def __init__(self, polymorphic_serializer, *args, **kwargs):
@@ -370,7 +370,7 @@ def __init__(self, method_name=None, **kwargs):
370370
super().__init__(**kwargs)
371371

372372
def bind(self, field_name, parent):
373-
default_method_name = "get_{field_name}".format(field_name=field_name)
373+
default_method_name = f"get_{field_name}"
374374
if self.method_name is None:
375375
self.method_name = default_method_name
376376
super().bind(field_name, parent)

rest_framework_json_api/serializers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ def get_polymorphic_serializer_for_type(cls, obj_type):
442442
return cls._poly_type_serializer_map[obj_type]
443443
except KeyError:
444444
raise NotImplementedError(
445-
"No polymorphic serializer has been found for type {}".format(obj_type)
445+
f"No polymorphic serializer has been found for type {obj_type}"
446446
)
447447

448448
@classmethod

0 commit comments

Comments
 (0)