Skip to content

Commit 5e627d4

Browse files
chore: update typehints via __future__.annotations
This commit changes all typehints from "old" ways of typehinting (type comments, `Union`, etc.) to more modern ways, like using | notation for unions, etc.
1 parent edec103 commit 5e627d4

File tree

17 files changed

+146
-124
lines changed

17 files changed

+146
-124
lines changed

intranet/apps/dataimport/management/commands/import_students.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import csv
24
import sys
35

@@ -54,7 +56,7 @@ def generate_single_username(
5456

5557
return f"{graduating_year}{first_stripped[0]}{last_stripped[:7]}".lower()
5658

57-
def find_next_available_username(self, used_username: str, username_set: set = None) -> str:
59+
def find_next_available_username(self, used_username: str, username_set: set[str] | None = None) -> str:
5860
"""Find the next available username.
5961
6062
Args:

intranet/apps/eighth/forms/admin/activities.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
from __future__ import annotations
2+
13
import logging
2-
from typing import List # noqa
34

45
from django import forms, http
56
from django.contrib.auth import get_user_model
@@ -11,7 +12,7 @@
1112

1213

1314
class ActivityDisplayField(forms.ModelChoiceField):
14-
cancelled_acts = None # type: List[EighthActivity]
15+
cancelled_acts: list[EighthActivity] | None = None
1516

1617
def __init__(self, *args, **kwargs):
1718
if "block" in kwargs:

intranet/apps/eighth/models.py

Lines changed: 42 additions & 39 deletions
Large diffs are not rendered by default.

intranet/apps/eighth/tasks.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import calendar
24
import datetime
35
from typing import Collection

intranet/apps/eighth/views/admin/groups.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
from __future__ import annotations
2+
13
import csv
24
import logging
35
import re
4-
from typing import List, Optional
56

67
from cacheops import invalidate_model, invalidate_obj
78
from django import http
@@ -155,7 +156,7 @@ def get_file_string(fileobj):
155156
return filetext
156157

157158

158-
def get_user_info(key: str, val) -> Optional[List[User]]:
159+
def get_user_info(key: str, val) -> list[User] | None:
159160
if key in ["username", "id"]:
160161
try:
161162
u = get_user_model().objects.filter(**{key: val})
@@ -199,7 +200,7 @@ def handle_group_input(filetext: str):
199200
return find_users_input(lines)
200201

201202

202-
def find_users_input(lines: List[str]):
203+
def find_users_input(lines: list[str]):
203204
sure_users = []
204205
unsure_users = []
205206
for line in lines:
@@ -486,7 +487,7 @@ def eighth_admin_signup_group_action(request, group_id, schact_id):
486487
)
487488

488489

489-
def eighth_admin_perform_group_signup(*, group_id: int, schact_id: int, request: Optional[http.HttpRequest], skip_users: set):
490+
def eighth_admin_perform_group_signup(*, group_id: int, schact_id: int, request: http.HttpRequest | None, skip_users: set):
490491
"""Performs sign up of all users in a specific group up for a
491492
specific scheduled activity.
492493

intranet/apps/eighth/views/admin/hybrid.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
from __future__ import annotations
2+
13
import logging
2-
from typing import Optional
34

45
from django import http
56
from django.contrib import messages
@@ -212,7 +213,7 @@ def eighth_admin_signup_group_action_hybrid(request, group_id, schact_virtual_id
212213
)
213214

214215

215-
def eighth_admin_perform_group_signup(*, group_id: int, schact_virtual_id: int, schact_person_id: int, request: Optional[http.HttpRequest]):
216+
def eighth_admin_perform_group_signup(*, group_id: int, schact_virtual_id: int, schact_person_id: int, request: http.HttpRequest | None):
216217
"""Performs sign up of all users in a specific group up for a
217218
specific scheduled activity.
218219

intranet/apps/features/helpers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from typing import Optional
1+
from __future__ import annotations
22

33

4-
def get_feature_context(request) -> Optional[str]:
4+
def get_feature_context(request) -> str | None:
55
"""Given a Django request, returns the 'context' that should be used to select feature
66
announcements to display (one of ``dashboard``, ``login``, ``eighth_signup``, or ``None``).
77

intranet/apps/notifications/emails.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
from __future__ import annotations
2+
13
import logging
2-
from typing import Collection, Mapping
4+
from typing import Mapping, MutableSequence
35

46
from django.conf import settings
57
from django.core.mail import EmailMultiAlternatives
@@ -13,11 +15,11 @@ def email_send(
1315
html_template: str,
1416
data: Mapping[str, object],
1517
subject: str,
16-
emails: Collection[str], # pylint: disable=unsubscriptable-object
17-
headers: Mapping[str, str] = None, # pylint: disable=unsubscriptable-object
18+
emails: MutableSequence[str],
19+
headers: Mapping[str, str] | None = None,
1820
bcc: bool = False,
1921
*,
20-
custom_logger: logging.Logger = None,
22+
custom_logger: logging.Logger | None = None,
2123
) -> EmailMultiAlternatives:
2224
"""Send an HTML/Plaintext email with the following fields.
2325
If we are not in production and settings.FORCE_EMAIL_SEND is not set, does not actually send the email

intranet/apps/printing/views.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
from __future__ import annotations
2+
13
import logging
24
import math
35
import os
46
import re
57
import subprocess
68
import tempfile
79
from io import BytesIO
8-
from typing import Dict, Optional
910

1011
import magic
1112
from django.conf import settings
@@ -62,14 +63,15 @@ def set_user_ratelimit_status(username: str) -> None:
6263
cache.incr(cache_key)
6364

6465

65-
def get_printers() -> Dict[str, str]:
66+
def get_printers() -> dict[str, str] | list:
6667
"""Returns a dictionary mapping name:description for available printers.
6768
6869
This requires that a CUPS client be configured on the server.
6970
Otherwise, this returns an empty dictionary.
7071
7172
Returns:
72-
A dictionary mapping name:description for available printers.
73+
A dictionary mapping name:description for available printers, or
74+
an empty list if cups isn't installed or lpstat fails
7375
"""
7476

7577
key = "printing:printers"
@@ -116,7 +118,7 @@ def get_printers() -> Dict[str, str]:
116118
return printers
117119

118120

119-
def convert_soffice(tmpfile_name: str) -> Optional[str]:
121+
def convert_soffice(tmpfile_name: str) -> str | None:
120122
"""Converts a doc or docx to a PDF with soffice.
121123
122124
Args:
@@ -147,7 +149,7 @@ def convert_soffice(tmpfile_name: str) -> Optional[str]:
147149
return None
148150

149151

150-
def convert_pdf(tmpfile_name: str, cmdname: str = "ps2pdf") -> Optional[str]:
152+
def convert_pdf(tmpfile_name: str, cmdname: str = "ps2pdf") -> str | None:
151153
new_name = f"{tmpfile_name}.pdf"
152154
try:
153155
output = subprocess.check_output([cmdname, tmpfile_name, new_name], stderr=subprocess.STDOUT, universal_newlines=True)
@@ -209,7 +211,7 @@ def get_mimetype(tmpfile_name: str) -> str:
209211
return mimetype
210212

211213

212-
def convert_file(tmpfile_name: str, orig_fname: str) -> Optional[str]:
214+
def convert_file(tmpfile_name: str, orig_fname: str) -> str | None:
213215
detected = get_mimetype(tmpfile_name)
214216

215217
add_breadcrumb(category="printing", message=f"Detected file type {detected}", level="debug")
@@ -241,7 +243,7 @@ def convert_file(tmpfile_name: str, orig_fname: str) -> Optional[str]:
241243
raise InvalidInputPrintingError(f"Invalid file type {detected}")
242244

243245

244-
def check_page_range(page_range: str, max_pages: int) -> Optional[int]:
246+
def check_page_range(page_range: str, max_pages: int) -> int | None:
245247
"""Returns the number of pages included in the range, or None if it is an invalid range.
246248
247249
Args:

intranet/apps/signage/views.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
from __future__ import annotations
2+
13
import datetime
24
import logging
3-
from typing import Optional
45

56
from django import http
67
from django.conf import settings
@@ -20,7 +21,7 @@
2021
logger = logging.getLogger(__name__)
2122

2223

23-
def check_internal_ip(request) -> Optional[HttpResponse]:
24+
def check_internal_ip(request) -> HttpResponse | None:
2425
"""
2526
A method to determine if a request is allowed to load a signage page.
2627

intranet/apps/templatetags/paginate.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import List, Union
1+
from __future__ import annotations
22

33
from django import template
44

@@ -13,8 +13,8 @@ def query_transform(request, **kwargs):
1313
return query.urlencode()
1414

1515

16-
@register.filter # TODO: replace return type with list[int | None]
17-
def page_list(paginator, current_page) -> List[Union[int, None]]:
16+
@register.filter
17+
def page_list(paginator, current_page) -> list[int | None]:
1818
"""Pagination
1919
2020
If there is a ``None`` in the output, it should be replaced

0 commit comments

Comments
 (0)