Skip to content

Commit 8996b91

Browse files
refactor: add type hints to tooling
1 parent ee23e04 commit 8996b91

File tree

5 files changed

+53
-38
lines changed

5 files changed

+53
-38
lines changed

openedx_filters/course_authoring/filters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class LMSPageURLRequested(OpenEdxPublicFilter):
2222
- Function or Method: get_asset_json
2323
"""
2424

25-
filter_type = "org.openedx.course_authoring.lms.page.url.requested.v1"
25+
filter_type: str = "org.openedx.course_authoring.lms.page.url.requested.v1"
2626

2727
@classmethod
2828
def run_filter(cls, url: str, org: str) -> tuple[str, str]:

openedx_filters/exceptions.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"""
44

55

6+
from typing import Optional
7+
8+
69
class OpenEdxFilterException(Exception):
710
"""
811
Base exception for filters.
@@ -18,7 +21,13 @@ class OpenEdxFilterException(Exception):
1821
exception.
1922
"""
2023

21-
def __init__(self, message="", redirect_to=None, status_code=None, **kwargs):
24+
def __init__(
25+
self,
26+
message: Optional[str] = "",
27+
redirect_to: Optional[str] = "",
28+
status_code: Optional[int] = None,
29+
**kwargs
30+
) -> None:
2231
"""
2332
Init method for OpenEdxFilterException.
2433
@@ -31,7 +40,7 @@ def __init__(self, message="", redirect_to=None, status_code=None, **kwargs):
3140
for key, value in kwargs.items():
3241
setattr(self, key, value)
3342

34-
def __str__(self):
43+
def __str__(self) -> str | None:
3544
"""
3645
Show string representation of OpenEdxFilterException using its message.
3746
"""

openedx_filters/learning/filters.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class AccountSettingsRenderStarted(OpenEdxPublicFilter):
3333
This filter doesn't work alongside the account MFE, only with the legacy account settings page.
3434
"""
3535

36-
filter_type = "org.openedx.learning.student.settings.render.started.v1"
36+
filter_type: str = "org.openedx.learning.student.settings.render.started.v1"
3737

3838
class RedirectToPage(OpenEdxFilterException):
3939
"""
@@ -107,7 +107,7 @@ def run_filter(cls, context: dict, template_name: str) -> tuple[dict, str]:
107107
- dict: context dictionary for the account settings page, possibly modified.
108108
- str: template name to be rendered by the account settings page, possibly modified.
109109
"""
110-
data = super().run_pipeline(context=context, template_name=template_name)
110+
data: dict = super().run_pipeline(context=context, template_name=template_name)
111111
return data.get("context"), data.get("template_name")
112112

113113

@@ -128,8 +128,8 @@ class StudentRegistrationRequested(OpenEdxPublicFilter, SensitiveDataManagementM
128128
- Function or Method: RegistrationView.post
129129
"""
130130

131-
filter_type = "org.openedx.learning.student.registration.requested.v1"
132-
sensitive_form_data = [
131+
filter_type: str = "org.openedx.learning.student.registration.requested.v1"
132+
sensitive_form_data: list[str] = [
133133
"password",
134134
"newpassword",
135135
"new_password",
@@ -181,7 +181,7 @@ class StudentLoginRequested(OpenEdxPublicFilter):
181181
- Function or Method: login_user
182182
"""
183183

184-
filter_type = "org.openedx.learning.student.login.requested.v1"
184+
filter_type: str = "org.openedx.learning.student.login.requested.v1"
185185

186186
class PreventLogin(OpenEdxFilterException):
187187
"""
@@ -238,7 +238,7 @@ class CourseEnrollmentStarted(OpenEdxPublicFilter):
238238
- Function or Method: enroll
239239
"""
240240

241-
filter_type = "org.openedx.learning.course.enrollment.started.v1"
241+
filter_type: str = "org.openedx.learning.course.enrollment.started.v1"
242242

243243
class PreventEnrollment(OpenEdxFilterException):
244244
"""
@@ -286,7 +286,7 @@ class CourseUnenrollmentStarted(OpenEdxPublicFilter):
286286
- Function or Method: unenroll
287287
"""
288288

289-
filter_type = "org.openedx.learning.course.unenrollment.started.v1"
289+
filter_type: str = "org.openedx.learning.course.unenrollment.started.v1"
290290

291291
class PreventUnenrollment(OpenEdxFilterException):
292292
"""
@@ -333,7 +333,7 @@ class CertificateCreationRequested(OpenEdxPublicFilter):
333333
- Function or Method: _generate_certificate_task
334334
"""
335335

336-
filter_type = "org.openedx.learning.certificate.creation.requested.v1"
336+
filter_type:str = "org.openedx.learning.certificate.creation.requested.v1"
337337

338338
class PreventCertificateCreation(OpenEdxFilterException):
339339
"""
@@ -407,7 +407,7 @@ class CertificateRenderStarted(OpenEdxPublicFilter):
407407
- Function or Method: render_html_view
408408
"""
409409

410-
filter_type = "org.openedx.learning.certificate.render.started.v1"
410+
filter_type: str = "org.openedx.learning.certificate.render.started.v1"
411411

412412
class RedirectToPage(OpenEdxFilterException):
413413
"""
@@ -494,7 +494,7 @@ class CohortChangeRequested(OpenEdxPublicFilter):
494494
- Function or Method: assign
495495
"""
496496

497-
filter_type = "org.openedx.learning.cohort.change.requested.v1"
497+
filter_type: str = "org.openedx.learning.cohort.change.requested.v1"
498498

499499
class PreventCohortChange(OpenEdxFilterException):
500500
"""
@@ -538,7 +538,7 @@ class CohortAssignmentRequested(OpenEdxPublicFilter):
538538
- Function or Method: assign
539539
"""
540540

541-
filter_type = "org.openedx.learning.cohort.assignment.requested.v1"
541+
filter_type: str = "org.openedx.learning.cohort.assignment.requested.v1"
542542

543543
class PreventCohortAssignment(OpenEdxFilterException):
544544
"""
@@ -582,7 +582,7 @@ class CourseAboutRenderStarted(OpenEdxPublicFilter):
582582
- Function or Method: course_about
583583
"""
584584

585-
filter_type = "org.openedx.learning.course_about.render.started.v1"
585+
filter_type: str = "org.openedx.learning.course_about.render.started.v1"
586586

587587
class RedirectToPage(OpenEdxFilterException):
588588
"""
@@ -683,7 +683,7 @@ class DashboardRenderStarted(OpenEdxPublicFilter):
683683
This filter doesn't work alongside the dashboard MFE, only with the legacy student dashboard.
684684
"""
685685

686-
filter_type = "org.openedx.learning.dashboard.render.started.v1"
686+
filter_type: str = "org.openedx.learning.dashboard.render.started.v1"
687687

688688
class RedirectToPage(OpenEdxFilterException):
689689
"""
@@ -779,7 +779,7 @@ class VerticalBlockChildRenderStarted(OpenEdxPublicFilter):
779779
- Function or Method: VerticalBlock._student_or_public_view
780780
"""
781781

782-
filter_type = "org.openedx.learning.vertical_block_child.render.started.v1"
782+
filter_type: str = "org.openedx.learning.vertical_block_child.render.started.v1"
783783

784784
class PreventChildBlockRender(OpenEdxFilterException):
785785
"""
@@ -825,7 +825,7 @@ class CourseEnrollmentQuerysetRequested(OpenEdxPublicFilter):
825825
marked to be removed if it's not used. See openedx-filters#245 for more information.
826826
"""
827827

828-
filter_type = "org.openedx.learning.course_enrollment_queryset.requested.v1"
828+
filter_type: str = "org.openedx.learning.course_enrollment_queryset.requested.v1"
829829

830830
class PreventEnrollmentQuerysetRequest(OpenEdxFilterException):
831831
"""
@@ -864,7 +864,7 @@ class RenderXBlockStarted(OpenEdxPublicFilter):
864864
- Function or Method: render_xblock
865865
"""
866866

867-
filter_type = "org.openedx.learning.xblock.render.started.v1"
867+
filter_type: str = "org.openedx.learning.xblock.render.started.v1"
868868

869869
class PreventXBlockBlockRender(OpenEdxFilterException):
870870
"""
@@ -925,7 +925,7 @@ class VerticalBlockRenderCompleted(OpenEdxPublicFilter):
925925
- Function or Method: VerticalBlock._student_or_public_view
926926
"""
927927

928-
filter_type = "org.openedx.learning.vertical_block.render.completed.v1"
928+
filter_type: str = "org.openedx.learning.vertical_block.render.completed.v1"
929929

930930
class PreventVerticalBlockRender(OpenEdxFilterException):
931931
"""
@@ -973,7 +973,7 @@ class CourseHomeUrlCreationStarted(OpenEdxPublicFilter):
973973
- Function or Method: course_home_url
974974
"""
975975

976-
filter_type = "org.openedx.learning.course.homepage.url.creation.started.v1"
976+
filter_type: str = "org.openedx.learning.course.homepage.url.creation.started.v1"
977977

978978
@classmethod
979979
def run_filter(cls, course_key: CourseKey, course_home_url: str) -> tuple[CourseKey, str]:
@@ -1010,7 +1010,7 @@ class CourseEnrollmentAPIRenderStarted(OpenEdxPublicFilter):
10101010
- Function or Method: EnrollmentSerializer.to_representation
10111011
"""
10121012

1013-
filter_type = "org.openedx.learning.home.enrollment.api.rendered.v1"
1013+
filter_type: str = "org.openedx.learning.home.enrollment.api.rendered.v1"
10141014

10151015
@classmethod
10161016
def run_filter(cls, course_key: CourseKey, serialized_enrollment: dict) -> tuple[CourseKey, dict]:
@@ -1047,7 +1047,7 @@ class CourseRunAPIRenderStarted(OpenEdxPublicFilter):
10471047
- Function or Method: CourseRunSerializer.to_representation
10481048
"""
10491049

1050-
filter_type = "org.openedx.learning.home.courserun.api.rendered.started.v1"
1050+
filter_type: str = "org.openedx.learning.home.courserun.api.rendered.started.v1"
10511051

10521052
@classmethod
10531053
def run_filter(cls, serialized_courserun: dict) -> dict:
@@ -1081,7 +1081,7 @@ class InstructorDashboardRenderStarted(OpenEdxPublicFilter):
10811081
- Function or Method: instructor_dashboard_2
10821082
"""
10831083

1084-
filter_type = "org.openedx.learning.instructor.dashboard.render.started.v1"
1084+
filter_type: str = "org.openedx.learning.instructor.dashboard.render.started.v1"
10851085

10861086
class RedirectToPage(OpenEdxFilterException):
10871087
"""
@@ -1179,7 +1179,7 @@ class ORASubmissionViewRenderStarted(OpenEdxPublicFilter):
11791179
- Function or Method: render_submission
11801180
"""
11811181

1182-
filter_type = "org.openedx.learning.ora.submission_view.render.started.v1"
1182+
filter_type: str = "org.openedx.learning.ora.submission_view.render.started.v1"
11831183

11841184
class RenderInvalidTemplate(OpenEdxFilterException):
11851185
"""
@@ -1237,7 +1237,7 @@ class IDVPageURLRequested(OpenEdxPublicFilter):
12371237
- Function or Method: XBlockVerificationService.get_verify_location
12381238
"""
12391239

1240-
filter_type = "org.openedx.learning.idv.page.url.requested.v1"
1240+
filter_type: str = "org.openedx.learning.idv.page.url.requested.v1"
12411241

12421242
@classmethod
12431243
def run_filter(cls, url: str) -> str:
@@ -1271,7 +1271,7 @@ class CourseAboutPageURLRequested(OpenEdxPublicFilter):
12711271
- Function or Method: get_link_for_about_page
12721272
"""
12731273

1274-
filter_type = "org.openedx.learning.course_about.page.url.requested.v1"
1274+
filter_type: str = "org.openedx.learning.course_about.page.url.requested.v1"
12751275

12761276
@classmethod
12771277
def run_filter(cls, url: str, org: str) -> tuple[str, str]:
@@ -1309,7 +1309,7 @@ class ScheduleQuerySetRequested(OpenEdxPublicFilter):
13091309
- Function or Method: BinnedSchedulesBaseResolver.get_schedules_with_target_date_by_bin_and_orgs
13101310
"""
13111311

1312-
filter_type = "org.openedx.learning.schedule.queryset.requested.v1"
1312+
filter_type: str = "org.openedx.learning.schedule.queryset.requested.v1"
13131313

13141314
@classmethod
13151315
def run_filter(cls, schedules: QuerySet) -> QuerySet:

openedx_filters/tooling.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Tooling necessary to use Open edX Filters.
33
"""
44
from logging import getLogger
5+
from typing import Any, Optional
56

67
from django.conf import settings
78
from django.utils.module_loading import import_string
@@ -18,14 +19,14 @@ class OpenEdxPublicFilter:
1819

1920
filter_type = ""
2021

21-
def __repr__(self):
22+
def __repr__(self) -> str:
2223
"""
2324
Represent OpenEdxPublicFilter as a string.
2425
"""
2526
return "<OpenEdxPublicFilter: {filter_type}>".format(filter_type=self.filter_type)
2627

2728
@classmethod
28-
def get_steps_for_pipeline(cls, pipeline, fail_silently):
29+
def get_steps_for_pipeline(cls, pipeline: list, fail_silently: Optional[bool] = True) -> list:
2930
"""
3031
Get pipeline objects from paths.
3132
@@ -68,7 +69,7 @@ def get_steps_for_pipeline(cls, pipeline, fail_silently):
6869
return step_list
6970

7071
@classmethod
71-
def get_pipeline_configuration(cls):
72+
def get_pipeline_configuration(cls) -> tuple[list, bool, dict]:
7273
"""
7374
Get pipeline configuration from filter settings.
7475
@@ -95,9 +96,11 @@ def get_pipeline_configuration(cls):
9596
False the opposite.
9697
extra_config: anything else defined in the dictionary.
9798
"""
98-
filter_config = cls.get_filter_config()
99+
filter_config: dict = cls.get_filter_config()
99100

100-
pipeline, fail_silently, extra_config = [], True, {}
101+
pipeline: list = []
102+
fail_silently: bool = True
103+
extra_config: dict = {}
101104

102105
if not filter_config:
103106
return pipeline, fail_silently, extra_config
@@ -119,7 +122,7 @@ def get_pipeline_configuration(cls):
119122
return pipeline, fail_silently, extra_config
120123

121124
@classmethod
122-
def get_filter_config(cls):
125+
def get_filter_config(cls) -> dict:
123126
"""
124127
Get filters configuration from settings.
125128
@@ -161,7 +164,7 @@ def get_filter_config(cls):
161164
return filters_config.get(cls.filter_type, {})
162165

163166
@classmethod
164-
def run_pipeline(cls, **kwargs):
167+
def run_pipeline(cls, **kwargs: Any) -> dict:
165168
"""
166169
Execute filters in order.
167170

openedx_filters/utils.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"""
44

55

6+
from django.http import QueryDict
7+
8+
69
class SensitiveDataManagementMixin:
710
"""
811
Custom class used manage sensitive data within filter arguments.
@@ -11,7 +14,7 @@ class SensitiveDataManagementMixin:
1114
sensitive_form_data: list[str] = []
1215

1316
@classmethod
14-
def extract_sensitive_data(cls, form_data):
17+
def extract_sensitive_data(cls, form_data: QueryDict) -> dict:
1518
"""
1619
Extract sensitive data from its child class input arguments.
1720
@@ -25,8 +28,8 @@ def extract_sensitive_data(cls, form_data):
2528
>> form_data
2629
{"username": "example"}
2730
"""
28-
sensitive_data = {}
29-
base_form_data = form_data.copy()
31+
sensitive_data: dict = {}
32+
base_form_data: QueryDict = form_data.copy()
3033
for key, value in base_form_data.items():
3134
if key in cls.sensitive_form_data:
3235
form_data.pop(key)

0 commit comments

Comments
 (0)