Skip to content

Commit b685200

Browse files
refactor: address quality issues with mypy and filter signatures
1 parent 93e0105 commit b685200

File tree

5 files changed

+52
-37
lines changed

5 files changed

+52
-37
lines changed

openedx_filters/course_authoring/filters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class LMSPageURLRequested(OpenEdxPublicFilter):
2525
filter_type = "org.openedx.course_authoring.lms.page.url.requested.v1"
2626

2727
@classmethod
28-
def run_filter(cls, url: str, org: str) -> tuple[str, str]:
28+
def run_filter(cls, url: str, org: str) -> tuple[str | None, str | None]:
2929
"""
3030
Process the inputs using the configured pipeline steps to modify the URL of the page requested by the user.
3131

openedx_filters/exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def __init__(
4040
for key, value in kwargs.items():
4141
setattr(self, key, value)
4242

43-
def __str__(self) -> str | None:
43+
def __str__(self) -> str:
4444
"""
4545
Show string representation of OpenEdxFilterException using its message.
4646
"""

openedx_filters/learning/filters.py

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Package where filters related to the learning architectural subdomain are implemented.
33
"""
44

5-
from typing import Any, Optional
5+
from typing import Any, Optional, Union
66

77
from django.db.models.query import QuerySet
88
from django.http import HttpResponse, QueryDict
@@ -94,7 +94,7 @@ def __init__(self, message: str, response: Optional[HttpResponse] = None) -> Non
9494
super().__init__(message, response=response)
9595

9696
@classmethod
97-
def run_filter(cls, context: dict, template_name: str) -> tuple[dict, str]:
97+
def run_filter(cls, context: dict[str, Any], template_name: str) -> tuple[dict[str, Any] | None, str | None]:
9898
"""
9999
Process the input context and template_name using the configured pipeline steps to modify the account settings.
100100
@@ -159,7 +159,7 @@ def run_filter(cls, form_data: QueryDict) -> QueryDict:
159159
"""
160160
sensitive_data = cls.extract_sensitive_data(form_data)
161161
data = super().run_pipeline(form_data=form_data)
162-
form_data = data.get("form_data")
162+
form_data = data.get("form_data", QueryDict())
163163
form_data.update(sensitive_data)
164164
return form_data
165165

@@ -249,7 +249,7 @@ class PreventEnrollment(OpenEdxFilterException):
249249
"""
250250

251251
@classmethod
252-
def run_filter(cls, user: Any, course_key: CourseKey, mode: str) -> tuple[Any, CourseKey, str]:
252+
def run_filter(cls, user: Any, course_key: CourseKey, mode: str) -> tuple[Any, CourseKey | None, str | None]:
253253
"""
254254
Process the user, course_key, and mode using the configured pipeline steps to modify the enrollment process.
255255
@@ -345,14 +345,14 @@ class PreventCertificateCreation(OpenEdxFilterException):
345345

346346
@classmethod
347347
def run_filter( # pylint: disable=too-many-positional-arguments
348-
cls: type,
348+
cls,
349349
user: Any,
350350
course_key: CourseKey,
351351
mode: str,
352352
status: str,
353353
grade: float,
354354
generation_mode: str,
355-
) -> tuple[Any, CourseKey, str, str, float, str]:
355+
) -> tuple[Any, CourseKey | None, str | None, str | None, float | None, str | None]:
356356
"""
357357
Process the inputs using the configured pipeline steps to modify the certificate creation process.
358358
@@ -460,7 +460,7 @@ def __init__(self, message: str, response: HttpResponse) -> None:
460460
)
461461

462462
@classmethod
463-
def run_filter(cls, context: dict, custom_template: Any) -> tuple[dict, Any]:
463+
def run_filter(cls, context: dict, custom_template: Any) -> tuple[dict[str, Any] | None, Any]:
464464
"""
465465
Process the context and custom_template using the configured pipeline steps to modify the certificate rendering.
466466
@@ -646,7 +646,7 @@ def __init__(self, message: str, response: HttpResponse) -> None:
646646
)
647647

648648
@classmethod
649-
def run_filter(cls, context: dict, template_name: str) -> tuple[dict, str]:
649+
def run_filter(cls, context: dict[str, Any], template_name: str) -> tuple[dict[str, Any] | None, str | None]:
650650
"""
651651
Process the context and template_name using the configured pipeline steps to modify the course about rendering.
652652
@@ -745,7 +745,7 @@ def __init__(self, message: str, response: Optional[HttpResponse] = None) -> Non
745745
)
746746

747747
@classmethod
748-
def run_filter(cls, context: dict, template_name: str) -> tuple[dict, str]:
748+
def run_filter(cls, context: dict[str, Any], template_name: str) -> tuple[dict[str, Any] | None, str | None]:
749749
"""
750750
Process the context and template_name using the configured pipeline steps to modify the dashboard rendering.
751751
@@ -790,7 +790,7 @@ class PreventChildBlockRender(OpenEdxFilterException):
790790
"""
791791

792792
@classmethod
793-
def run_filter(cls, block: Any, context: dict) -> tuple[Any, dict]:
793+
def run_filter(cls, block: Any, context: dict[str, Any]) -> tuple[Any, dict[str, Any] | None]:
794794
"""
795795
Process the block and context using the configured pipeline steps to modify the rendering of a child block.
796796
@@ -833,7 +833,7 @@ class PreventEnrollmentQuerysetRequest(OpenEdxFilterException):
833833
"""
834834

835835
@classmethod
836-
def run_filter(cls, enrollments: QuerySet) -> QuerySet:
836+
def run_filter(cls, enrollments: QuerySet) -> QuerySet | None:
837837
"""
838838
Process the enrollments QuerySet using the configured pipeline steps to modify the course enrollment data.
839839
@@ -891,7 +891,11 @@ def __init__(self, message: str, response: Optional[HttpResponse] = None):
891891
super().__init__(message, response=response)
892892

893893
@classmethod
894-
def run_filter(cls, context: dict, student_view_context: dict):
894+
def run_filter(
895+
cls,
896+
context: dict[str, Any],
897+
student_view_context: dict
898+
) -> tuple[dict[str, Any] | None, dict[str, Any] | None]:
895899
"""
896900
Process the inputs using the configured pipeline steps to modify the rendering of an XBlock.
897901
@@ -936,7 +940,13 @@ class PreventVerticalBlockRender(OpenEdxFilterException):
936940
"""
937941

938942
@classmethod
939-
def run_filter(cls, block: Any, fragment: Any, context: dict, view: str) -> tuple[Any, Any, dict, str]:
943+
def run_filter(
944+
cls,
945+
block: Any,
946+
fragment: Any,
947+
context: dict[str, Any],
948+
view: str
949+
) -> tuple[Any, Any, dict[str, Any] | None, str | None]:
940950
"""
941951
Process the inputs using the configured pipeline steps to modify the rendering of a vertical block.
942952
@@ -976,7 +986,7 @@ class CourseHomeUrlCreationStarted(OpenEdxPublicFilter):
976986
filter_type = "org.openedx.learning.course.homepage.url.creation.started.v1"
977987

978988
@classmethod
979-
def run_filter(cls, course_key: CourseKey, course_home_url: str) -> tuple[CourseKey, str]:
989+
def run_filter(cls, course_key: CourseKey, course_home_url: str) -> tuple[CourseKey | None, str | None]:
980990
"""
981991
Process the course_key and course_home_url using the configured pipeline steps to modify the course home url.
982992
@@ -1013,7 +1023,11 @@ class CourseEnrollmentAPIRenderStarted(OpenEdxPublicFilter):
10131023
filter_type = "org.openedx.learning.home.enrollment.api.rendered.v1"
10141024

10151025
@classmethod
1016-
def run_filter(cls, course_key: CourseKey, serialized_enrollment: dict) -> tuple[CourseKey, dict]:
1026+
def run_filter(
1027+
cls,
1028+
course_key: CourseKey,
1029+
serialized_enrollment: dict[str, Any]
1030+
) -> tuple[CourseKey | None, dict[str, Any] | None]:
10171031
"""
10181032
Process the inputs using the configured pipeline steps to modify the course enrollment data.
10191033
@@ -1050,7 +1064,7 @@ class CourseRunAPIRenderStarted(OpenEdxPublicFilter):
10501064
filter_type = "org.openedx.learning.home.courserun.api.rendered.started.v1"
10511065

10521066
@classmethod
1053-
def run_filter(cls, serialized_courserun: dict) -> dict:
1067+
def run_filter(cls, serialized_courserun: dict[str, Any]) -> dict[str, Any] | None:
10541068
"""
10551069
Process the serialized_courserun using the configured pipeline steps to modify the course run data.
10561070
@@ -1145,7 +1159,7 @@ def __init__(self, message: str, response: Optional[HttpResponse] = None):
11451159
)
11461160

11471161
@classmethod
1148-
def run_filter(cls, context: dict, template_name: str) -> tuple[dict, str]:
1162+
def run_filter(cls, context: dict[str, Any], template_name: str) -> tuple[dict[str, Any] | None, str | None]:
11491163
"""
11501164
Process the context and template_name using the configured pipeline steps to modify the instructor dashboard.
11511165
@@ -1203,7 +1217,7 @@ def __init__(
12031217
super().__init__(message, context=context, template_name=template_name)
12041218

12051219
@classmethod
1206-
def run_filter(cls, context: dict, template_name: str) -> tuple[dict, str]:
1220+
def run_filter(cls, context: dict[str, Any], template_name: str) -> tuple[dict[str, Any] | None, str | None]:
12071221
"""
12081222
Process the context and template_name using the configured pipeline steps to modify the submission view.
12091223
@@ -1240,7 +1254,7 @@ class IDVPageURLRequested(OpenEdxPublicFilter):
12401254
filter_type = "org.openedx.learning.idv.page.url.requested.v1"
12411255

12421256
@classmethod
1243-
def run_filter(cls, url: str) -> str:
1257+
def run_filter(cls, url: str) -> str | None:
12441258
"""
12451259
Process the URL using the configured pipeline steps to modify the ID verification page URL.
12461260
@@ -1274,7 +1288,7 @@ class CourseAboutPageURLRequested(OpenEdxPublicFilter):
12741288
filter_type = "org.openedx.learning.course_about.page.url.requested.v1"
12751289

12761290
@classmethod
1277-
def run_filter(cls, url: str, org: str) -> tuple[str, str]:
1291+
def run_filter(cls, url: str, org: str) -> tuple[str | None, str | None]:
12781292
"""
12791293
Process the URL and org using the configured pipeline steps to modify the course about page URL.
12801294
@@ -1312,7 +1326,7 @@ class ScheduleQuerySetRequested(OpenEdxPublicFilter):
13121326
filter_type = "org.openedx.learning.schedule.queryset.requested.v1"
13131327

13141328
@classmethod
1315-
def run_filter(cls, schedules: QuerySet) -> QuerySet:
1329+
def run_filter(cls, schedules: QuerySet) -> QuerySet | None:
13161330
"""
13171331
Process the schedules QuerySet using the configured pipeline steps to modify the schedules data.
13181332

openedx_filters/tooling.py

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

77
from django.conf import settings
88
from django.utils.module_loading import import_string
@@ -26,7 +26,7 @@ def __repr__(self) -> str:
2626
return "<OpenEdxPublicFilter: {filter_type}>".format(filter_type=self.filter_type)
2727

2828
@classmethod
29-
def get_steps_for_pipeline(cls, pipeline: list, fail_silently: bool = True) -> list:
29+
def get_steps_for_pipeline(cls, pipeline: list, fail_silently: bool = True) -> list[type]:
3030
"""
3131
Get pipeline objects from paths.
3232
@@ -69,7 +69,7 @@ def get_steps_for_pipeline(cls, pipeline: list, fail_silently: bool = True) -> l
6969
return step_list
7070

7171
@classmethod
72-
def get_pipeline_configuration(cls) -> tuple[list, bool, dict]:
72+
def get_pipeline_configuration(cls) -> tuple[list[str], bool, dict[str, Any]]:
7373
"""
7474
Get pipeline configuration from filter settings.
7575
@@ -98,7 +98,9 @@ def get_pipeline_configuration(cls) -> tuple[list, bool, dict]:
9898
"""
9999
filter_config = cls.get_filter_config()
100100

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

103105
if not filter_config:
104106
return pipeline, fail_silently, extra_config
@@ -120,7 +122,7 @@ def get_pipeline_configuration(cls) -> tuple[list, bool, dict]:
120122
return pipeline, fail_silently, extra_config
121123

122124
@classmethod
123-
def get_filter_config(cls) -> dict:
125+
def get_filter_config(cls) -> dict[str, Any]:
124126
"""
125127
Get filters configuration from settings.
126128
@@ -162,7 +164,7 @@ def get_filter_config(cls) -> dict:
162164
return filters_config.get(cls.filter_type, {})
163165

164166
@classmethod
165-
def run_pipeline(cls, **kwargs: Any) -> dict:
167+
def run_pipeline(cls, **kwargs: Any) -> dict[str, Any] | Any:
166168
"""
167169
Execute filters in order.
168170
@@ -199,14 +201,12 @@ def run_pipeline(cls, **kwargs: Any) -> dict:
199201
information check their Github repository:
200202
https://github.com/python-social-auth/social-core
201203
"""
202-
pipeline: list[str] = []
203-
fail_silently: bool = True
204-
extra_config: dict[str, Any] = {}
204+
pipeline, fail_silently, extra_config = cls.get_pipeline_configuration()
205205

206206
if not pipeline:
207207
return kwargs
208208

209-
steps: list[] = cls.get_steps_for_pipeline(pipeline, fail_silently)
209+
steps = cls.get_steps_for_pipeline(pipeline, fail_silently)
210210
filter_metadata = {
211211
"filter_type": cls.filter_type,
212212
"running_pipeline": pipeline,

openedx_filters/utils.py

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

55

6+
from typing import Union
67
from django.http import QueryDict
78

89

@@ -11,10 +12,10 @@ class SensitiveDataManagementMixin:
1112
Custom class used manage sensitive data within filter arguments.
1213
"""
1314

14-
sensitive_form_data = []
15+
sensitive_form_data: list[str] = []
1516

1617
@classmethod
17-
def extract_sensitive_data(cls, form_data: QueryDict) -> dict:
18+
def extract_sensitive_data(cls, form_data: QueryDict) -> dict[str, str]:
1819
"""
1920
Extract sensitive data from its child class input arguments.
2021
@@ -29,10 +30,10 @@ def extract_sensitive_data(cls, form_data: QueryDict) -> dict:
2930
{"username": "example"}
3031
"""
3132
sensitive_data = {}
32-
base_form_data = form_data.copy()
33+
base_form_data = form_data.copy()
3334
for key, value in base_form_data.items():
3435
if key in cls.sensitive_form_data:
3536
form_data.pop(key)
36-
sensitive_data[key] = value
37+
sensitive_data[key] = str(value)
3738

3839
return sensitive_data

0 commit comments

Comments
 (0)