From fc366d1bd40ead028bef71ab1a0264b451036bc2 Mon Sep 17 00:00:00 2001 From: Daniel Goshev Date: Fri, 23 Feb 2024 14:37:38 +0200 Subject: [PATCH] Resolve mypy errors. --- styleguide_example/common/models.py | 12 ++++++-- styleguide_example/testing_examples/models.py | 29 ++++++++++++++----- .../testing_examples/selectors/schools.py | 27 ++++++++++++----- 3 files changed, 51 insertions(+), 17 deletions(-) diff --git a/styleguide_example/common/models.py b/styleguide_example/common/models.py index b96d58f4..1a1cf93d 100644 --- a/styleguide_example/common/models.py +++ b/styleguide_example/common/models.py @@ -1,5 +1,5 @@ from django.db import models -from django.db.models.query import F, Q +from django.db.models import F, Q from django.utils import timezone @@ -29,7 +29,13 @@ class RandomModel(BaseModel): start_date = models.DateField() end_date = models.DateField() - simple_objects = models.ManyToManyField(SimpleModel, blank=True, related_name="random_objects") + simple_objects = models.ManyToManyField( + SimpleModel, blank=True, related_name="random_objects" + ) class Meta: - constraints = [models.CheckConstraint(name="start_date_before_end_date", check=Q(start_date__lt=F("end_date")))] + constraints = [ + models.CheckConstraint( + name="start_date_before_end_date", check=Q(start_date__lt=F("end_date")) + ) + ] diff --git a/styleguide_example/testing_examples/models.py b/styleguide_example/testing_examples/models.py index 990e7b1d..47dd3d57 100644 --- a/styleguide_example/testing_examples/models.py +++ b/styleguide_example/testing_examples/models.py @@ -1,7 +1,7 @@ from uuid import uuid4 from django.db import models -from django.db.models.query import F, Q +from django.db.models import F, Q class School(models.Model): @@ -15,7 +15,9 @@ def __str__(self): class Student(models.Model): email = models.EmailField(max_length=255) identifier = models.UUIDField(default=uuid4) - school = models.ForeignKey(School, related_name="students", on_delete=models.CASCADE) + school = models.ForeignKey( + School, related_name="students", on_delete=models.CASCADE + ) class Meta: unique_together = ( @@ -30,14 +32,19 @@ def __str__(self): class SchoolCourse(models.Model): name = models.CharField(max_length=255) slug = models.SlugField(max_length=255) - school = models.ForeignKey(School, related_name="school_courses", on_delete=models.CASCADE) + school = models.ForeignKey( + School, related_name="school_courses", on_delete=models.CASCADE + ) start_date = models.DateField() end_date = models.DateField() class Meta: constraints = [ - models.CheckConstraint(name="school_course_start_before_end", check=Q(start_date__lt=F("end_date"))) + models.CheckConstraint( + name="school_course_start_before_end", + check=Q(start_date__lt=F("end_date")), + ) ] unique_together = ( @@ -54,8 +61,12 @@ def __str__(self): class Roster(models.Model): - student = models.ForeignKey(Student, related_name="rosters", on_delete=models.CASCADE) - school_course = models.ForeignKey(SchoolCourse, related_name="rosters", on_delete=models.CASCADE) + student = models.ForeignKey( + Student, related_name="rosters", on_delete=models.CASCADE + ) + school_course = models.ForeignKey( + SchoolCourse, related_name="rosters", on_delete=models.CASCADE + ) start_date = models.DateField() end_date = models.DateField() @@ -64,4 +75,8 @@ class Roster(models.Model): deactivated_at = models.DateField(null=True, blank=True) class Meta: - constraints = [models.CheckConstraint(name="roster_start_before_end", check=Q(start_date__lt=F("end_date")))] + constraints = [ + models.CheckConstraint( + name="roster_start_before_end", check=Q(start_date__lt=F("end_date")) + ) + ] diff --git a/styleguide_example/testing_examples/selectors/schools.py b/styleguide_example/testing_examples/selectors/schools.py index cec3d615..85cdb0a4 100644 --- a/styleguide_example/testing_examples/selectors/schools.py +++ b/styleguide_example/testing_examples/selectors/schools.py @@ -2,7 +2,7 @@ from typing import Optional from django.core.exceptions import ValidationError -from django.db.models.query import Q, QuerySet +from django.db.models import Q, QuerySet from styleguide_example.testing_examples.models import School, SchoolCourse @@ -12,7 +12,10 @@ def school_list_school_courses( - *, school: School, start_date: Optional[date] = None, end_date: Optional[date] = None + *, + school: School, + start_date: Optional[date] = None, + end_date: Optional[date] = None, ) -> QuerySet[SchoolCourse]: if start_date is None and end_date: raise ValidationError(SCHOOL_LIST_SCHOOL_COURSES_PROVIDE_START_DATE_MSG) @@ -20,18 +23,28 @@ def school_list_school_courses( school_courses = school.school_courses.order_by("start_date") if start_date and end_date: - started_courses_Q = Q(start_date__lte=start_date, end_date__gte=start_date, end_date__lte=end_date) + started_courses_Q = Q( + start_date__lte=start_date, end_date__gte=start_date, end_date__lte=end_date + ) courses_in_period_q = Q(start_date__gte=start_date, end_date__lte=end_date) - courses_wrapping_period_q = Q(start_date__lte=start_date, end_date__gte=end_date) - future_course_q = Q(start_date__gte=start_date, start_date__lte=end_date, end_date__gte=end_date) + courses_wrapping_period_q = Q( + start_date__lte=start_date, end_date__gte=end_date + ) + future_course_q = Q( + start_date__gte=start_date, start_date__lte=end_date, end_date__gte=end_date + ) return school_courses.filter( - started_courses_Q | courses_in_period_q | courses_wrapping_period_q | future_course_q + started_courses_Q + | courses_in_period_q + | courses_wrapping_period_q + | future_course_q ) if start_date and end_date is None: return school_courses.filter( - Q(start_date__gte=start_date) | Q(start_date__lte=start_date, end_date__gte=start_date) + Q(start_date__gte=start_date) + | Q(start_date__lte=start_date, end_date__gte=start_date) ) return school_courses