pydantic-avro 0.7.1__tar.gz → 0.7.3__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pydantic-avro
3
- Version: 0.7.1
3
+ Version: 0.7.3
4
4
  Summary: Converting pydantic classes to avro schemas
5
5
  Home-page: https://github.com/godatadriven/pydantic-avro
6
6
  License: MIT
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "pydantic-avro"
3
- version = "0.7.1"
3
+ version = "0.7.3"
4
4
  description = "Converting pydantic classes to avro schemas"
5
5
  authors = ["Peter van 't Hof' <peter.vanthof@godatadriven.com>"]
6
6
 
@@ -20,7 +20,7 @@ pydantic = ">=1.4,<3.0"
20
20
 
21
21
  [tool.poetry.dev-dependencies]
22
22
  coverage = { version = "^7.2.2", extras = ["toml"] }
23
- pytest = "^7.4.4"
23
+ pytest = "^8.3.5"
24
24
  pytest-mock = "^3.10.0"
25
25
  pyproject-flake8 = "^7.0.0"
26
26
  isort = "^5.10.0"
@@ -18,7 +18,7 @@ entry_points = \
18
18
 
19
19
  setup_kwargs = {
20
20
  'name': 'pydantic-avro',
21
- 'version': '0.7.1',
21
+ 'version': '0.7.3',
22
22
  'description': 'Converting pydantic classes to avro schemas',
23
23
  'long_description': '[![Python package](https://github.com/godatadriven/pydantic-avro/actions/workflows/python-package.yml/badge.svg)](https://github.com/godatadriven/pydantic-avro/actions/workflows/python-package.yml)\n[![codecov](https://codecov.io/gh/godatadriven/pydantic-avro/branch/main/graph/badge.svg?token=5L08GOERAW)](https://codecov.io/gh/godatadriven/pydantic-avro)\n[![PyPI version](https://badge.fury.io/py/pydantic-avro.svg)](https://badge.fury.io/py/pydantic-avro)\n[![CodeQL](https://github.com/godatadriven/pydantic-avro/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/godatadriven/pydantic-avro/actions/workflows/codeql-analysis.yml)\n\n# pydantic-avro\n\nThis library can convert a pydantic class to a avro schema or generate python code from a avro schema.\n\n### Install\n\n```bash\npip install pydantic-avro\n```\n\n### Pydantic class to avro schema\n\n```python\nimport json\nfrom typing import Optional\n\nfrom pydantic_avro.base import AvroBase\n\n\nclass TestModel(AvroBase):\n key1: str\n key2: int\n key2: Optional[str]\n\n\nschema_dict: dict = TestModel.avro_schema()\nprint(json.dumps(schema_dict))\n\n```\n\n### Avro schema to pydantic\n\n```shell\n# Print to stdout\npydantic-avro avro_to_pydantic --asvc /path/to/schema.asvc\n\n# Save it to a file\npydantic-avro avro_to_pydantic --asvc /path/to/schema.asvc --output /path/to/output.py\n```\n\n\n### Install for developers\n\n###### Install package\n\n- Requirement: Poetry 1.*\n\n```shell\npoetry install\n```\n\n###### Run unit tests\n```shell\npytest\ncoverage run -m pytest # with coverage\n# or (depends on your local env) \npoetry run pytest\npoetry run coverage run -m pytest # with coverage\n```\n\n##### Run linting\n\nThe linting is checked in the github workflow. To fix and review issues run this:\n```shell\nblack . # Auto fix all issues\nisort . # Auto fix all issues\npflake . # Only display issues, fixing is manual\n```\n',
24
24
  'author': "Peter van 't Hof'",
@@ -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
 
@@ -56,10 +62,19 @@ def logical_type_handler(t: dict) -> str:
56
62
 
57
63
  def enum_type_handler(t: dict) -> str:
58
64
  """Gets the enum type of a given Avro enum type and adds it to the class registry"""
59
- name = t["type"].get("name")
65
+ if t["type"] == "enum":
66
+ # comes from a unioned enum (e.g. ["null", "enum"])
67
+ type_info = t
68
+ else:
69
+ # comes from a direct enum
70
+ type_info = t["type"]
71
+
72
+ name = type_info["name"]
73
+ symbols = type_info["symbols"]
74
+
60
75
  if not ClassRegistry().has_class(name):
61
76
  enum_class = f"class {name}(str, Enum):\n"
62
- for s in t["type"].get("symbols"):
77
+ for s in symbols:
63
78
  enum_class += f' {s} = "{s}"\n'
64
79
  ClassRegistry().add_class(name, enum_class)
65
80
  return name
@@ -120,7 +135,7 @@ def get_pydantic_type(t: Union[str, dict]) -> str:
120
135
  if isinstance(t, str):
121
136
  t = {"type": t}
122
137
 
123
- if isinstance(t["type"], str):
138
+ if isinstance(t.get("type"), str):
124
139
  if ClassRegistry().has_class(t["type"]):
125
140
  return t["type"]
126
141
 
File without changes
File without changes