From 91d1cb03fa26561548dd9c97e48b691515360f3f Mon Sep 17 00:00:00 2001 From: Marcos Prieto Date: Thu, 9 May 2024 10:58:10 +0200 Subject: [PATCH] Fix all mypy's attr-defined issues in the codebase --- lms/scripts/init_db.py | 1 + lms/services/moodle.py | 30 ++++++++++++++++++++++++++---- pyproject.toml | 1 - 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/lms/scripts/init_db.py b/lms/scripts/init_db.py index 158393f933..4394eec63c 100755 --- a/lms/scripts/init_db.py +++ b/lms/scripts/init_db.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +# mypy: disable-error-code="attr-defined" """ Initialize the DB. diff --git a/lms/services/moodle.py b/lms/services/moodle.py index ebf898e82e..fcae7d378e 100644 --- a/lms/services/moodle.py +++ b/lms/services/moodle.py @@ -1,4 +1,5 @@ from enum import Enum +from typing import Literal, NotRequired, TypedDict from lms.services.aes import AESService from lms.services.exceptions import ExternalRequestError @@ -22,6 +23,23 @@ class Function(str, Enum): """Returns a list of pages in a provided list of courses.""" +class File(TypedDict): + """Represents a file or folder in an LMS's file storage.""" + + type: Literal["File", "Folder"] + mime_type: NotRequired[Literal["text/html", "application/pdf", "video"]] + + id: str + """ID in our system""" + lms_id: str + """Raw ID in the LMS""" + + display_name: str + + updated_at: NotRequired[str] + children: NotRequired[list["File"]] + + class MoodleAPIClient: API_PATH = "webservice/rest/server.php" @@ -141,9 +159,13 @@ def page(self, course_id, page_id) -> dict | None: } def list_pages(self, course_id: int): + root: File = { # type:ignore + "type": "Folder", + "display_name": "", + "children": [], + } contents = self.course_contents(course_id) - root = {"type": "Folder", "display_name": "", "children": []} - folders = {root["display_name"]: root} + folders: dict[str, File] = {root["display_name"]: root} for topic in contents: topic_name = topic["name"] @@ -153,7 +175,7 @@ def list_pages(self, course_id: int): # Pages can only be at the top level modules if module["modname"] == "page": if topic_name not in folders: - new_folder = { + new_folder: File = { "type": "Folder", "display_name": topic_name, "id": f"{course_id}-{topic_name}", @@ -172,7 +194,7 @@ def list_pages(self, course_id: int): ) updated_at = page_index[0]["updated_at"] if page_index else None - file_node = { + file_node: File = { "type": "File", "mime_type": "text/html", "display_name": module["name"], diff --git a/pyproject.toml b/pyproject.toml index fce707c9ff..511e06202d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -146,7 +146,6 @@ project_type = "application" python_version = 3.11 disable_error_code = [ - "attr-defined", "import-untyped", ]