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 cb84a7c commit 990b07d
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 146 deletions.
269 changes: 141 additions & 128 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ python = "^3.11"
aiohttp = "^3.9.3"
pydantic = "^2.6.4"
PyYAML = "^6.0.1"
questionpy-server = { git = "https://github.com/questionpy-org/questionpy-server.git", rev = "36a93e84270a60464db9e07b6bcfb5d972329468" }
questionpy-server = { git = "https://github.com/questionpy-org/questionpy-server.git", rev = "b2577fd99e09a37ff3b05dd2a89b60a24963e875" }
jinja2 = "^3.1.3"
aiohttp-jinja2 = "^1.6"
lxml = "~5.1.0"
Expand Down
5 changes: 2 additions & 3 deletions questionpy/_attempt.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
from typing import TYPE_CHECKING, ClassVar

import jinja2
from pydantic import BaseModel
from pydantic import BaseModel, JsonValue

from questionpy_common.api import PlainMapping
from questionpy_common.api.attempt import AttemptFile, AttemptUi, CacheControl, ScoredInputModel, ScoringCode

from ._ui import create_jinja2_environment
Expand Down Expand Up @@ -75,7 +74,7 @@ def __init__(
question: "Question",
attempt_state: BaseAttemptState,
scoring_state: BaseScoringState | None = None,
response: PlainMapping | None = None,
response: dict[str, JsonValue] | None = None,
) -> None:
self.question = question
self.attempt_state = attempt_state
Expand Down
11 changes: 5 additions & 6 deletions questionpy/_qtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
from abc import ABC
from typing import ClassVar, Generic, Self, TypeVar

from pydantic import BaseModel, ValidationError
from pydantic import BaseModel, JsonValue, ValidationError

from questionpy_common.api import PlainMapping
from questionpy_common.api.qtype import OptionsFormValidationError
from questionpy_common.api.question import ScoringMethod, SubquestionModel
from questionpy_common.environment import get_qpy_environment
Expand Down Expand Up @@ -60,7 +59,7 @@ def get_new_question_options_form(cls) -> OptionsFormDefinition:
return cls.options_class.qpy_form

@classmethod
def from_options(cls, old_qswv: QuestionStateWithVersion | None, form_data: PlainMapping) -> Self:
def from_options(cls, old_qswv: QuestionStateWithVersion | None, form_data: dict[str, JsonValue]) -> Self:
try:
parsed_form_data = cls.options_class.model_validate(form_data)
except ValidationError as e:
Expand Down Expand Up @@ -89,10 +88,10 @@ def make_question_state(cls, options: FormModel) -> BaseQuestionState:
return cls.question_state_class()

@classmethod
def validate_options(cls, raw_options: PlainMapping) -> FormModel:
def validate_options(cls, raw_options: dict[str, JsonValue]) -> FormModel:
return cls.options_class.model_validate(raw_options)

def get_options_form(self) -> tuple[OptionsFormDefinition, PlainMapping]:
def get_options_form(self) -> tuple[OptionsFormDefinition, dict[str, JsonValue]]:
return self.options_class.qpy_form, self.options.model_dump()

def start_attempt(self, variant: int) -> Attempt:
Expand All @@ -103,7 +102,7 @@ def get_attempt(
self,
attempt_state: BaseAttemptState,
scoring_state: BaseScoringState | None = None,
response: PlainMapping | None = None,
response: dict[str, JsonValue] | None = None,
*,
compute_score: bool = False,
generate_hint: bool = False,
Expand Down
7 changes: 3 additions & 4 deletions questionpy/_wrappers/_qtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
# (c) Technische Universität Berlin, innoCampus <[email protected]>
from collections.abc import Callable, Mapping

from pydantic import ValidationError
from pydantic import JsonValue, ValidationError

from questionpy import Question
from questionpy._wrappers._question import QuestionWrapper
from questionpy_common.api import PlainMapping
from questionpy_common.api.qtype import InvalidQuestionStateError, QuestionTypeInterface
from questionpy_common.api.question import QuestionInterface
from questionpy_common.elements import OptionsFormDefinition
Expand Down Expand Up @@ -46,14 +45,14 @@ def _get_question_internal(self, qswv: str) -> Question:

return self._question_class.from_state(parsed_qswv)

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]]:
if question_state is not None:
question = self._get_question_internal(question_state)
return question.get_options_form()

return self._question_class.get_new_question_options_form(), {}

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:
parsed_old_state = None
if old_state is not None:
parsed_old_state = self._question_class.question_state_with_version_class.model_validate_json(old_state)
Expand Down
9 changes: 5 additions & 4 deletions questionpy/_wrappers/_question.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# This file is part of the QuestionPy SDK. (https://questionpy.org)
# The QuestionPy SDK is free software released under terms of the MIT license. See LICENSE.md.
# (c) Technische Universität Berlin, innoCampus <[email protected]>
from pydantic import JsonValue

from questionpy import Attempt, Question
from questionpy_common.api import PlainMapping
from questionpy_common.api.attempt import AttemptModel, AttemptScoredModel, AttemptStartedModel, AttemptUi, ScoreModel
from questionpy_common.api.question import QuestionInterface, QuestionModel
from questionpy_common.environment import get_qpy_environment
Expand Down Expand Up @@ -82,7 +83,7 @@ def _get_attempt_internal(
self,
attempt_state: str,
scoring_state: str | None = None,
response: PlainMapping | None = None,
response: dict[str, JsonValue] | None = None,
*,
compute_score: bool = False,
generate_hint: bool = False,
Expand All @@ -101,15 +102,15 @@ def _get_attempt_internal(
)

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:
return _export_attempt(self._get_attempt_internal(attempt_state, scoring_state, response))

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

0 comments on commit 990b07d

Please sign in to comment.