Skip to content

Commit aa33959

Browse files
slivercn2ygk
andauthored
Removed all deprecations (#1040)
Co-authored-by: Alan Crosswell <[email protected]>
1 parent 8cd79ae commit aa33959

17 files changed

+64
-158
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ any parts of the framework not mentioned in the documentation should generally b
2727
* Removed support for Django 3.0.
2828
* Removed support for Django 3.1.
2929
* Removed support for Python 3.6.
30+
* Removed obsolete method `utils.get_included_serializers`.
31+
* Removed optional `format_type` argument of `utils.format_link_segment`.
32+
* Removed `format_type`s default argument of `utils.format_value`. `format_type` is now required.
3033

3134
## [4.3.0] - 2021-12-10
3235

example/factories.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class Meta:
4242
email = factory.LazyAttribute(lambda x: faker.email())
4343

4444
bio = factory.RelatedFactory("example.factories.AuthorBioFactory", "author")
45-
type = factory.SubFactory(AuthorTypeFactory)
45+
author_type = factory.SubFactory(AuthorTypeFactory)
4646

4747

4848
class AuthorBioFactory(factory.django.DjangoModelFactory):

example/fixtures/blogentry.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
"modified_at": "2016-05-02T10:09:48.277",
7878
"name": "Alice",
7979
"email": "[email protected]",
80-
"type": null
80+
"author_type": null
8181
}
8282
},
8383
{
@@ -88,7 +88,7 @@
8888
"modified_at": "2016-05-02T10:09:57.133",
8989
"name": "Bob",
9090
"email": "[email protected]",
91-
"type": null
91+
"author_type": null
9292
}
9393
},
9494
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Generated by Django 4.0 on 2021-12-29 13:07
2+
3+
import django.db.models.deletion
4+
from django.db import migrations, models
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
("contenttypes", "0002_remove_content_type_name"),
11+
("example", "0010_auto_20210714_0809"),
12+
]
13+
14+
operations = [
15+
migrations.RenameField(
16+
model_name="author",
17+
old_name="type",
18+
new_name="author_type",
19+
),
20+
migrations.AlterField(
21+
model_name="project",
22+
name="polymorphic_ctype",
23+
field=models.ForeignKey(
24+
editable=False,
25+
null=True,
26+
on_delete=django.db.models.deletion.CASCADE,
27+
related_name="polymorphic_%(app_label)s.%(class)s_set+",
28+
to="contenttypes.contenttype",
29+
),
30+
),
31+
]

example/models.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class Meta:
5454
class Author(BaseModel):
5555
name = models.CharField(max_length=50)
5656
email = models.EmailField()
57-
type = models.ForeignKey(AuthorType, null=True, on_delete=models.CASCADE)
57+
author_type = models.ForeignKey(AuthorType, null=True, on_delete=models.CASCADE)
5858

5959
def __str__(self):
6060
return self.name

example/serializers.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,13 @@ class AuthorSerializer(serializers.ModelSerializer):
252252
help_text="help for defaults",
253253
)
254254
initials = serializers.SerializerMethodField()
255-
included_serializers = {"bio": AuthorBioSerializer, "type": AuthorTypeSerializer}
255+
included_serializers = {
256+
"bio": AuthorBioSerializer,
257+
"author_type": AuthorTypeSerializer,
258+
}
256259
related_serializers = {
257260
"bio": "example.serializers.AuthorBioSerializer",
258-
"type": "example.serializers.AuthorTypeSerializer",
261+
"author_type": "example.serializers.AuthorTypeSerializer",
259262
"comments": "example.serializers.CommentSerializer",
260263
"entries": "example.serializers.EntrySerializer",
261264
"first_entry": "example.serializers.EntrySerializer",
@@ -270,7 +273,7 @@ class Meta:
270273
"entries",
271274
"comments",
272275
"first_entry",
273-
"type",
276+
"author_type",
274277
"secrets",
275278
"defaults",
276279
"initials",

example/tests/__snapshots__/test_openapi.ambr

+6-6
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@
124124
},
125125
"relationships": {
126126
"properties": {
127+
"authorType": {
128+
"$ref": "#/components/schemas/reltoone"
129+
},
127130
"bio": {
128131
"$ref": "#/components/schemas/reltoone"
129132
},
@@ -135,9 +138,6 @@
135138
},
136139
"firstEntry": {
137140
"$ref": "#/components/schemas/reltoone"
138-
},
139-
"type": {
140-
"$ref": "#/components/schemas/reltoone"
141141
}
142142
},
143143
"type": "object"
@@ -532,6 +532,9 @@
532532
},
533533
"relationships": {
534534
"properties": {
535+
"authorType": {
536+
"$ref": "#/components/schemas/reltoone"
537+
},
535538
"bio": {
536539
"$ref": "#/components/schemas/reltoone"
537540
},
@@ -543,9 +546,6 @@
543546
},
544547
"firstEntry": {
545548
"$ref": "#/components/schemas/reltoone"
546-
},
547-
"type": {
548-
"$ref": "#/components/schemas/reltoone"
549549
}
550550
},
551551
"type": "object"

example/tests/test_format_keys.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def test_options_format_field_names(db, client):
5959
"bio",
6060
"entries",
6161
"firstEntry",
62-
"type",
62+
"authorType",
6363
"comments",
6464
"secrets",
6565
"defaults",

example/tests/test_model_viewsets.py

-26
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import pytest
21
from django.contrib.auth import get_user_model
32
from django.test import override_settings
43
from django.urls import reverse
@@ -216,28 +215,3 @@ def test_404_error_pointer(self):
216215
response = self.client.get(not_found_url)
217216
assert 404 == response.status_code
218217
assert errors == response.json()
219-
220-
221-
@pytest.mark.django_db
222-
def test_patch_allow_field_type(author, author_type_factory, client):
223-
"""
224-
Verify that type field may be updated.
225-
"""
226-
# TODO remove in next major version 5.0.0 see serializers.ReservedFieldNamesMixin
227-
with pytest.deprecated_call():
228-
author_type = author_type_factory()
229-
url = reverse("author-detail", args=[author.id])
230-
231-
data = {
232-
"data": {
233-
"id": author.id,
234-
"type": "authors",
235-
"relationships": {
236-
"data": {"id": author_type.id, "type": "author-type"}
237-
},
238-
}
239-
}
240-
241-
response = client.patch(url, data=data)
242-
243-
assert response.status_code == 200

example/tests/test_views.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ def test_get_related_serializer_class_many(self):
423423
self.assertEqual(got, EntrySerializer)
424424

425425
def test_get_serializer_comes_from_included_serializers(self):
426-
kwargs = {"pk": self.author.id, "related_field": "type"}
426+
kwargs = {"pk": self.author.id, "related_field": "author_type"}
427427
view = self._get_view(kwargs)
428428
related_serializers = view.get_serializer_class().related_serializers
429429
delattr(view.get_serializer_class(), "related_serializers")
@@ -470,14 +470,14 @@ def test_retrieve_related_single_reverse_lookup(self):
470470
def test_retrieve_related_single(self):
471471
url = reverse(
472472
"author-related",
473-
kwargs={"pk": self.author.type.pk, "related_field": "type"},
473+
kwargs={"pk": self.author.author_type.pk, "related_field": "author_type"},
474474
)
475475
resp = self.client.get(url)
476476
expected = {
477477
"data": {
478478
"type": "authorTypes",
479-
"id": str(self.author.type.id),
480-
"attributes": {"name": str(self.author.type.name)},
479+
"id": str(self.author.author_type.id),
480+
"attributes": {"name": str(self.author.author_type.name)},
481481
}
482482
}
483483
self.assertEqual(resp.status_code, 200)

rest_framework_json_api/parsers.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from rest_framework import parsers
55
from rest_framework.exceptions import ParseError
66

7-
from rest_framework_json_api import exceptions, renderers, serializers
7+
from rest_framework_json_api import exceptions, renderers
88
from rest_framework_json_api.utils import get_resource_name, undo_format_field_names
99

1010

@@ -160,12 +160,8 @@ def parse(self, stream, media_type=None, parser_context=None):
160160
)
161161

162162
# Construct the return data
163-
serializer_class = getattr(view, "serializer_class", None)
164163
parsed_data = {"id": data.get("id")} if "id" in data else {}
165-
# TODO remove in next major version 5.0.0 see serializers.ReservedFieldNamesMixin
166-
if serializer_class is not None:
167-
if issubclass(serializer_class, serializers.PolymorphicModelSerializer):
168-
parsed_data["type"] = data.get("type")
164+
parsed_data["type"] = data.get("type")
169165
parsed_data.update(self.parse_attributes(data))
170166
parsed_data.update(self.parse_relationships(data))
171167
parsed_data.update(self.parse_metadata(result))

rest_framework_json_api/serializers.py

+1-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import warnings
21
from collections import OrderedDict
32
from collections.abc import Mapping
43

@@ -157,7 +156,7 @@ def validate_path(serializer_class, field_path, path):
157156
class ReservedFieldNamesMixin:
158157
"""Ensures that reserved field names are not used and an error raised instead."""
159158

160-
_reserved_field_names = {"meta", "results"}
159+
_reserved_field_names = {"meta", "results", "type"}
161160

162161
def get_fields(self):
163162
fields = super().get_fields()
@@ -171,18 +170,6 @@ def get_fields(self):
171170
f"{', '.join(sorted(found_reserved_field_names))}"
172171
)
173172

174-
if "type" in fields:
175-
# see https://jsonapi.org/format/#document-resource-object-fields
176-
warnings.warn(
177-
DeprecationWarning(
178-
f"Field name 'type' found in serializer class "
179-
f"{self.__class__.__module__}.{self.__class__.__qualname__} "
180-
f"which is not allowed according to the JSON:API spec and "
181-
f"won't be supported anymore in the next major DJA release. "
182-
f"Rename 'type' field to something else. "
183-
)
184-
)
185-
186173
return fields
187174

188175

rest_framework_json_api/utils.py

+3-32
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import inspect
22
import operator
3-
import warnings
43
from collections import OrderedDict
54

65
import inflection
@@ -147,23 +146,14 @@ def undo_format_field_name(field_name):
147146
return field_name
148147

149148

150-
def format_link_segment(value, format_type=None):
149+
def format_link_segment(value):
151150
"""
152151
Takes a string value and returns it with formatted keys as set in `format_type`
153152
or `JSON_API_FORMAT_RELATED_LINKS`.
154153
155154
:format_type: Either 'dasherize', 'camelize', 'capitalize' or 'underscore'
156155
"""
157-
if format_type is None:
158-
format_type = json_api_settings.FORMAT_RELATED_LINKS
159-
else:
160-
warnings.warn(
161-
DeprecationWarning(
162-
"Using `format_type` argument is deprecated."
163-
"Use `format_value` instead."
164-
)
165-
)
166-
156+
format_type = json_api_settings.FORMAT_RELATED_LINKS
167157
return format_value(value, format_type)
168158

169159

@@ -179,15 +169,7 @@ def undo_format_link_segment(value):
179169
return value
180170

181171

182-
def format_value(value, format_type=None):
183-
if format_type is None:
184-
warnings.warn(
185-
DeprecationWarning(
186-
"Using `format_value` without passing on `format_type` argument is deprecated."
187-
"Use `format_field_name` instead."
188-
)
189-
)
190-
format_type = json_api_settings.FORMAT_FIELD_NAMES
172+
def format_value(value, format_type):
191173
if format_type == "dasherize":
192174
# inflection can't dasherize camelCase
193175
value = inflection.underscore(value)
@@ -342,17 +324,6 @@ def get_default_included_resources_from_serializer(serializer):
342324
return list(getattr(meta, "included_resources", []))
343325

344326

345-
def get_included_serializers(serializer):
346-
warnings.warn(
347-
DeprecationWarning(
348-
"Using of `get_included_serializers(serializer)` function is deprecated."
349-
"Use `serializer.included_serializers` instead."
350-
)
351-
)
352-
353-
return getattr(serializer, "included_serializers", dict())
354-
355-
356327
def get_relation_instance(resource_instance, source, serializer):
357328
try:
358329
relation_instance = operator.attrgetter(source)(resource_instance)

setup.cfg

-4
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,6 @@ DJANGO_SETTINGS_MODULE=example.settings.test
5858
filterwarnings =
5959
error::DeprecationWarning
6060
error::PendingDeprecationWarning
61-
# TODO remove in next major version of DJA 5.0.0
62-
# this deprecation warning filter needs to be added as AuthorSerializer is used in
63-
# too many tests which introduced the type field name in tests
64-
ignore:Field name 'type'
6561
testpaths =
6662
example
6763
tests

tests/test_parsers.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def test_parse_formats_field_names(
6363
result = parse(data, parser_context)
6464
assert result == {
6565
"id": "123",
66+
"type": "BasicModel",
6667
"test_attribute": "test-value",
6768
"test_relationship": {"id": "123", "type": "TestRelationship"},
6869
}
@@ -85,7 +86,7 @@ def test_parse_with_default_arguments(self, parse):
8586
},
8687
}
8788
result = parse(data, None)
88-
assert result == {}
89+
assert result == {"type": "BasicModel"}
8990

9091
def test_parse_preserves_json_value_field_names(
9192
self, settings, parse, parser_context

tests/test_serializers.py

-9
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,3 @@ class ReservedFieldNamesSerializer(serializers.Serializer):
5050
"ReservedFieldNamesSerializer uses following reserved field name(s) which is "
5151
"not allowed: meta, results"
5252
)
53-
54-
55-
def test_serializer_fields_deprecated_field_name_type():
56-
with pytest.deprecated_call():
57-
58-
class TypeFieldNameSerializer(serializers.Serializer):
59-
type = serializers.CharField()
60-
61-
TypeFieldNameSerializer().fields

0 commit comments

Comments
 (0)