You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
Problem is I have a dataclass which has a Union of multiple similar but slightly different schemas. One such schema has all the fields from another, but the other has an additional field. By suppling the strict and strict unions match parameters I am able fix this.
Full reproducible example below:
fromtypingimportType, TypeVar, Unionfromdataclassesimportdataclassfromdaciteimportfrom_dictfromdataclasses_avroschemaimportAvroModelfromdataclasses_avroschema.typesimportJsonDict@dataclassclassFoo(AvroModel):
b: int@dataclassclassBar(AvroModel):
a: strb: int@dataclassclassBaz(AvroModel):
action: Union[Foo, Bar]
data= {"a": "a", "b": 2}
bar=from_dict(Bar, data)
print(bar)
# Bar(a='a', b=2)baz=Baz(bar)
print(baz)
# Baz(action=Bar(a='a', b=2))serialized_val=Baz(bar).serialize()
print(Baz.deserialize(serialized_val, create_instance=False))
# {'action': {'a': 'a', 'b': 2}}print(Baz.deserialize(serialized_val))
# Baz(action=Foo(b=2)) # INCORRECT!CT=TypeVar("CT", bound="AvroModel")
classCustomAvroModel(AvroModel):
# https://github.com/marcosschroh/dataclasses-avroschema/blob/2a100666c93afb3f4916ea84b2ed9904b71a3632/dataclasses_avroschema/schema_generator.py#L206@classmethoddefconfig(cls: Type[CT]) ->JsonDict:
""" Get the default config for dacite and always include the self reference """# We need to make sure that the `avro schemas` has been generated, otherwise cls.klass is empty# It won't affect the performance because the rendered schema will be store in cls.rendered_schemaifcls.klassisNone:
# Generate dataclass and metadatacls.klass=cls.generate_dataclass()
return {
"check_types": False,
"forward_references": {
cls.klass.__name__: cls.klass,
},
"strict_unions_match": True,
"strict": True,
}
@dataclassclassBaz(CustomAvroModel):
action: Union[Foo, Bar]
baz=Baz(bar)
serialized_val=Baz(bar).serialize()
print(Baz.deserialize(serialized_val))
# Baz(action=Bar(a='a', b=2)) # CORRECT!
Describe the solution you'd like
I would like a simpler way to specify the dacite defaults passed to the config method. I am not sure the best way to handle this, however maybe something like the following would be better?:
Describe alternatives you've considered
Alternative is to have the CustomAvroModel as currently shown in the reproducible example. This could likely be abstracted better.
Additional context
No additional context.
The text was updated successfully, but these errors were encountered:
Is your feature request related to a problem? Please describe.
Problem is I have a dataclass which has a Union of multiple similar but slightly different schemas. One such schema has all the fields from another, but the other has an additional field. By suppling the strict and strict unions match parameters I am able fix this.
Full reproducible example below:
Describe the solution you'd like
I would like a simpler way to specify the dacite defaults passed to the
config
method. I am not sure the best way to handle this, however maybe something like the following would be better?:Describe alternatives you've considered
Alternative is to have the CustomAvroModel as currently shown in the reproducible example. This could likely be abstracted better.
Additional context
No additional context.
The text was updated successfully, but these errors were encountered: