Skip to content

Clean up python2 related code crafts #974

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Adam Wróbel <https://adamwrobel.com>
Adam Ziolkowski <[email protected]>
Alan Crosswell <[email protected]>
Anton Shutik <[email protected]>
Asif Saif Uddin <[email protected]>
Beni Keller <[email protected]>
Boris Pleshakov <[email protected]>
Charlie Allatson <[email protected]>
Expand Down
14 changes: 7 additions & 7 deletions rest_framework_json_api/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
]


class SkipDataMixin(object):
class SkipDataMixin:
"""
This workaround skips "data" rendering for relationships
in order to save some sql queries and improve performance
"""

def __init__(self, *args, **kwargs):
super(SkipDataMixin, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)

def get_attribute(self, instance):
raise SkipField
Expand All @@ -49,7 +49,7 @@ class ManyRelatedFieldWithNoData(SkipDataMixin, DRFManyRelatedField):
pass


class HyperlinkedMixin(object):
class HyperlinkedMixin:
self_link_view_name = None
related_link_view_name = None
related_link_lookup_field = "pk"
Expand All @@ -72,7 +72,7 @@ def __init__(self, self_link_view_name=None, related_link_view_name=None, **kwar
# implicit `self` argument to be passed.
self.reverse = reverse

super(HyperlinkedMixin, self).__init__(**kwargs)
super().__init__(**kwargs)

def get_url(self, name, view_name, kwargs, request):
"""
Expand Down Expand Up @@ -197,7 +197,7 @@ def __init__(self, **kwargs):
if model:
self.model = model

super(ResourceRelatedField, self).__init__(**kwargs)
super().__init__(**kwargs)

def use_pk_only_optimization(self):
# We need the real object to determine its type...
Expand Down Expand Up @@ -245,7 +245,7 @@ def to_internal_value(self, data):
received_type=data["type"],
)

return super(ResourceRelatedField, self).to_internal_value(data["id"])
return super().to_internal_value(data["id"])

def to_representation(self, value):
if getattr(self, "pk_field", None) is not None:
Expand Down Expand Up @@ -329,7 +329,7 @@ class PolymorphicResourceRelatedField(ResourceRelatedField):

def __init__(self, polymorphic_serializer, *args, **kwargs):
self.polymorphic_serializer = polymorphic_serializer
super(PolymorphicResourceRelatedField, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)

def use_pk_only_optimization(self):
return False
Expand Down
18 changes: 5 additions & 13 deletions rest_framework_json_api/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,12 +495,10 @@ def render_relationship_view(
links = view.get_links()
if links:
render_data.update({"links": links}),
return super(JSONRenderer, self).render(
render_data, accepted_media_type, renderer_context
)
return super().render(render_data, accepted_media_type, renderer_context)

def render_errors(self, data, accepted_media_type=None, renderer_context=None):
return super(JSONRenderer, self).render(
return super().render(
utils.format_errors(data), accepted_media_type, renderer_context
)

Expand All @@ -522,9 +520,7 @@ def render(self, data, accepted_media_type=None, renderer_context=None):
# be None
response = renderer_context.get("response", None)
if response is not None and response.status_code == 204:
return super(JSONRenderer, self).render(
None, accepted_media_type, renderer_context
)
return super().render(None, accepted_media_type, renderer_context)

from rest_framework_json_api.views import RelationshipView

Expand All @@ -536,9 +532,7 @@ def render(self, data, accepted_media_type=None, renderer_context=None):
# If `resource_name` is set to None then render default as the dev
# wants to build the output format manually.
if resource_name is None or resource_name is False:
return super(JSONRenderer, self).render(
data, accepted_media_type, renderer_context
)
return super().render(data, accepted_media_type, renderer_context)

json_api_data = data
# initialize json_api_meta with pagination meta or an empty dict
Expand Down Expand Up @@ -664,9 +658,7 @@ def render(self, data, accepted_media_type=None, renderer_context=None):
if json_api_meta:
render_data["meta"] = utils.format_field_names(json_api_meta)

return super(JSONRenderer, self).render(
render_data, accepted_media_type, renderer_context
)
return super().render(render_data, accepted_media_type, renderer_context)


class BrowsableAPIRenderer(renderers.BrowsableAPIRenderer):
Expand Down
16 changes: 7 additions & 9 deletions rest_framework_json_api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def __init__(self, *args, **kwargs):
self.model_class = kwargs.pop("model_class", self.model_class)
# this has no fields but assumptions are made elsewhere that self.fields exists.
self.fields = {}
super(ResourceIdentifierObjectSerializer, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)

def to_representation(self, instance):
return {
Expand All @@ -69,15 +69,15 @@ def to_internal_value(self, data):
self.fail("incorrect_type", data_type=type(data["pk"]).__name__)


class SparseFieldsetsMixin(object):
class SparseFieldsetsMixin:
"""
A serializer mixin that adds support for sparse fieldsets through `fields` query parameter.

Specification: https://jsonapi.org/format/#fetching-sparse-fieldsets
"""

def __init__(self, *args, **kwargs):
super(SparseFieldsetsMixin, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)
context = kwargs.get("context")
request = context.get("request") if context else None

Expand Down Expand Up @@ -107,7 +107,7 @@ def __init__(self, *args, **kwargs):
self.fields.pop(field_name)


class IncludedResourcesValidationMixin(object):
class IncludedResourcesValidationMixin:
"""
A serializer mixin that adds validation of `include` query parameter to
support compound documents.
Expand Down Expand Up @@ -150,7 +150,7 @@ def validate_path(serializer_class, field_path, path):
this_serializer_class, included_field_path, included_field_name
)

super(IncludedResourcesValidationMixin, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)


class LazySerializersDict(Mapping):
Expand Down Expand Up @@ -302,9 +302,7 @@ class PolymorphicSerializerMetaclass(SerializerMetaclass):
"""

def __new__(cls, name, bases, attrs):
new_class = super(PolymorphicSerializerMetaclass, cls).__new__(
cls, name, bases, attrs
)
new_class = super().__new__(cls, name, bases, attrs)

# Ensure initialization is only performed for subclasses of PolymorphicModelSerializer
# (excluding PolymorphicModelSerializer class itself).
Expand Down Expand Up @@ -363,7 +361,7 @@ def get_fields(self):
raise Exception(
"Cannot get fields from a polymorphic serializer given a queryset"
)
return super(PolymorphicModelSerializer, self).get_fields()
return super().get_fields()

@classmethod
def get_polymorphic_serializer_for_instance(cls, instance):
Expand Down
2 changes: 1 addition & 1 deletion rest_framework_json_api/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
}


class JSONAPISettings(object):
class JSONAPISettings:
"""
A settings object that allows JSON:API settings to be access as
properties.
Expand Down
12 changes: 6 additions & 6 deletions rest_framework_json_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
)


class PreloadIncludesMixin(object):
class PreloadIncludesMixin:
"""
This mixin provides a helper attributes to select or prefetch related models
based on the include specified in the URL.
Expand Down Expand Up @@ -60,7 +60,7 @@ def get_prefetch_related(self, include):
return getattr(self, "prefetch_for_includes", {}).get(include, None)

def get_queryset(self, *args, **kwargs):
qs = super(PreloadIncludesMixin, self).get_queryset(*args, **kwargs)
qs = super().get_queryset(*args, **kwargs)

included_resources = get_included_resources(
self.request, self.get_serializer_class()
Expand All @@ -78,10 +78,10 @@ def get_queryset(self, *args, **kwargs):
return qs


class AutoPrefetchMixin(object):
class AutoPrefetchMixin:
def get_queryset(self, *args, **kwargs):
"""This mixin adds automatic prefetching for OneToOne and ManyToMany fields."""
qs = super(AutoPrefetchMixin, self).get_queryset(*args, **kwargs)
qs = super().get_queryset(*args, **kwargs)

included_resources = get_included_resources(
self.request, self.get_serializer_class()
Expand Down Expand Up @@ -127,7 +127,7 @@ def get_queryset(self, *args, **kwargs):
return qs


class RelatedMixin(object):
class RelatedMixin:
"""
This mixin handles all related entities, whose Serializers are declared in "related_serializers"
"""
Expand Down Expand Up @@ -239,7 +239,7 @@ def get_serializer_class(self):
return self.serializer_class

def __init__(self, **kwargs):
super(RelationshipView, self).__init__(**kwargs)
super().__init__(**kwargs)
# We include this simply for dependency injection in tests.
# We can't add it as a class attributes or it would expect an
# implicit `self` argument to be passed.
Expand Down