-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: define protocols for started and scored attempts
- Loading branch information
Showing
3 changed files
with
128 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,15 +2,15 @@ | |
# 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 abc import ABC | ||
from typing import ClassVar, Generic, Self, TypeVar | ||
from typing import ClassVar, Generic, Self, TypeVar, cast | ||
|
||
from pydantic import BaseModel, JsonValue, ValidationError | ||
|
||
from questionpy_common.api.qtype import InvalidQuestionStateError, OptionsFormValidationError | ||
from questionpy_common.api.question import ScoringMethod, SubquestionModel | ||
from questionpy_common.environment import get_qpy_environment | ||
|
||
from ._attempt import Attempt | ||
from ._attempt import Attempt, AttemptProtocol, AttemptScoredProtocol, AttemptStartedProtocol | ||
from ._util import get_mro_type_hint | ||
from .form import FormModel, OptionsFormDefinition | ||
|
||
|
@@ -126,7 +126,7 @@ def get_options_form(self) -> tuple[OptionsFormDefinition, dict[str, JsonValue]] | |
"""Return the options form and field values for viewing or editing this question.""" | ||
return self.options_class.qpy_form, self.options.model_dump(mode="json") | ||
|
||
def start_attempt(self, variant: int) -> Attempt: | ||
def start_attempt(self, variant: int) -> AttemptStartedProtocol: | ||
attempt_state = self.attempt_class.make_attempt_state(self, variant) | ||
return self.attempt_class(self, attempt_state) | ||
|
||
|
@@ -135,14 +135,27 @@ def get_attempt( | |
attempt_state: dict[str, JsonValue], | ||
scoring_state: dict[str, JsonValue] | None = None, | ||
response: dict[str, JsonValue] | None = None, | ||
) -> Attempt: | ||
) -> AttemptProtocol: | ||
parsed_attempt_state = self.attempt_class.attempt_state_class.model_validate(attempt_state) | ||
parsed_scoring_state = None | ||
if scoring_state is not None: | ||
parsed_scoring_state = self.attempt_class.scoring_state_class.model_validate(scoring_state) | ||
|
||
return self.attempt_class(self, parsed_attempt_state, parsed_scoring_state, response) | ||
|
||
def score_attempt( | ||
self, | ||
attempt_state: dict[str, JsonValue], | ||
scoring_state: dict[str, JsonValue] | None, | ||
response: dict[str, JsonValue] | None, | ||
*, | ||
try_scoring_with_countback: bool, | ||
try_giving_hint: bool, | ||
) -> AttemptScoredProtocol: | ||
attempt = self.get_attempt(attempt_state, scoring_state, response) | ||
attempt.score_response(try_scoring_with_countback=try_scoring_with_countback, try_giving_hint=try_giving_hint) | ||
return cast(AttemptScoredProtocol, attempt) | ||
|
||
def __init_subclass__(cls, *args: object, **kwargs: object) -> None: | ||
super().__init_subclass__(*args, **kwargs) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters