Handle forward references #5
Comments
Working for forward references to self -- need to figure out how to handle forward references to types that aren't scheme'd yet. |
Thought: subclass Nested and make it registry/converter aware? Pro: Circular schema now work. |
Minimal reproducible example from typing import List
from marshmallow_annotations import AnnotationSchema
class Foo:
bars: List["Bar"]
class Bar:
foos: List[Foo]
class FooSchema(AnnotationSchema):
class Meta:
target = Foo
register_as_scheme = True
class BarSchema(AnnotationSchema):
class Meta:
target = Bar
register_as_scheme = True results in This can be worked around (in my actual case) by manually providing a nested field (the actual case I discovered this in was using sqlalchemy backrefs and providing a type hint on the backref'd type). For an actual solution, not sure tbh. Could add a special marker entry in the The thunk registered could be slightly smart and cache the converter after first usage. I'm really not sure what downstream effects there might be (e.g. if something is introspecting marshmallow fields to do something) Something that I like less is adding some sort of Essentially it's an eager version of using a thunk that replaces it's internal implementation with the actual field. In this instance, if something is inspected marshmallow fields to gather information then that could be (in theory at least) delayed until after the reify function is called. Finding the "thunked" schemas might be as easy as getting a hold of the marshmallow schema registry, chaining all the values together and looking for any that carry like a def reify(*, schema_registry=None, converter_registry=None):
if schema_registry is None:
from marshmallow.class_registry import _registry as schema_registry
if converter_registry is None:
from marshmallow_annotations.registry import registry as converter_registry
schemes = (s for s in chain.from_iterable(registry.values()) if getattr(s, "__ANNOATION_THUNKED_FIELD__", False))
for schema in schemes:
_reify_internal(schema, converter_registry)
def _reify_internal(schema, converter_registry):
# find thunked fields
# for each thunked field, generate actual converter field and replace thunk
# remove __ANNOTATION_THUNKED_FIELD__
pass In either case, the thunk needs to carry over information of the hinted type as well as any options that should be passed to the converter. And I don't think there's a reason both couldn't be supported. |
Could possibly be handled completely in registry by returning a special |
This is fixed on 3.6.5+ but we need to handle it properly - some sort of delayed field instantiation maybe.
would delay only apply to forward references to the target it self
if not, how to delay look up for types that don't have a scheme yet
The text was updated successfully, but these errors were encountered: