Skip to content

Commit

Permalink
refactor: use pydantic.JsonValue instead of own alias
Browse files Browse the repository at this point in the history
  • Loading branch information
MHajoha committed Jul 8, 2024
1 parent 36a93e8 commit b2577fd
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 24 deletions.
14 changes: 0 additions & 14 deletions questionpy_common/api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
# This file is part of QuestionPy. (https://questionpy.org)
# QuestionPy is free software released under terms of the MIT license. See LICENSE.md.
# (c) Technische Universität Berlin, innoCampus <[email protected]>
from collections.abc import (
Mapping,
Sequence, # noqa: F401
)
from typing import (
TypeAlias,
Union, # noqa: F401,
)

from pydantic import BaseModel
from typing_extensions import TypeAliasType


class Localized(BaseModel):
lang: str


# "Regular" recursive type aliases break Pydantic: https://github.com/pydantic/pydantic/issues/8346
PlainValue: TypeAlias = TypeAliasType("PlainValue", "Union[None, int, str, bool, Sequence[PlainValue], PlainMapping]")
PlainMapping: TypeAlias = TypeAliasType("PlainMapping", Mapping[str, PlainValue])
7 changes: 4 additions & 3 deletions questionpy_common/api/qtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@
from questionpy_common.api.package import BasePackageInterface

if TYPE_CHECKING:
from pydantic import JsonValue

from questionpy_common.elements import OptionsFormDefinition

from . import PlainMapping
from .question import QuestionInterface

__all__ = ["InvalidQuestionStateError", "OptionsFormValidationError", "QuestionTypeInterface"]


class QuestionTypeInterface(BasePackageInterface, Protocol):
@abstractmethod
def get_options_form(self, question_state: str | None) -> tuple[OptionsFormDefinition, PlainMapping]:
def get_options_form(self, question_state: str | None) -> tuple[OptionsFormDefinition, dict[str, JsonValue]]:
"""Get the form used to create a new or edit an existing question.
Args:
Expand All @@ -30,7 +31,7 @@ def get_options_form(self, question_state: str | None) -> tuple[OptionsFormDefin
"""

@abstractmethod
def create_question_from_options(self, old_state: str | None, form_data: PlainMapping) -> QuestionInterface:
def create_question_from_options(self, old_state: str | None, form_data: dict[str, JsonValue]) -> QuestionInterface:
"""Create or update the question (state) with the form data from a submitted question edit form.
Args:
Expand Down
8 changes: 4 additions & 4 deletions questionpy_common/api/question.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
from enum import Enum
from typing import Annotated, Protocol

from pydantic import BaseModel, Field
from pydantic import BaseModel, Field, JsonValue

from . import Localized, PlainMapping
from . import Localized
from .attempt import AttemptModel, AttemptScoredModel, AttemptStartedModel

__all__ = ["PossibleResponse", "QuestionInterface", "QuestionModel", "ScoringMethod", "SubquestionModel"]
Expand Down Expand Up @@ -58,7 +58,7 @@ def start_attempt(self, variant: int) -> AttemptStartedModel:

@abstractmethod
def get_attempt(
self, attempt_state: str, scoring_state: str | None = None, response: PlainMapping | None = None
self, attempt_state: str, scoring_state: str | None = None, response: dict[str, JsonValue] | None = None
) -> AttemptModel:
"""Create an attempt object for a previously started attempt.
Expand All @@ -77,7 +77,7 @@ def score_attempt(
self,
attempt_state: str,
scoring_state: str | None = None,
response: PlainMapping | None = None,
response: dict[str, JsonValue] | None = None,
*,
try_scoring_with_countback: bool = False,
try_giving_hint: bool = False,
Expand Down
5 changes: 2 additions & 3 deletions questionpy_server/worker/runtime/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
from struct import Struct
from typing import Any, ClassVar

from pydantic import BaseModel
from pydantic import BaseModel, JsonValue

from questionpy_common.api import PlainMapping
from questionpy_common.api.attempt import AttemptModel, AttemptScoredModel, AttemptStartedModel
from questionpy_common.api.qtype import InvalidQuestionStateError
from questionpy_common.api.question import QuestionModel
Expand Down Expand Up @@ -155,7 +154,7 @@ class CreateQuestionFromOptions(MessageToWorker):
request_user: RequestUser
question_state: str | None
"""Old question state or ``None`` if the question is new."""
form_data: PlainMapping
form_data: dict[str, JsonValue]

class Response(MessageToServer):
message_id: ClassVar[MessageIds] = MessageIds.RETURN_CREATE_QUESTION
Expand Down
Binary file modified tests/test_data/package/package_1.qpy
Binary file not shown.
Binary file modified tests/test_data/package/package_2.qpy
Binary file not shown.

0 comments on commit b2577fd

Please sign in to comment.