Skip to content

Commit 500146a

Browse files
committed
fixup! Add type hints and mypy
linting
1 parent ee7a89f commit 500146a

File tree

5 files changed

+31
-29
lines changed

5 files changed

+31
-29
lines changed

packet/log_utils.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
from packet.context_processors import get_rit_name
1111
from packet.utils import is_freshman_on_floor
1212

13-
F = TypeVar('F', bound=Callable)
13+
WrappedFunc = TypeVar('WrappedFunc', bound=Callable)
1414

15-
def log_time(func: F) -> F:
15+
def log_time(func: WrappedFunc) -> WrappedFunc:
1616
"""
1717
Decorator for logging the execution time of a function
1818
"""
@@ -27,7 +27,7 @@ def wrapped_function(*args: list, **kwargs: dict) -> Any:
2727

2828
return result
2929

30-
return cast(F, wrapped_function)
30+
return cast(WrappedFunc, wrapped_function)
3131

3232

3333
def _format_cache(func: Any) -> str:
@@ -43,7 +43,7 @@ def _format_cache(func: Any) -> str:
4343
_caches = (get_rit_name, ldap.get_member, is_freshman_on_floor)
4444

4545

46-
def log_cache(func: F) -> F:
46+
def log_cache(func: WrappedFunc) -> WrappedFunc:
4747
"""
4848
Decorator for logging cache info
4949
"""
@@ -56,4 +56,4 @@ def wrapped_function(*args: list, **kwargs: dict) -> Any:
5656

5757
return result
5858

59-
return cast(F, wrapped_function)
59+
return cast(WrappedFunc, wrapped_function)

packet/models.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ class Freshman(db.Model):
3838
rit_username = cast(str, Column(String(10), primary_key=True))
3939
name = cast(str, Column(String(64), nullable=False))
4040
onfloor = cast(bool, Column(Boolean, nullable=False))
41-
fresh_signatures = cast(FreshSignature, relationship('FreshSignature'))
41+
fresh_signatures = cast('FreshSignature', relationship('FreshSignature'))
4242

4343
# One freshman can have multiple packets if they repeat the intro process
44-
packets = cast(Packet, relationship('Packet', order_by='desc(Packet.id)'))
44+
packets = cast('Packet', relationship('Packet', order_by='desc(Packet.id)'))
4545

4646
@classmethod
47-
def by_username(cls, username: str) -> Packet:
47+
def by_username(cls, username: str) -> 'Packet':
4848
"""
4949
Helper method to retrieve a freshman by their RIT username
5050
"""
@@ -69,11 +69,12 @@ class Packet(db.Model):
6969

7070
# The `lazy='subquery'` kwarg enables eager loading for signatures which makes signature calculations much faster
7171
# See the docs here for details: https://docs.sqlalchemy.org/en/latest/orm/loading_relationships.html
72-
upper_signatures = cast(UpperSignature, relationship('UpperSignature', lazy='subquery',
72+
upper_signatures = cast('UpperSignature', relationship('UpperSignature', lazy='subquery',
7373
order_by='UpperSignature.signed.desc(), UpperSignature.updated'))
74-
fresh_signatures = cast(FreshSignature, relationship('FreshSignature', lazy='subquery',
74+
fresh_signatures = cast('FreshSignature', relationship('FreshSignature', lazy='subquery',
7575
order_by='FreshSignature.signed.desc(), FreshSignature.updated'))
76-
misc_signatures = cast(MiscSignature, relationship('MiscSignature', lazy='subquery', order_by='MiscSignature.updated'))
76+
misc_signatures = cast('MiscSignature',
77+
relationship('MiscSignature', lazy='subquery', order_by='MiscSignature.updated'))
7778

7879
def is_open(self) -> bool:
7980
return self.start < datetime.now() < self.end

packet/notifications.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from datetime import datetime
22
from typing import Any, Callable, TypeVar, cast
33

4-
import onesignal
4+
import onesignal_sdk as onesignal
55

66
from packet import app, intro_onesignal_client, csh_onesignal_client
77
from packet.models import NotificationSubscription, Packet
@@ -14,21 +14,21 @@
1414
'url': app.config['PROTOCOL'] + app.config['SERVER_NAME']
1515
}
1616

17-
F = TypeVar('F', bound=Callable)
17+
WrappedFunc = TypeVar('WrappedFunc', bound=Callable)
1818

19-
def require_onesignal_intro(func: F) -> F:
19+
def require_onesignal_intro(func: WrappedFunc) -> WrappedFunc:
2020
def require_onesignal_intro_wrapper(*args: list, **kwargs: dict) -> Any:
2121
if intro_onesignal_client:
2222
return func(*args, **kwargs)
2323
return None
24-
return cast(F, require_onesignal_intro_wrapper)
24+
return cast(WrappedFunc, require_onesignal_intro_wrapper)
2525

26-
def require_onesignal_csh(func: F) -> F:
26+
def require_onesignal_csh(func: WrappedFunc) -> WrappedFunc:
2727
def require_onesignal_csh_wrapper(*args: list, **kwargs: dict) -> Any:
2828
if csh_onesignal_client:
2929
return func(*args, **kwargs)
3030
return None
31-
return cast(F, require_onesignal_csh_wrapper)
31+
return cast(WrappedFunc, require_onesignal_csh_wrapper)
3232

3333

3434
def send_notification(notification_body: dict, subscriptions: list, client: onesignal.Client) -> None:
@@ -60,7 +60,8 @@ def packet_signed_notification(packet: Packet, signer: str) -> None:
6060
@require_onesignal_intro
6161
def packet_100_percent_notification(packet: Packet) -> None:
6262
member_subscriptions = NotificationSubscription.query.filter(cast(Any, NotificationSubscription.member).isnot(None))
63-
intro_subscriptions = NotificationSubscription.query.filter(cast(Any, NotificationSubscription.freshman_username).isnot(None))
63+
intro_subscriptions = NotificationSubscription.query.filter(
64+
cast(Any, NotificationSubscription.freshman_username).isnot(None))
6465
if member_subscriptions or intro_subscriptions:
6566
notification_body = post_body
6667
notification_body['contents']['en'] = packet.freshman.name + ' got 💯 on packet!'

packet/stats.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from datetime import date, timedelta
1+
from datetime import date as dateType, timedelta
22
from typing import TypedDict, Union, cast, Callable
33

44
from packet.models import Packet, MiscSignature, UpperSignature
@@ -23,10 +23,10 @@ class SimplePacket(TypedDict):
2323
freshman_username: str
2424

2525
class SigDict(TypedDict):
26-
date: date
26+
date: dateType
2727
packet: SimplePacket
2828

29-
Stats = dict[date, list[str]]
29+
Stats = dict[dateType, list[str]]
3030

3131

3232
def packet_stats(packet_id: int) -> PacketStats:

packet/utils.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44
from datetime import datetime, time, timedelta, date
55
from functools import wraps, lru_cache
6-
from typing import Any, Callable, TypeVar, cast, Union, Literal, TypedDict
6+
from typing import Any, Callable, TypeVar, cast
77

88
import requests
99
from flask import session, redirect
@@ -15,9 +15,9 @@
1515

1616
INTRO_REALM = 'https://sso.csh.rit.edu/auth/realms/intro'
1717

18-
F = TypeVar('F', bound=Callable)
18+
WrappedFunc = TypeVar('WrappedFunc', bound=Callable)
1919

20-
def before_request(func: F) -> F:
20+
def before_request(func: WrappedFunc) -> WrappedFunc:
2121
"""
2222
Credit to Liam Middlebrook and Ram Zallan
2323
https://github.com/liam-middlebrook/gallery
@@ -45,7 +45,7 @@ def wrapped_function(*args: list, **kwargs: dict) -> Any:
4545
kwargs['info'] = info
4646
return func(*args, **kwargs)
4747

48-
return cast(F, wrapped_function)
48+
return cast(WrappedFunc, wrapped_function)
4949

5050

5151
@lru_cache(maxsize=128)
@@ -60,7 +60,7 @@ def is_freshman_on_floor(rit_username: str) -> bool:
6060
return False
6161

6262

63-
def packet_auth(func: F) -> F:
63+
def packet_auth(func: WrappedFunc) -> WrappedFunc:
6464
"""
6565
Decorator for easily configuring oidc
6666
"""
@@ -76,10 +76,10 @@ def wrapped_function(*args: list, **kwargs: dict) -> Any:
7676

7777
return func(*args, **kwargs)
7878

79-
return cast(F, wrapped_function)
79+
return cast(WrappedFunc, wrapped_function)
8080

8181

82-
def admin_auth(func: F) -> F:
82+
def admin_auth(func: WrappedFunc) -> WrappedFunc:
8383
"""
8484
Decorator for easily configuring oidc
8585
"""
@@ -98,7 +98,7 @@ def wrapped_function(*args: list, **kwargs: dict) -> Any:
9898

9999
return func(*args, **kwargs)
100100

101-
return cast(F, wrapped_function)
101+
return cast(WrappedFunc, wrapped_function)
102102

103103

104104
def notify_slack(name: str) -> None:

0 commit comments

Comments
 (0)