marshmallow-sqlalchemy icon indicating copy to clipboard operation
marshmallow-sqlalchemy copied to clipboard

Using marshmallow.fields.Enum in TYPE_MAPPING for enum fields

Open monim67 opened this issue 2 years ago • 4 comments

Marshmallow now has fields.Enum from version 3.18. Using this in ModelConverter.SQLA_TYPE_MAPPING for enum fields raises exception.

class ExtendedModelConverter(ModelConverter):
    SQLA_TYPE_MAPPING = {
        **ModelConverter.SQLA_TYPE_MAPPING,
        Enum: fields.Enum,
    }

Stacktrace:

  File "site-packages/marshmallow/schema.py", line 121, in __new__
    klass._declared_fields = mcs.get_declared_fields(
  File "site-packages/marshmallow_sqlalchemy/schema.py", line 91, in get_declared_fields
    fields.update(mcs.get_declared_sqla_fields(fields, converter, opts, dict_cls))
  File "site-packages/marshmallow_sqlalchemy/schema.py", line 130, in get_declared_sqla_fields
    converter.fields_for_model(
  File "site-packages/marshmallow_sqlalchemy/convert.py", line 154, in fields_for_model
    field = base_fields.get(key) or self.property2field(prop)
  File "site-packages/marshmallow_sqlalchemy/convert.py", line 198, in property2field
    ret = field_class(**field_kwargs)
TypeError: __init__() missing 1 required positional argument: 'enum'

monim67 avatar Feb 07 '23 12:02 monim67

Getting the same error with https://github.com/sqlalchemy/sqlalchemy/releases/tag/rel_2_0_2 it works with 2.0.1. For now I downgraded to sqlalchemy to 2.0.1 but not sure if I need to fix something in code or wait until this repo is fixed.

class UserSchema(SQLAlchemyAutoSchema): File "/usr/local/lib/python3.11/site-packages/marshmallow/schema.py", line 121, in __new__ klass._declared_fields = mcs.get_declared_fields( ^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/marshmallow_sqlalchemy/schema.py", line 91, in get_declared_fields fields.update(mcs.get_declared_sqla_fields(fields, converter, opts, dict_cls)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/marshmallow_sqlalchemy/schema.py", line 130, in get_declared_sqla_fields converter.fields_for_model( File "/usr/local/lib/python3.11/site-packages/marshmallow_sqlalchemy/convert.py", line 154, in fields_for_model field = base_fields.get(key) or self.property2field(prop) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/marshmallow_sqlalchemy/convert.py", line 193, in property2field field_class = field_class or self._get_field_class_for_property(prop) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/marshmallow_sqlalchemy/convert.py", line 275, in _get_field_class_for_property column = _base_column(prop.columns[0]) ^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/sqlalchemy/util/langhelpers.py", line 1329, in __getattr__ return self._fallback_getattr(key) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/sqlalchemy/util/langhelpers.py", line 1298, in _fallback_getattr raise AttributeError(key)

rimvislt avatar Feb 07 '23 23:02 rimvislt

@rimvislt I'm afraid SQLA 2 is not supported at all (CI fails with 2.0). See #488.

lafrech avatar Feb 08 '23 09:02 lafrech

And Enum field can't be instantiated like this. It requires an enum as argument.

lafrech avatar Feb 23 '23 23:02 lafrech

And Enum field can't be instantiated like this. It requires an enum as argument.

Is there any plan to support marshmallow.fields.Enum? Or there's other ways or work around for this issue?

monim67 avatar Mar 13 '23 15:03 monim67

@monim67 I gave it a shot in !611.

There is also a workaround mentioned in #112.

panda-byte avatar Aug 14 '24 13:08 panda-byte