diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a6dd943..c57598f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -12,3 +12,17 @@ repos: rev: v0.910 hooks: - id: mypy + args: [--no-strict-optional, --ignore-missing-imports] + additional_dependencies: [ + types-ujson>=0.1.1, + types-PyMySQL>=1.0.2, + types-ipaddress>=1.0.0, + types-enum34>=1.1.0, + types-cryptography>=3.3.5, + types-orjson>=3.6.0, + types-aiofiles>=0.1.9, + types-pkg-resources>=0.1.3, + types-requests>=2.25.9, + types-toml>=0.10.0, + pydantic>=1.8.2 + ] diff --git a/ormar/fields/model_fields.py b/ormar/fields/model_fields.py index a404204..a819b61 100644 --- a/ormar/fields/model_fields.py +++ b/ormar/fields/model_fields.py @@ -63,7 +63,11 @@ def is_auto_primary_key(primary_key: bool, autoincrement: bool) -> bool: def convert_choices_if_needed( - field_type: "Type", choices: Set, nullable: bool, scale: int = None + field_type: "Type", + choices: Set, + nullable: bool, + scale: int = None, + represent_as_str: bool = False, ) -> Set: """ Converts dates to isoformat as fastapi can check this condition in routes @@ -74,10 +78,14 @@ def convert_choices_if_needed( :param field_type: type o the field :type field_type: Type - :param nullable: set of choices - :type nullable: Set + :param choices: set of choices + :type choices: Set :param scale: scale for decimals :type scale: int + :param nullable: flag if field_nullable + :type nullable: bool + :param represent_as_str: flag for bytes fields + :type represent_as_str: bool :param scale: scale for decimals :type scale: int :return: value, choices list @@ -88,6 +96,8 @@ def convert_choices_if_needed( if field_type == decimal.Decimal: precision = scale choices = {encoder(o, precision) for o in choices} + elif field_type == bytes: + choices = {encoder(o, represent_as_str) for o in choices} elif encoder: choices = {encoder(o) for o in choices} if nullable: @@ -138,6 +148,7 @@ class ModelFieldFactory: choices=choices, nullable=nullable, scale=kwargs.get("scale", None), + represent_as_str=kwargs.get("represent_as_base64_str", False), ) namespace = dict( diff --git a/ormar/models/helpers/validation.py b/ormar/models/helpers/validation.py index 0ed1635..b3e8e15 100644 --- a/ormar/models/helpers/validation.py +++ b/ormar/models/helpers/validation.py @@ -63,6 +63,9 @@ def convert_value_if_needed(field: "BaseField", value: Any) -> Any: if field.__type__ == decimal.Decimal: precision = field.scale # type: ignore value = encoder(value, precision) + elif field.__type__ == bytes: + represent_as_string = field.represent_as_base64_str + value = encoder(value, represent_as_string) elif encoder: value = encoder(value) return value diff --git a/tests/test_fastapi/test_choices_schema.py b/tests/test_fastapi/test_choices_schema.py index da8a167..800c90e 100644 --- a/tests/test_fastapi/test_choices_schema.py +++ b/tests/test_fastapi/test_choices_schema.py @@ -121,8 +121,6 @@ def test_all_endpoints(): "blob_col": blob.decode("utf-8"), }, ) - if response.status_code != 200: - print(response.text) assert response.status_code == 200 item = Organisation(**response.json()) assert item.pk is not None diff --git a/tests/test_inheritance_and_pydantic_generation/test_pydantic_fields_order.py b/tests/test_inheritance_and_pydantic_generation/test_pydantic_fields_order.py index 96d5f98..ffc515e 100644 --- a/tests/test_inheritance_and_pydantic_generation/test_pydantic_fields_order.py +++ b/tests/test_inheritance_and_pydantic_generation/test_pydantic_fields_order.py @@ -14,7 +14,7 @@ class BaseMeta(ormar.ModelMeta): metadata = metadata -class TestModel(ormar.Model): +class NewTestModel(ormar.Model): class Meta: database = database metadata = metadata @@ -37,5 +37,5 @@ def create_test_database(): def test_model_field_order(): - TestCreate = TestModel.get_pydantic(exclude={"a"}) + TestCreate = NewTestModel.get_pydantic(exclude={"a"}) assert list(TestCreate.__fields__.keys()) == ["b", "c", "d", "e", "f"]