Skip to content

new typing.UnionType not supported #212

Closed
@Panaetius

Description

Describe the bug
https://peps.python.org/pep-0604/ introduces a new typing.UnionType which can be specified using |, which currently does not work in dataclasses-avroschema.

To Reproduce

from dataclasses_avroschema import AvroModel
class MyModel(AvroModel):
     test: int | bool
     
MyModel.avro_schema()

Output:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/path/to/dataclasses_avroschema/schema_generator.py", line 70, in avro_schema
    avro_schema = cls.generate_schema(schema_type=AVRO)
  File "/path/to/dataclasses_avroschema/schema_generator.py", line 55, in generate_schema
    cls.schema_def = cls._generate_avro_schema()
  File "/path/to/dataclasses_avroschema/schema_generator.py", line 66, in _generate_avro_schema
    return AvroSchemaDefinition("record", cls.klass, metadata=metadata, parent=cls.parent or cls)
  File "<string>", line 8, in __init__
  File "/path/to/dataclasses_avroschema/schema_definition.py", line 56, in __post_init__
    self.fields = self.parse_dataclasses_fields()
  File "/path/to/dataclasses_avroschema/schema_definition.py", line 63, in parse_dataclasses_fields
    return self.parse_fields()
  File "/path/to/dataclasses_avroschema/schema_definition.py", line 66, in parse_fields
    return [
  File "/path/to/dataclasses_avroschema/schema_definition.py", line 67, in <listcomp>
    AvroField(
  File "/path/to/dataclasses_avroschema/fields.py", line 1096, in field_factory
    raise ValueError(msg)
ValueError: Type int | bool is unknown. Please check the valid types at https://marcosschroh.github.io/dataclasses-avroschema/fields_specification/#avro-field-and-python-types-summary

Expected behavior

Should output '{"type": "record", "name": "MyModel", "fields": [{"name": "test", "type": ["long", "boolean"]}], "doc": "MyModel(test: Union[int, bool])"}'

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions