python-desert / desert

Deserialize to objects while staying DRY
https://desert.readthedocs.io
MIT License
158 stars 10 forks source link

Different schema classes are generated per reference of the same data class #270

Open jmgoncalves opened 1 year ago

jmgoncalves commented 1 year ago
from dataclasses import dataclass
import desert

@dataclass
class A:
    some_string: str

@dataclass
class B:
    prop_one: A
    prop_two: A

if __name__ == "__main__":
    schema = desert.schema_class(B)()
    prop_one_schema = schema.fields["prop_one"].schema
    prop_two_schema = schema.fields["prop_two"].schema
    print(isinstance(prop_one_schema, prop_one_schema.__class__)) # True
    print(isinstance(prop_one_schema, prop_two_schema.__class__)) # False
    print(isinstance(prop_two_schema, prop_one_schema.__class__)) # False
    print(isinstance(prop_two_schema, prop_two_schema.__class__)) # True

For the same class A there are as many schema classes as there are properties of type A.

This is inefficient (uses up more memory than needed) and can cause problems with other libraries, such as https://github.com/marshmallow-code/apispec which uses the schema class and modifiers to determine schema equality.