Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using marshmallow.fields.Enum in TYPE_MAPPING for enum fields #485

Open
monim67 opened this issue Feb 7, 2023 · 4 comments
Open

Using marshmallow.fields.Enum in TYPE_MAPPING for enum fields #485

monim67 opened this issue Feb 7, 2023 · 4 comments

Comments

@monim67
Copy link

monim67 commented Feb 7, 2023

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'
@rimvislt
Copy link

rimvislt commented Feb 7, 2023

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)

@lafrech
Copy link
Member

lafrech commented Feb 8, 2023

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

@lafrech
Copy link
Member

lafrech commented Feb 23, 2023

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

@monim67
Copy link
Author

monim67 commented Mar 13, 2023

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants