Skip to content

Commit

Permalink
Resolve mypy errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Goshev committed Feb 23, 2024
1 parent 66430ab commit a440f4b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 23 deletions.
8 changes: 2 additions & 6 deletions .github/workflows/django.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,8 @@ jobs:
key: python-${{ hashFiles('requirements/local.txt') }}-${{ hashFiles('requirements/base.txt') }}
- name: Install dependencies
run: pip install -r requirements/local.txt
- name: Run isort
uses: isort/isort-action@master
- name: Run black
uses: psf/black@stable
- name: Run flake8
run: flake8
- name: Run ruff
uses: chartboost/ruff-action@v1
- name: Type check
run: mypy --config mypy.ini styleguide_example/
- name: Run tests
Expand Down
12 changes: 9 additions & 3 deletions styleguide_example/common/models.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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"))
)
]
29 changes: 22 additions & 7 deletions styleguide_example/testing_examples/models.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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 = (
Expand All @@ -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 = (
Expand All @@ -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()
Expand All @@ -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"))
)
]
27 changes: 20 additions & 7 deletions styleguide_example/testing_examples/selectors/schools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -12,26 +12,39 @@


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)

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

0 comments on commit a440f4b

Please sign in to comment.