remove .vscode settings, re-dump orjson choices to fix choices, move mypy config into pyproject.toml
This commit is contained in:
@ -64,8 +64,11 @@ def convert_choices_if_needed( # noqa: CCR001
|
||||
value = value.isoformat() if not isinstance(value, str) else value
|
||||
choices = [o.isoformat() for o in field.choices]
|
||||
elif field.__type__ == pydantic.Json:
|
||||
value = json.dumps(value) if not isinstance(value, str) else value
|
||||
value = (
|
||||
json.dumps(value) if not isinstance(value, str) else re_dump_value(value)
|
||||
)
|
||||
value = value.decode("utf-8") if isinstance(value, bytes) else value
|
||||
choices = [re_dump_value(x) for x in field.choices]
|
||||
elif field.__type__ == uuid.UUID:
|
||||
value = str(value) if not isinstance(value, str) else value
|
||||
choices = [str(o) for o in field.choices]
|
||||
@ -86,6 +89,21 @@ def convert_choices_if_needed( # noqa: CCR001
|
||||
return value, choices
|
||||
|
||||
|
||||
def re_dump_value(value: str) -> str:
|
||||
"""
|
||||
Rw-dumps choices due to different string representation in orjson and json
|
||||
:param value: string to re-dump
|
||||
:type value: str
|
||||
:return: re-dumped choices
|
||||
:rtype: List[str]
|
||||
"""
|
||||
try:
|
||||
result: Union[str, bytes] = json.dumps(json.loads(value))
|
||||
except json.JSONDecodeError:
|
||||
result = value
|
||||
return result.decode("utf-8") if isinstance(result, bytes) else result
|
||||
|
||||
|
||||
def validate_choices(field: "BaseField", value: Any) -> None:
|
||||
"""
|
||||
Validates if given value is in provided choices.
|
||||
|
||||
@ -1,5 +1,15 @@
|
||||
import uuid
|
||||
from typing import Callable, Collection, Dict, List, Optional, Set, TYPE_CHECKING, cast
|
||||
from typing import (
|
||||
Any,
|
||||
Callable,
|
||||
Collection,
|
||||
Dict,
|
||||
List,
|
||||
Optional,
|
||||
Set,
|
||||
TYPE_CHECKING,
|
||||
cast,
|
||||
)
|
||||
|
||||
import ormar
|
||||
from ormar.exceptions import ModelPersistenceError
|
||||
@ -93,7 +103,7 @@ class SavePrepareMixin(RelationMixin, AliasMixin):
|
||||
if field.__type__ == uuid.UUID and name in model_dict:
|
||||
parsers = {"string": lambda x: str(x), "hex": lambda x: "%.32x" % x.int}
|
||||
uuid_format = field.column_type.uuid_format
|
||||
parser = parsers.get(uuid_format, lambda x: x)
|
||||
parser: Callable[..., Any] = parsers.get(uuid_format, lambda x: x)
|
||||
model_dict[name] = parser(model_dict[name])
|
||||
return model_dict
|
||||
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
from typing import Any, Dict, List, Optional, TYPE_CHECKING, Tuple, Type, Union, cast
|
||||
|
||||
try:
|
||||
from sqlalchemy.engine.result import ResultProxy
|
||||
from sqlalchemy.engine.result import ResultProxy # type: ignore
|
||||
except ImportError: # pragma: no cover
|
||||
from sqlalchemy.engine.result import Row as ResultProxy # type: ignore
|
||||
|
||||
|
||||
Reference in New Issue
Block a user