diff options
author | 2024-07-08 15:09:17 +0100 | |
---|---|---|
committer | 2024-07-08 15:09:17 +0100 | |
commit | 642c0795c8738bf8b9ae39b9cf0180f7cdbac650 (patch) | |
tree | 4a075255d00d9f8a2f369567bdb79f6eefa4be9a /backend/models/question.py | |
parent | Migration to official Sentry release CI action (#275) (diff) | |
parent | Stop using gunicorn and use uvicorn directly to run application (diff) |
Merge pull request #276 from python-discord/jb3/environ/python-3.12
3.12 + Updates
Diffstat (limited to 'backend/models/question.py')
-rw-r--r-- | backend/models/question.py | 33 |
1 files changed, 18 insertions, 15 deletions
diff --git a/backend/models/question.py b/backend/models/question.py index 201aa51..a13ce93 100644 --- a/backend/models/question.py +++ b/backend/models/question.py @@ -4,11 +4,12 @@ from pydantic import BaseModel, Field, root_validator, validator from backend.constants import QUESTION_TYPES, REQUIRED_QUESTION_TYPE_DATA -_TESTS_TYPE = t.Union[t.Dict[str, str], int] +_TESTS_TYPE = dict[str, str] | int class Unittests(BaseModel): """Schema model for unittest suites in code questions.""" + allow_failure: bool = False tests: _TESTS_TYPE @@ -16,17 +17,19 @@ class Unittests(BaseModel): def validate_tests(cls, value: _TESTS_TYPE) -> _TESTS_TYPE: """Confirm that at least one test exists in a test suite.""" if isinstance(value, dict): - keys = len(value.keys()) - (1 if "setUp" in value.keys() else 0) + keys = len(value.keys()) - (1 if "setUp" in value else 0) if keys == 0: - raise ValueError("Must have at least one test in a test suite.") + msg = "Must have at least one test in a test suite." + raise ValueError(msg) return value class CodeQuestion(BaseModel): """Schema model for questions of type `code`.""" + language: str - unittests: t.Optional[Unittests] + unittests: Unittests | None class Question(BaseModel): @@ -42,22 +45,20 @@ class Question(BaseModel): allow_population_by_field_name = True @validator("type", pre=True) - def validate_question_type(cls, value: str) -> t.Optional[str]: + def validate_question_type(cls, value: str) -> str: """Checks if question type in currently allowed types list.""" value = value.lower() if value not in QUESTION_TYPES: - raise ValueError( - f"{value} is not valid question type. " - f"Allowed question types: {QUESTION_TYPES}." - ) + msg = f"{value} is not valid question type. Allowed question types: {QUESTION_TYPES}." + raise ValueError(msg) return value @root_validator def validate_question_data( - cls, - value: dict[str, t.Any] - ) -> t.Optional[dict[str, t.Any]]: + cls, + value: dict[str, t.Any], + ) -> dict[str, t.Any]: """Check does required data exists for question type and remove other data.""" # When question type don't need data, don't add anything to keep DB clean. if value.get("type") not in REQUIRED_QUESTION_TYPE_DATA: @@ -65,13 +66,15 @@ class Question(BaseModel): for key, data_type in REQUIRED_QUESTION_TYPE_DATA[value["type"]].items(): if key not in value.get("data", {}): - raise ValueError(f"Required question data key '{key}' not provided.") + msg = f"Required question data key '{key}' not provided." + raise ValueError(msg) if not isinstance(value["data"][key], data_type): - raise ValueError( + msg = ( f"Question data key '{key}' expects {data_type.__name__}, " - f"got {type(value['data'][key]).__name__} instead." + f"got {type(value["data"][key]).__name__} instead." ) + raise TypeError(msg) # Validate unittest options if value.get("type").lower() == "code": |