Skip to content

Commit

Permalink
Upgrade mypy & django-stubs
Browse files Browse the repository at this point in the history
This was pretty simply generated:

    poetry update mypy django-stubs

After performing the update, 7 errors must be fixed in 3 files!
  • Loading branch information
DavidCain committed Jun 22, 2024
1 parent f4e228c commit d884472
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 59 deletions.
91 changes: 41 additions & 50 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion ws/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,9 @@ def clean_winter_terrain_level(self) -> str | None:
program = self.cleaned_data.get("program")
if program and not enums.Program(program).winter_rules_apply():
return None
return self.cleaned_data.get("winter_terrain_level", "")
level = self.cleaned_data.get("winter_terrain_level", "")
assert isinstance(level, str)
return level

def _init_wimp(self):
"""Configure the WIMP widget, load saved participant if applicable."""
Expand Down
30 changes: 23 additions & 7 deletions ws/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from bs4 import BeautifulSoup
from django.conf import settings
from django.contrib.auth.models import AnonymousUser, User
from django.contrib.contenttypes.models import ContentType, ContentTypeManager
from django.contrib.contenttypes.models import ContentType
from django.contrib.postgres.indexes import GistIndex
from django.contrib.postgres.search import SearchQuery, SearchRank, SearchVector
from django.core.exceptions import ValidationError
Expand Down Expand Up @@ -53,8 +53,9 @@ def delete(self, *args, **kwargs): # pylint: disable=signature-differs

@classmethod
def load(cls) -> Self:
obj, _created = cls.objects.get_or_create(pk=1)
return obj
manager: QuerySet = cls.objects # type:ignore[attr-defined]
obj, _created = manager.get_or_create(pk=1)
return cast(Self, obj)


class Car(models.Model):
Expand Down Expand Up @@ -118,7 +119,7 @@ class Discount(models.Model):
"""Discount at another company available to MITOC members."""

administrators = models.ManyToManyField(
"Participant",
"ws.Participant",
blank=True,
help_text="Persons selected to administer this discount",
related_name="discounts_administered",
Expand Down Expand Up @@ -1857,13 +1858,21 @@ def activity(self) -> str:
If any class wants to break the naming convention, they should
set db_name to be the activity without underscores.
"""
content_type = cast(ContentTypeManager, ContentType.objects).get_for_model(self)
content_type = ContentType.objects.get_for_model(self)
model_name: str = content_type.model
activity = model_name[: model_name.rfind("leaderapplication")]
return "winter_school" if (activity == "winterschool") else activity

@staticmethod
def model_from_activity(activity: enums.Activity) -> type["LeaderApplication"]:
def model_from_activity(
activity: enums.Activity,
) -> (
# Technically, `type[LeaderApplication]` would work too.
# However, being specific about supported applications helps mypy.
type["ClimbingLeaderApplication"]
| type["HikingLeaderApplication"]
| type["WinterSchoolLeaderApplication"]
):
"""Get the specific inheriting child from the activity.
Inverse of activity().
Expand All @@ -1879,7 +1888,14 @@ def model_from_activity(activity: enums.Activity) -> type["LeaderApplication"]:
model_class = content_type.model_class()
if model_class is None:
raise NoApplicationDefinedError(f"No application for {activity.label}")
assert issubclass(model_class, LeaderApplication)
assert issubclass(
model_class,
(
ClimbingLeaderApplication
| HikingLeaderApplication
| WinterSchoolLeaderApplication
),
)
return model_class


Expand Down
8 changes: 7 additions & 1 deletion ws/utils/ratings.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@ def activity_chairs(self) -> QuerySet[User]:

# The model is meant to be a class attribute of SingleObjectMixin
@property
def model(self) -> type[models.LeaderApplication]:
def model(
self,
) -> (
type[models.ClimbingLeaderApplication]
| type[models.HikingLeaderApplication]
| type[models.WinterSchoolLeaderApplication]
):
"""Return the application model for this activity type.
The model will be None if no application exists for the activity.
Expand Down

0 comments on commit d884472

Please sign in to comment.