lovasoa / marshmallow_dataclass

Automatic generation of marshmallow schemas from dataclasses.
https://lovasoa.github.io/marshmallow_dataclass/html/marshmallow_dataclass.html
MIT License
456 stars 78 forks source link

field_for_schema can use _RECURSION_GUARD.seen_classes before it is initialized #229

Open dairiki opened 1 year ago

dairiki commented 1 year ago

Currently, while computing schemas for dataclasses, we keep some thread-local context in _RECURSION_GUARD.seen_classes.

The field_for_schema function does not manage the thread-locals in _RECURSION_GUARD, yet it can (attempt to) use them if called for a type that is or includes a dataclass (or other exotic types). This can cause an exception if field_for_schema is called before the first call to class_schema.

from marshmallow_dataclass import field_for_schema

class A:
    x: int

field_for_schema(A) # => AttributeError("'_thread__local' object has not attribute 'seen_classes'")

This bug is fixed by #214 (currently a work-in-progress).