pydantic-avro 0.7.2__py3-none-any.whl → 0.8.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.
@@ -39,13 +39,19 @@ def list_type_handler(t: dict) -> str:
39
39
 
40
40
  def map_type_handler(t: dict) -> str:
41
41
  """Get the Python type of a given Avro map type"""
42
+ type_field = t["type"]
43
+ value_type = None
44
+ if isinstance(type_field, dict):
45
+ avro_value_type = type_field.get("values")
46
+ if avro_value_type is None:
47
+ raise AttributeError(f"Values are required for map type. Received: {t}")
48
+ value_type = get_pydantic_type(avro_value_type)
49
+ if isinstance(type_field, str):
50
+ value_type = t.get("values")
51
+
52
+ if value_type is None:
53
+ raise AttributeError(f"Values are required for map type. Received: {t}")
42
54
 
43
- avro_value_type = t["type"].get("values")
44
-
45
- if avro_value_type is None:
46
- raise AttributeError("Values are required for map type")
47
-
48
- value_type = get_pydantic_type(avro_value_type)
49
55
  return f"Dict[str, {value_type}]"
50
56
 
51
57
 
@@ -129,7 +135,7 @@ def get_pydantic_type(t: Union[str, dict]) -> str:
129
135
  if isinstance(t, str):
130
136
  t = {"type": t}
131
137
 
132
- if isinstance(t["type"], str):
138
+ if isinstance(t.get("type"), str):
133
139
  if ClassRegistry().has_class(t["type"]):
134
140
  return t["type"]
135
141
 
@@ -22,6 +22,45 @@ STRING_TYPE_MAPPING = {
22
22
  "binary": "bytes",
23
23
  }
24
24
 
25
+ AVRO_TYPE_MAPPING = {
26
+ "timestamp-millis": {
27
+ "type": "long",
28
+ "logicalType": "timestamp-millis",
29
+ },
30
+ "timestamp-micros": {
31
+ "type": "long",
32
+ "logicalType": "timestamp-micros",
33
+ },
34
+ "time-millis": {
35
+ "type": "int",
36
+ "logicalType": "time-millis",
37
+ },
38
+ "time-micros": {
39
+ "type": "long",
40
+ "logicalType": "time-micros",
41
+ },
42
+ "decimal": {
43
+ "type": "bytes",
44
+ "logicalType": "decimal",
45
+ },
46
+ "uuid": {
47
+ "type": "string",
48
+ "logicalType": "uuid",
49
+ },
50
+ "int": "int",
51
+ "long": "long",
52
+ "float": "float",
53
+ "double": "double",
54
+ "boolean": "boolean",
55
+ "bytes": "bytes",
56
+ "string": "string",
57
+ "null": "null",
58
+ "date": {
59
+ "type": "int",
60
+ "logicalType": "date",
61
+ },
62
+ }
63
+
25
64
 
26
65
  def get_definition(ref: str, schema: dict):
27
66
  """Reading definition of base schema for nested structs"""
@@ -91,6 +130,7 @@ class AvroTypeConverter:
91
130
  t = field_props.get("type")
92
131
  f = field_props.get("format")
93
132
  r = field_props.get("$ref")
133
+ at = field_props.get("avro_type")
94
134
  if "allOf" in field_props and len(field_props["allOf"]) == 1:
95
135
  r = field_props["allOf"][0]["$ref"]
96
136
  u = field_props.get("anyOf")
@@ -101,6 +141,13 @@ class AvroTypeConverter:
101
141
  return self._handle_references(r, avro_type_dict)
102
142
  elif t is None:
103
143
  raise ValueError(f"Field '{field_props}' does not have a defined type.")
144
+ elif at is not None:
145
+ if not isinstance(at, str) or at not in AVRO_TYPE_MAPPING:
146
+ raise ValueError(
147
+ f"Field '{field_props}' does not have a supported avro_type. Type should be one of "
148
+ f" {AVRO_TYPE_MAPPING.keys()}"
149
+ )
150
+ avro_type_dict["type"] = AVRO_TYPE_MAPPING[at]
104
151
  elif t == "array":
105
152
  return self._array_to_avro(field_props, avro_type_dict)
106
153
  elif t == "string":
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pydantic-avro
3
- Version: 0.7.2
3
+ Version: 0.8.0
4
4
  Summary: Converting pydantic classes to avro schemas
5
5
  Home-page: https://github.com/godatadriven/pydantic-avro
6
6
  License: MIT
@@ -62,6 +62,17 @@ pydantic-avro avro_to_pydantic --asvc /path/to/schema.asvc
62
62
  pydantic-avro avro_to_pydantic --asvc /path/to/schema.asvc --output /path/to/output.py
63
63
  ```
64
64
 
65
+ ### Specify expected Avro type
66
+
67
+ ```python
68
+ from datetime import datetime
69
+ from pydantic import Field
70
+ from pydantic_avro.base import AvroBase
71
+
72
+ class ExampleModel(AvroBase):
73
+ field1: int = Field(..., avro_type="long") # Explicitly set Avro type to "long"
74
+ field2: datetime = Field(..., avro_type="timestamp-millis") # Explicitly set Avro type to "timestamp-millis"
75
+ ```
65
76
 
66
77
  ### Install for developers
67
78
 
@@ -5,14 +5,14 @@ pydantic_avro/base.py,sha256=i1wco_T6FXFdtlxzbzXt3Jv93TmTzTfJ493KCfZh1dc,78
5
5
  pydantic_avro/from_avro/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
6
  pydantic_avro/from_avro/avro_to_pydantic.py,sha256=FOEd_6TqoHSlLALgyveb456B3f7TEPGw2ByRgRcigqc,1356
7
7
  pydantic_avro/from_avro/class_registery.py,sha256=n_8yELp-Eux9bAs4CJOV78bQ061VV_-2FuvowOQpWCg,864
8
- pydantic_avro/from_avro/types.py,sha256=OIvadvTZJEbUDbTn2To6CqRpJhV8Un_jNqG5NKhqnkY,4864
8
+ pydantic_avro/from_avro/types.py,sha256=lt-L0mVAflSrpFxQKtjxx1lUCWICdSaXlwGfeJkfXgI,5167
9
9
  pydantic_avro/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
10
10
  pydantic_avro/to_avro/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
11
11
  pydantic_avro/to_avro/base.py,sha256=Y8QOGTpXO3BwJ6arrRK-k2JniK5kLTvlDpQ0PyZojmg,1409
12
12
  pydantic_avro/to_avro/config.py,sha256=R9HLkWiXs7YZ02te8CQt5Kf2rbsE8Fx-hjqv3tBSFJU,152
13
- pydantic_avro/to_avro/types.py,sha256=gY-4ay0IF1dDIxtVsLyzH-AkzcE_dHpnyCrd00cj3TE,7755
14
- pydantic_avro-0.7.2.dist-info/entry_points.txt,sha256=gwHiQfbGLO8Np2sa1bZ_bpxU7sEufx6IachViBE_Fnw,66
15
- pydantic_avro-0.7.2.dist-info/LICENSE,sha256=gBlYCG1yxb0vGlsmek0lMPVOK5YDxQope4F54jzeqoY,1069
16
- pydantic_avro-0.7.2.dist-info/WHEEL,sha256=vxFmldFsRN_Hx10GDvsdv1wroKq8r5Lzvjp6GZ4OO8c,88
17
- pydantic_avro-0.7.2.dist-info/METADATA,sha256=uy2VasYG9obxrtyy8AEyDuv_rMOiKQ64oRsiue3bt4g,2571
18
- pydantic_avro-0.7.2.dist-info/RECORD,,
13
+ pydantic_avro/to_avro/types.py,sha256=9bGoTDxejaHUsKRvQxwqF3-k7NGHU-vjTnhzR3o8m8o,8980
14
+ pydantic_avro-0.8.0.dist-info/entry_points.txt,sha256=gwHiQfbGLO8Np2sa1bZ_bpxU7sEufx6IachViBE_Fnw,66
15
+ pydantic_avro-0.8.0.dist-info/LICENSE,sha256=gBlYCG1yxb0vGlsmek0lMPVOK5YDxQope4F54jzeqoY,1069
16
+ pydantic_avro-0.8.0.dist-info/WHEEL,sha256=vxFmldFsRN_Hx10GDvsdv1wroKq8r5Lzvjp6GZ4OO8c,88
17
+ pydantic_avro-0.8.0.dist-info/METADATA,sha256=eqI3PQnW1t_AXqyV5gFFlV62w1czrR5m1IaTzY4DLzk,2945
18
+ pydantic_avro-0.8.0.dist-info/RECORD,,