pydantic-avro 0.9.3__py3-none-any.whl → 0.10.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pydantic_avro/__init__.py CHANGED
@@ -1 +1,3 @@
1
- from pydantic_avro.to_avro.base import AvroBase
1
+ from pydantic_avro.to_avro.base import AvroBase as AvroBase
2
+
3
+ __all__ = ["AvroBase"]
@@ -1,3 +1,4 @@
1
+ from copy import deepcopy
1
2
  from typing import Any, Dict, List, Optional, Set
2
3
 
3
4
  from pydantic_avro.to_avro.config import DEFS_NAME
@@ -133,6 +134,13 @@ class AvroTypeConverter:
133
134
  f = field_props.get("format")
134
135
  r = field_props.get("$ref")
135
136
  at = field_props.get("avro_type")
137
+
138
+ # For Pydantic v1, check json_schema_extra for avro_type
139
+ if at is None and "json_schema_extra" in field_props:
140
+ json_schema_extra = field_props.get("json_schema_extra")
141
+ if isinstance(json_schema_extra, dict):
142
+ at = json_schema_extra.get("avro_type")
143
+
136
144
  if "allOf" in field_props and len(field_props["allOf"]) == 1:
137
145
  r = field_props["allOf"][0]["$ref"]
138
146
  if ("prefixItems" in field_props or ("minItems" in field_props and "maxItems" in field_props)) and t == "array":
@@ -142,9 +150,9 @@ class AvroTypeConverter:
142
150
  discriminator = field_props.get("discriminator")
143
151
 
144
152
  if u is not None:
145
- return self._union_to_avro(u, avro_type_dict, discriminator)
153
+ return self._union_to_avro(u, avro_type_dict, discriminator, parent_avro_type=at)
146
154
  elif o is not None:
147
- return self._union_to_avro(o, avro_type_dict, discriminator)
155
+ return self._union_to_avro(o, avro_type_dict, discriminator, parent_avro_type=at)
148
156
  elif r is not None:
149
157
  return self._handle_references(r, avro_type_dict)
150
158
  elif t is None:
@@ -232,15 +240,37 @@ class AvroTypeConverter:
232
240
  value_type = self._get_avro_type_dict(a)["type"]
233
241
  return {"type": "map", "values": value_type}
234
242
 
235
- def _union_to_avro(self, field_props: list, avro_type_dict: dict, discriminator: Optional[dict] = None) -> dict:
243
+ def _should_propagate_avro_type(self, union_element: dict, parent_avro_type: str) -> bool:
244
+ """Determine if parent avro_type should be propagated to this union element"""
245
+ # Don't propagate to null types or elements that already have avro_type
246
+ if union_element.get("type") == "null" or "avro_type" in union_element:
247
+ return False
248
+
249
+ element_format = union_element.get("format")
250
+
251
+ # Only propagate temporal avro_types to date-time formatted elements
252
+ temporal_avro_types = {"timestamp-millis", "timestamp-micros", "time-millis", "time-micros", "date"}
253
+ if parent_avro_type in temporal_avro_types:
254
+ return element_format in {"date-time", "date", "time"}
255
+
256
+ # For other avro_types, only propagate if there's no format (meaning it's not already specialized)
257
+ return element_format is None
258
+
259
+ def _union_to_avro(self, field_props: list, avro_type_dict: dict, discriminator: Optional[dict] = None, parent_avro_type: Optional[str] = None) -> dict:
236
260
  """Returns a type of a union field, including discriminated unions"""
237
261
  # Handle discriminated unions
238
262
  if discriminator is not None:
239
263
  return self._discriminated_union_to_avro(field_props, avro_type_dict, discriminator)
240
264
 
241
- # Standard union handling (unchanged)
265
+ # Standard union handling
242
266
  avro_type_dict["type"] = []
243
267
  for union_element in field_props:
268
+ # Propagate parent avro_type to compatible union elements
269
+ if parent_avro_type is not None and self._should_propagate_avro_type(union_element, parent_avro_type):
270
+ # Create a deep copy to avoid modifying the original
271
+ union_element = deepcopy(union_element)
272
+ union_element["avro_type"] = parent_avro_type
273
+
244
274
  t = self._get_avro_type_dict(union_element)
245
275
  avro_type_dict["type"].append(t["type"])
246
276
  return avro_type_dict
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pydantic-avro
3
- Version: 0.9.3
3
+ Version: 0.10.0
4
4
  Summary: Converting pydantic classes to avro schemas
5
5
  Home-page: https://github.com/godatadriven/pydantic-avro
6
6
  License: MIT
@@ -1,4 +1,4 @@
1
- pydantic_avro/__init__.py,sha256=P4vaozEL8Rea7xWdB6ENj3DNF2RPPOWascfwbtQS7gE,48
1
+ pydantic_avro/__init__.py,sha256=BhoChBv6KWCDjep-S3cAT7YELqnVp33gYYAoGA8r42o,84
2
2
  pydantic_avro/__main__.py,sha256=-AL9FNYsAdFmilm-96MbbxymTW6QminXPCBZJ1nkqBE,695
3
3
  pydantic_avro/base.py,sha256=i1wco_T6FXFdtlxzbzXt3Jv93TmTzTfJ493KCfZh1dc,78
4
4
  pydantic_avro/from_avro/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -9,9 +9,9 @@ pydantic_avro/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
9
  pydantic_avro/to_avro/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
10
10
  pydantic_avro/to_avro/base.py,sha256=QtCgXnqhcQAxgWVihz5idwoY4slmkJ2WDKcGqLld9Ig,2066
11
11
  pydantic_avro/to_avro/config.py,sha256=R9HLkWiXs7YZ02te8CQt5Kf2rbsE8Fx-hjqv3tBSFJU,152
12
- pydantic_avro/to_avro/types.py,sha256=-qemQC_vs-xy7VJDTQ1y1k7K1J6Qv3pTLILqp17MO2A,12731
13
- pydantic_avro-0.9.3.dist-info/entry_points.txt,sha256=gwHiQfbGLO8Np2sa1bZ_bpxU7sEufx6IachViBE_Fnw,66
14
- pydantic_avro-0.9.3.dist-info/LICENSE,sha256=gBlYCG1yxb0vGlsmek0lMPVOK5YDxQope4F54jzeqoY,1069
15
- pydantic_avro-0.9.3.dist-info/WHEEL,sha256=vxFmldFsRN_Hx10GDvsdv1wroKq8r5Lzvjp6GZ4OO8c,88
16
- pydantic_avro-0.9.3.dist-info/METADATA,sha256=r_czdRovj5dhYm8sfsLTm16EY7HD5LAGEsOSMJnUZ7g,2945
17
- pydantic_avro-0.9.3.dist-info/RECORD,,
12
+ pydantic_avro/to_avro/types.py,sha256=qLzIjsjC993Y15Q-haALkVmjeXg8cDSho9-uWQzKTa4,14432
13
+ pydantic_avro-0.10.0.dist-info/entry_points.txt,sha256=gwHiQfbGLO8Np2sa1bZ_bpxU7sEufx6IachViBE_Fnw,66
14
+ pydantic_avro-0.10.0.dist-info/LICENSE,sha256=gBlYCG1yxb0vGlsmek0lMPVOK5YDxQope4F54jzeqoY,1069
15
+ pydantic_avro-0.10.0.dist-info/WHEEL,sha256=vxFmldFsRN_Hx10GDvsdv1wroKq8r5Lzvjp6GZ4OO8c,88
16
+ pydantic_avro-0.10.0.dist-info/METADATA,sha256=wTZ2dMxMbY9UcsDpS4ZqavFrgUYk0XX88--098JHk_s,2946
17
+ pydantic_avro-0.10.0.dist-info/RECORD,,