Skip to content

Commit

Permalink
📚 add type safety and type hinting to pmobile
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleyzhang01 committed Nov 5, 2024
1 parent fc30c55 commit 2264a3b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
13 changes: 11 additions & 2 deletions backend/pennmobile/admin.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
# CUSTOM ADMIN SETTUP FOR PENN MOBILE
from typing import Any, Dict, Optional, Type, TypeAlias

from django.contrib import admin, messages
from django.contrib.admin.apps import AdminConfig
from django.db.models import Model
from django.http import HttpRequest
from django.urls import reverse
from django.utils.html import format_html


def add_post_poll_message(request, model):
ModelType: TypeAlias = Type[Model]
AdminContext: TypeAlias = Dict[str, Any]
MessageText: TypeAlias = str


def add_post_poll_message(request: HttpRequest, model: ModelType) -> None:
if (count := model.objects.filter(model.ACTION_REQUIRED_CONDITION).count()) > 0:
link = reverse(f"admin:{model._meta.app_label}_{model._meta.model_name}_changelist")
messages.info(
Expand All @@ -21,7 +30,7 @@ def add_post_poll_message(request, model):
class CustomAdminSite(admin.AdminSite):
site_header = "Penn Mobile Backend Admin"

def index(self, request, extra_context=None):
def index(self, request: HttpRequest, extra_context: Optional[AdminContext] = None) -> Any:
from portal.models import Poll, Post

add_post_poll_message(request, Post)
Expand Down
3 changes: 2 additions & 1 deletion backend/pennmobile/analytics.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from enum import Enum
from typing import Optional

from analytics.analytics import AnalyticsTxn, LabsAnalytics, Product

Expand All @@ -23,7 +24,7 @@ class Metric(str, Enum):
PORTAL_POLL_VOTED = "portal.poll.voted"


def record_analytics(metric: Metric, username=None):
def record_analytics(metric: Metric, username: Optional[str] = None) -> None:
if not AnalyticsEngine:
print("AnalyticsEngine not initialized")
return
Expand Down
2 changes: 1 addition & 1 deletion backend/pennmobile/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@


@app.task(bind=True)
def debug_task(self):
def debug_task(self) -> None:
print(f"Request: {self.request!r}")
6 changes: 3 additions & 3 deletions backend/pennmobile/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from xmlrunner.extra.djangotestrunner import XMLTestRunner


def check_wharton(*args):
def check_wharton(*args) -> bool:
return False


Expand All @@ -20,12 +20,12 @@ def submit(self, txn):
class MobileTestCIRunner(XMLTestRunner):
@mock.patch("analytics.analytics.LabsAnalytics", MockLabsAnalytics)
@mock.patch("gsr_booking.models.GroupMembership.check_wharton", check_wharton)
def run_tests(self, test_labels, **kwargs):
def run_tests(self, test_labels, **kwargs) -> None:
return super().run_tests(test_labels, **kwargs)


class MobileTestLocalRunner(DiscoverRunner):
@mock.patch("analytics.analytics.LabsAnalytics", MockLabsAnalytics)
@mock.patch("gsr_booking.models.GroupMembership.check_wharton", check_wharton)
def run_tests(self, test_labels, **kwargs):
def run_tests(self, test_labels, **kwargs) -> None:
return super().run_tests(test_labels, **kwargs)

0 comments on commit 2264a3b

Please sign in to comment.