diff --git a/ormar/fields/model_fields.py b/ormar/fields/model_fields.py index 30628de..5081da9 100644 --- a/ormar/fields/model_fields.py +++ b/ormar/fields/model_fields.py @@ -1,7 +1,7 @@ import datetime import decimal import uuid -from typing import Any, List, Literal, Optional, TYPE_CHECKING, Union, overload +from typing import Any, Optional, TYPE_CHECKING, Union, overload import pydantic import sqlalchemy @@ -11,6 +11,11 @@ from ormar.fields import sqlalchemy_uuid from ormar.fields.base import BaseField # noqa I101 from ormar.fields.sqlalchemy_encrypted import EncryptBackends +try: + from typing import Literal +except ImportError: # pragma: no cover + from typing_extensions import Literal # type: ignore + def is_field_nullable( nullable: Optional[bool], @@ -426,23 +431,23 @@ class JSON(ModelFieldFactory, pydantic.Json): return sqlalchemy.JSON() -if TYPE_CHECKING: # pragma: nocover +if TYPE_CHECKING: # pragma: nocover # noqa: C901 @overload def LargeBinary( - max_length: int, *, represent_as_base64_str: Literal[True], **kwargs + max_length: int, *, represent_as_base64_str: Literal[True], **kwargs: Any ) -> str: ... @overload def LargeBinary( - max_length: int, *, represent_as_base64_str: Literal[False], **kwargs + max_length: int, *, represent_as_base64_str: Literal[False], **kwargs: Any ) -> bytes: ... @overload def LargeBinary( - max_length: int, represent_as_base64_str: Literal[False] = ..., **kwargs + max_length: int, represent_as_base64_str: Literal[False] = ..., **kwargs: Any ) -> bytes: ... @@ -456,7 +461,8 @@ else: class LargeBinary(ModelFieldFactory, bytes): """ - LargeBinary field factory that construct Field classes and populated their values. + LargeBinary field factory that construct Field classes + and populated their values. """ _type = bytes diff --git a/ormar/models/helpers/validation.py b/ormar/models/helpers/validation.py index 981517f..c86687e 100644 --- a/ormar/models/helpers/validation.py +++ b/ormar/models/helpers/validation.py @@ -142,10 +142,8 @@ def generate_model_example(model: Type["Model"], relation_map: Dict = None) -> D ) for name, field in model.Meta.model_fields.items(): if not field.is_relation: - if field.__type__ == bytes and field.represent_as_base64_str: - example[name] = "string" - else: - example[name] = field.__sample__ + is_bytes_str = field.__type__ == bytes and field.represent_as_base64_str + example[name] = field.__sample__ if not is_bytes_str else "string" elif isinstance(relation_map, dict) and name in relation_map: example[name] = get_nested_model_example( name=name, field=field, relation_map=relation_map