From 45215d3054c275ccb4f46f174c6a9390782b1708 Mon Sep 17 00:00:00 2001 From: Jarkko Jaakola <jarkko.jaakola@aiven.io> Date: Wed, 16 Oct 2024 13:46:54 +0300 Subject: [PATCH] fix: Avro dataclass introspect typing --- src/karapace/avro_dataclasses/introspect.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/karapace/avro_dataclasses/introspect.py b/src/karapace/avro_dataclasses/introspect.py index be9634493..ced8c8e96 100644 --- a/src/karapace/avro_dataclasses/introspect.py +++ b/src/karapace/avro_dataclasses/introspect.py @@ -5,12 +5,12 @@ from __future__ import annotations -from .schema import AvroType, EnumType, FieldSchema, MapType, RecordSchema +from .schema import ArrayType, AvroType, EnumType, FieldSchema, MapType, RecordSchema, TypeObject from collections.abc import Mapping, Sequence from dataclasses import Field, fields, is_dataclass, MISSING from enum import Enum from functools import lru_cache -from typing import Final, get_args, get_origin, TYPE_CHECKING, TypeVar, Union +from typing import Final, get_args, get_origin, Literal, TYPE_CHECKING, TypeVar, Union import datetime import uuid @@ -42,10 +42,17 @@ def _field_type_array(field: Field, origin: type, type_: object) -> AvroType: else: (inner_type,) = get_args(type_) + items: AvroType + if is_dataclass(inner_type): + assert isinstance(inner_type, type) + items = record_schema(inner_type) + else: + items = _field_type(field, inner_type) + return { "name": f"one_of_{field.name}", "type": "array", - "items": (record_schema(inner_type) if is_dataclass(inner_type) else _field_type(field, inner_type)), + "items": items, } @@ -143,6 +150,7 @@ def field_schema(field: Field) -> FieldSchema: "name": field.name, "type": _field_type(field, field.type), } + assert isinstance(field.type, type) return ( { **schema,