Skip to content

Commit

Permalink
refactor: address quality issues with mypy and filter signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
mariajgrimaldi committed Feb 4, 2025
1 parent 93e0105 commit b685200
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 37 deletions.
2 changes: 1 addition & 1 deletion openedx_filters/course_authoring/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class LMSPageURLRequested(OpenEdxPublicFilter):
filter_type = "org.openedx.course_authoring.lms.page.url.requested.v1"

@classmethod
def run_filter(cls, url: str, org: str) -> tuple[str, str]:
def run_filter(cls, url: str, org: str) -> tuple[str | None, str | None]:
"""
Process the inputs using the configured pipeline steps to modify the URL of the page requested by the user.
Expand Down
2 changes: 1 addition & 1 deletion openedx_filters/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def __init__(
for key, value in kwargs.items():
setattr(self, key, value)

def __str__(self) -> str | None:
def __str__(self) -> str:
"""
Show string representation of OpenEdxFilterException using its message.
"""
Expand Down
56 changes: 35 additions & 21 deletions openedx_filters/learning/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Package where filters related to the learning architectural subdomain are implemented.
"""

from typing import Any, Optional
from typing import Any, Optional, Union

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

@classmethod
def run_filter(cls, context: dict, template_name: str) -> tuple[dict, str]:
def run_filter(cls, context: dict[str, Any], template_name: str) -> tuple[dict[str, Any] | None, str | None]:
"""
Process the input context and template_name using the configured pipeline steps to modify the account settings.
Expand Down Expand Up @@ -159,7 +159,7 @@ def run_filter(cls, form_data: QueryDict) -> QueryDict:
"""
sensitive_data = cls.extract_sensitive_data(form_data)
data = super().run_pipeline(form_data=form_data)
form_data = data.get("form_data")
form_data = data.get("form_data", QueryDict())
form_data.update(sensitive_data)
return form_data

Expand Down Expand Up @@ -249,7 +249,7 @@ class PreventEnrollment(OpenEdxFilterException):
"""

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

@classmethod
def run_filter( # pylint: disable=too-many-positional-arguments
cls: type,
cls,
user: Any,
course_key: CourseKey,
mode: str,
status: str,
grade: float,
generation_mode: str,
) -> tuple[Any, CourseKey, str, str, float, str]:
) -> tuple[Any, CourseKey | None, str | None, str | None, float | None, str | None]:
"""
Process the inputs using the configured pipeline steps to modify the certificate creation process.
Expand Down Expand Up @@ -460,7 +460,7 @@ def __init__(self, message: str, response: HttpResponse) -> None:
)

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

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

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

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

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

@classmethod
def run_filter(cls, context: dict, student_view_context: dict):
def run_filter(
cls,
context: dict[str, Any],
student_view_context: dict
) -> tuple[dict[str, Any] | None, dict[str, Any] | None]:
"""
Process the inputs using the configured pipeline steps to modify the rendering of an XBlock.
Expand Down Expand Up @@ -936,7 +940,13 @@ class PreventVerticalBlockRender(OpenEdxFilterException):
"""

@classmethod
def run_filter(cls, block: Any, fragment: Any, context: dict, view: str) -> tuple[Any, Any, dict, str]:
def run_filter(
cls,
block: Any,
fragment: Any,
context: dict[str, Any],
view: str
) -> tuple[Any, Any, dict[str, Any] | None, str | None]:
"""
Process the inputs using the configured pipeline steps to modify the rendering of a vertical block.
Expand Down Expand Up @@ -976,7 +986,7 @@ class CourseHomeUrlCreationStarted(OpenEdxPublicFilter):
filter_type = "org.openedx.learning.course.homepage.url.creation.started.v1"

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

@classmethod
def run_filter(cls, course_key: CourseKey, serialized_enrollment: dict) -> tuple[CourseKey, dict]:
def run_filter(
cls,
course_key: CourseKey,
serialized_enrollment: dict[str, Any]
) -> tuple[CourseKey | None, dict[str, Any] | None]:
"""
Process the inputs using the configured pipeline steps to modify the course enrollment data.
Expand Down Expand Up @@ -1050,7 +1064,7 @@ class CourseRunAPIRenderStarted(OpenEdxPublicFilter):
filter_type = "org.openedx.learning.home.courserun.api.rendered.started.v1"

@classmethod
def run_filter(cls, serialized_courserun: dict) -> dict:
def run_filter(cls, serialized_courserun: dict[str, Any]) -> dict[str, Any] | None:
"""
Process the serialized_courserun using the configured pipeline steps to modify the course run data.
Expand Down Expand Up @@ -1145,7 +1159,7 @@ def __init__(self, message: str, response: Optional[HttpResponse] = None):
)

@classmethod
def run_filter(cls, context: dict, template_name: str) -> tuple[dict, str]:
def run_filter(cls, context: dict[str, Any], template_name: str) -> tuple[dict[str, Any] | None, str | None]:
"""
Process the context and template_name using the configured pipeline steps to modify the instructor dashboard.
Expand Down Expand Up @@ -1203,7 +1217,7 @@ def __init__(
super().__init__(message, context=context, template_name=template_name)

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

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

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

@classmethod
def run_filter(cls, schedules: QuerySet) -> QuerySet:
def run_filter(cls, schedules: QuerySet) -> QuerySet | None:
"""
Process the schedules QuerySet using the configured pipeline steps to modify the schedules data.
Expand Down
20 changes: 10 additions & 10 deletions openedx_filters/tooling.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Tooling necessary to use Open edX Filters.
"""
from logging import getLogger
from typing import Any, Union
from typing import Any

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

@classmethod
def get_steps_for_pipeline(cls, pipeline: list, fail_silently: bool = True) -> list:
def get_steps_for_pipeline(cls, pipeline: list, fail_silently: bool = True) -> list[type]:
"""
Get pipeline objects from paths.
Expand Down Expand Up @@ -69,7 +69,7 @@ def get_steps_for_pipeline(cls, pipeline: list, fail_silently: bool = True) -> l
return step_list

@classmethod
def get_pipeline_configuration(cls) -> tuple[list, bool, dict]:
def get_pipeline_configuration(cls) -> tuple[list[str], bool, dict[str, Any]]:
"""
Get pipeline configuration from filter settings.
Expand Down Expand Up @@ -98,7 +98,9 @@ def get_pipeline_configuration(cls) -> tuple[list, bool, dict]:
"""
filter_config = cls.get_filter_config()

pipeline, fail_silently, extra_config = [], True, {}
pipeline: list = []
fail_silently: bool = True
extra_config: dict = {}

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

@classmethod
def get_filter_config(cls) -> dict:
def get_filter_config(cls) -> dict[str, Any]:
"""
Get filters configuration from settings.
Expand Down Expand Up @@ -162,7 +164,7 @@ def get_filter_config(cls) -> dict:
return filters_config.get(cls.filter_type, {})

@classmethod
def run_pipeline(cls, **kwargs: Any) -> dict:
def run_pipeline(cls, **kwargs: Any) -> dict[str, Any] | Any:
"""
Execute filters in order.
Expand Down Expand Up @@ -199,14 +201,12 @@ def run_pipeline(cls, **kwargs: Any) -> dict:
information check their Github repository:
https://github.com/python-social-auth/social-core
"""
pipeline: list[str] = []
fail_silently: bool = True
extra_config: dict[str, Any] = {}
pipeline, fail_silently, extra_config = cls.get_pipeline_configuration()

if not pipeline:
return kwargs

steps: list[] = cls.get_steps_for_pipeline(pipeline, fail_silently)
steps = cls.get_steps_for_pipeline(pipeline, fail_silently)
filter_metadata = {
"filter_type": cls.filter_type,
"running_pipeline": pipeline,
Expand Down
9 changes: 5 additions & 4 deletions openedx_filters/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""


from typing import Union
from django.http import QueryDict


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

sensitive_form_data = []
sensitive_form_data: list[str] = []

@classmethod
def extract_sensitive_data(cls, form_data: QueryDict) -> dict:
def extract_sensitive_data(cls, form_data: QueryDict) -> dict[str, str]:
"""
Extract sensitive data from its child class input arguments.
Expand All @@ -29,10 +30,10 @@ def extract_sensitive_data(cls, form_data: QueryDict) -> dict:
{"username": "example"}
"""
sensitive_data = {}
base_form_data = form_data.copy()
base_form_data = form_data.copy()
for key, value in base_form_data.items():
if key in cls.sensitive_form_data:
form_data.pop(key)
sensitive_data[key] = value
sensitive_data[key] = str(value)

return sensitive_data

0 comments on commit b685200

Please sign in to comment.