Skip to content
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

Solving mypy errors #406

Merged
merged 3 commits into from
Oct 7, 2023
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
23 changes: 12 additions & 11 deletions backend/authentication/serializers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
from typing import Any, Dict, Union

from django.utils.translation import gettext as _
from rest_framework import serializers
Expand All @@ -13,12 +14,12 @@
from .models import Support, SupportEntityType, User, UserResource, UserTask, UserTopic


class SupportEntityTypeSerializer(serializers.ModelSerializer):
class SupportEntityTypeSerializer(serializers.ModelSerializer[SupportEntityType]):
class Meta:
model = SupportEntityType
fields = "__all__"

def validate(self, data):
def validate(self, data: Dict[str, Union[str, Any]]) -> Dict[str, Union[str, Any]]:
validate_empty(data["name"], "name")

if len(data["name"]) < 3:
Expand All @@ -29,12 +30,12 @@ def validate(self, data):
return data


class SupportSerializer(serializers.ModelSerializer):
class SupportSerializer(serializers.ModelSerializer[Support]):
class Meta:
model = Support
fields = "__all__"

def validate(self, data):
def validate(self, data: Dict[str, Union[str, int]]) -> Dict[str, Union[str, int]]:
if data["supporter_entity"] == data["supported_entity"]:
raise serializers.ValidationError(
_(
Expand All @@ -51,7 +52,7 @@ class Meta:
model = User
fields = "__all__"

def validate(self, data):
def validate(self, data: Dict[str, Union[str, Any]]) -> Dict[str, Union[str, Any]]:
validate_empty(data["password"], "password")
validate_empty(data["name"], "name")
validate_empty(data["user_name"], "user_name")
Expand All @@ -71,36 +72,36 @@ def validate(self, data):
return data


class UserResourceSerializer(serializers.ModelSerializer):
class UserResourceSerializer(serializers.ModelSerializer[UserResource]):
class Meta:
model = UserResource
fields = "__all__"

def validate(self, data):
def validate(self, data: Dict[str, Union[str, int]]) -> Dict[str, Union[str, int]]:
validate_object_existence(User, data["user_id"])
validate_object_existence(Resource, data["resource_id"])

return data


class UserTaskSerializer(serializers.ModelSerializer):
class UserTaskSerializer(serializers.ModelSerializer[UserTask]):
class Meta:
model = UserTask
fields = "__all__"

def validate(self, data):
def validate(self, data: Dict[str, Union[str, int]]) -> Dict[str, Union[str, int]]:
validate_object_existence(User, data["user_id"])
validate_object_existence(Task, data["task_id"])

return data


class UserTopicSerializer(serializers.ModelSerializer):
class UserTopicSerializer(serializers.ModelSerializer[UserTopic]):
class Meta:
model = UserTopic
fields = "__all__"

def validate(self, data):
def validate(self, data: Dict[str, Union[str, int]]) -> Dict[str, Union[str, int]]:
validate_object_existence(User, data["user_id"])
validate_object_existence(Topic, data["topic_id"])

Expand Down
12 changes: 6 additions & 6 deletions backend/authentication/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,31 @@
)


class SupportEntityTypeViewSet(viewsets.ModelViewSet):
class SupportEntityTypeViewSet(viewsets.ModelViewSet[SupportEntityType]):
queryset = SupportEntityType.objects.all()
serializer_class = SupportEntityTypeSerializer


class SupportViewSet(viewsets.ModelViewSet):
class SupportViewSet(viewsets.ModelViewSet[Support]):
queryset = Support.objects.all()
serializer_class = SupportSerializer


class UserViewSet(viewsets.ModelViewSet):
class UserViewSet(viewsets.ModelViewSet[User]):
queryset = User.objects.all()
serializer_class = UserSerializer


class UserResourceViewSet(viewsets.ModelViewSet):
class UserResourceViewSet(viewsets.ModelViewSet[UserResource]):
queryset = UserResource.objects.all()
serializer_class = UserResourceSerializer


class UserTaskViewSet(viewsets.ModelViewSet):
class UserTaskViewSet(viewsets.ModelViewSet[UserTask]):
queryset = UserTask.objects.all()
serializer_class = UserTaskSerializer


class UserTopicViewSet(viewsets.ModelViewSet):
class UserTopicViewSet(viewsets.ModelViewSet[UserTopic]):
queryset = UserTopic.objects.all()
serializer_class = UserTopicSerializer
3 changes: 2 additions & 1 deletion backend/backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
import django_stubs_ext
import dotenv
from django.core.management.utils import get_random_secret_key
from rest_framework import viewsets

django_stubs_ext.monkeypatch()
django_stubs_ext.monkeypatch(extra_classes=(viewsets.ModelViewSet,))
dotenv.load_dotenv()


Expand Down
21 changes: 11 additions & 10 deletions backend/content/serializers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
from typing import Any, Dict, Union

from django.utils.translation import gettext as _
from rest_framework import serializers
Expand All @@ -15,12 +16,12 @@
from .models import Resource, ResourceTopic, Task, Topic, TopicFormat


class ResourceSerializer(serializers.ModelSerializer):
class ResourceSerializer(serializers.ModelSerializer[Resource]):
class Meta:
model = Resource
fields = "__all__"

def validate(self, data):
def validate(self, data: Dict[str, Union[str, Any]]) -> Dict[str, Union[str, Any]]:
validate_empty(data["name"], "name")
validate_empty(data["description"], "description")
validate_empty(data["url"], "url")
Expand All @@ -43,12 +44,12 @@ def validate(self, data):
return data


class TaskSerializer(serializers.ModelSerializer):
class TaskSerializer(serializers.ModelSerializer[Task]):
class Meta:
model = Task
fields = "__all__"

def validate(self, data):
def validate(self, data: Dict[str, Union[str, int]]) -> Dict[str, Union[str, int]]:
validate_empty(data["name"], "name")
validate_empty(data["description"], "description")
validate_empty(data["location"], "location")
Expand All @@ -57,12 +58,12 @@ def validate(self, data):
return data


class TopicSerializer(serializers.ModelSerializer):
class TopicSerializer(serializers.ModelSerializer[Topic]):
class Meta:
model = Topic
fields = "__all__"

def validate(self, data):
def validate(self, data: Dict[str, Union[str, int]]) -> Dict[str, Union[str, int]]:
validate_empty(data["name"], "name")
validate_empty(data["description"], "description")

Expand All @@ -81,24 +82,24 @@ def validate(self, data):
return data


class ResourceTopicSerializer(serializers.ModelSerializer):
class ResourceTopicSerializer(serializers.ModelSerializer[ResourceTopic]):
class Meta:
model = ResourceTopic
fields = "__all__"

def validate(self, data):
def validate(self, data: Dict[str, Union[str, int]]) -> Dict[str, Union[str, int]]:
validate_object_existence(Resource, data["resource_id"])
validate_object_existence(Topic, data["topic_id"])

return data


class TopicFormatSerializer(serializers.ModelSerializer):
class TopicFormatSerializer(serializers.ModelSerializer[TopicFormat]):
class Meta:
model = TopicFormat
fields = "__all__"

def validate(self, data):
def validate(self, data: Dict[str, Union[str, int]]) -> Dict[str, Union[str, int]]:
validate_object_existence(Topic, data["topic_id"])
validate_object_existence(Format, data["format_id"])

Expand Down
10 changes: 5 additions & 5 deletions backend/content/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@
)


class ResourceViewSet(viewsets.ModelViewSet):
class ResourceViewSet(viewsets.ModelViewSet[Resource]):
queryset = Resource.objects.all()
serializer_class = ResourceSerializer


class TaskViewSet(viewsets.ModelViewSet):
class TaskViewSet(viewsets.ModelViewSet[Task]):
queryset = Task.objects.all()
serializer_class = TaskSerializer


class TopicViewSet(viewsets.ModelViewSet):
class TopicViewSet(viewsets.ModelViewSet[Topic]):
queryset = Topic.objects.all()
serializer_class = TopicSerializer


class ResourceTopicViewSet(viewsets.ModelViewSet):
class ResourceTopicViewSet(viewsets.ModelViewSet[ResourceTopic]):
queryset = ResourceTopic.objects.all()
serializer_class = ResourceTopicSerializer


class TopicFormatViewSet(viewsets.ModelViewSet):
class TopicFormatViewSet(viewsets.ModelViewSet[TopicFormat]):
queryset = TopicFormat.objects.all()
serializer_class = TopicFormatSerializer
Loading