justanr / marshmallow-annotations

Allows declaring marshmallow schema through type annotations
MIT License
48 stars 12 forks source link

No field factory for Attr class #29

Closed manuelmazzuola closed 3 years ago

manuelmazzuola commented 5 years ago

I'm stuck in nested attrs classes.
marshmallow-annotation==2.4.0
attrs==19.1.0
Given the following class:

import attr
import typing
from marshmallow_annotations.ext.attrs import AttrsSchema

@attr.s(auto_attribs=True)
class Child:
    name: str

@attr.s(auto_attribs=True)
class Dad:
    children: typing.List[Child] = attr.ib(factory=list)

class ChildSchema(AttrsSchema):
    class Meta:
        target = Child

class DadSchema(AttrsSchema):
    class Meta:
        target = Dad

I get the error: marshmallow_annotations.exceptions.AnnotationConversionError: No field factory found for <class 'models.test.Child'> when DadSchema is being parsed.

Full stack trace:

  from models.test import ChildSchema, DadSchema
  File "/home/originof/src/test-mine/test_mine/models/test.py", line 21, in <module>
    class DadSchema(AttrsSchema):
  File "/home/originof/.local/share/virtualenvs/test-mine-3MfCQHxU/lib/python3.7/site-packages/marshmallow/schema.py", line 118, in __new__
    dict_cls=dict_cls
  File "/home/originof/.local/share/virtualenvs/test-mine-3MfCQHxU/lib/python3.7/site-packages/marshmallow_annotations/scheme.py", line 110, in get_declared_fields
    fields.update(converter.convert_all(target, ignore, klass.opts.field_configs))
  File "/home/originof/.local/share/virtualenvs/test-mine-3MfCQHxU/lib/python3.7/site-packages/marshmallow_annotations/ext/attrs.py", line 49, in convert_all
    return super().convert_all(target, ignore, configs)
  File "/home/originof/.local/share/virtualenvs/test-mine-3MfCQHxU/lib/python3.7/site-packages/marshmallow_annotations/converter.py", line 80, in convert_all
    for k, v in self._get_type_hints(target, ignore)
  File "/home/originof/.local/share/virtualenvs/test-mine-3MfCQHxU/lib/python3.7/site-packages/marshmallow_annotations/converter.py", line 80, in <dictcomp>
    for k, v in self._get_type_hints(target, ignore)
  File "/home/originof/.local/share/virtualenvs/test-mine-3MfCQHxU/lib/python3.7/site-packages/marshmallow_annotations/converter.py", line 66, in convert
    typehint, opts, field_name=field_name, target=target
  File "/home/originof/.local/share/virtualenvs/test-mine-3MfCQHxU/lib/python3.7/site-packages/marshmallow_annotations/converter.py", line 117, in _field_from_typehint
    return field_constructor(self, subtypes, kwargs)
  File "/home/originof/.local/share/virtualenvs/test-mine-3MfCQHxU/lib/python3.7/site-packages/marshmallow_annotations/registry.py", line 53, in _list_converter
    if converter.is_scheme(subtypes[0]):
  File "/home/originof/.local/share/virtualenvs/test-mine-3MfCQHxU/lib/python3.7/site-packages/marshmallow_annotations/converter.py", line 84, in is_scheme
    constructor = self.registry.get(typehint)
  File "/home/originof/.local/share/virtualenvs/test-mine-3MfCQHxU/lib/python3.7/site-packages/marshmallow_annotations/registry.py", line 120, in get

Any help would be welcome :D Thanks

manuelmazzuola commented 5 years ago

Maybe I've resolved, added register_as_scheme to ChildSchema.Meta class. What register_as_scheme does?

justanr commented 5 years ago

It creates the field factory for the type and scheme in the registry provided to the scheme. There's an implicit registry that's used under the hood.

register_as_scheme is documented in the Quickstart guide but is probably worth adding to the Customizing section as well.

manuelmazzuola commented 5 years ago

register_as_scheme is documented in the Quickstart guide but is probably worth adding to the Customizing section as well.

:+1: , or maybe you can change the error from No field factory found for <class 'models.test.Child'> to No field factory found for <class 'models.test.Child'>, forgot register_as_scheme attribute?

BTW, great job you did on this library :100: :+1:

justanr commented 3 years ago

Updated the error message and closed this issue. Sorry for the very late action on this :/ feel free to re-open this if it should be.