pydantic-avro 0.9.1__tar.gz → 0.9.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.
- {pydantic_avro-0.9.1 → pydantic_avro-0.9.3}/PKG-INFO +1 -1
- {pydantic_avro-0.9.1 → pydantic_avro-0.9.3}/pyproject.toml +1 -1
- {pydantic_avro-0.9.1 → pydantic_avro-0.9.3}/setup.py +1 -1
- {pydantic_avro-0.9.1 → pydantic_avro-0.9.3}/src/pydantic_avro/from_avro/types.py +1 -1
- {pydantic_avro-0.9.1 → pydantic_avro-0.9.3}/src/pydantic_avro/to_avro/base.py +19 -3
- {pydantic_avro-0.9.1 → pydantic_avro-0.9.3}/LICENSE +0 -0
- {pydantic_avro-0.9.1 → pydantic_avro-0.9.3}/README.md +0 -0
- {pydantic_avro-0.9.1 → pydantic_avro-0.9.3}/src/pydantic_avro/__init__.py +0 -0
- {pydantic_avro-0.9.1 → pydantic_avro-0.9.3}/src/pydantic_avro/__main__.py +0 -0
- {pydantic_avro-0.9.1 → pydantic_avro-0.9.3}/src/pydantic_avro/base.py +0 -0
- {pydantic_avro-0.9.1 → pydantic_avro-0.9.3}/src/pydantic_avro/from_avro/__init__.py +0 -0
- {pydantic_avro-0.9.1 → pydantic_avro-0.9.3}/src/pydantic_avro/from_avro/avro_to_pydantic.py +0 -0
- {pydantic_avro-0.9.1 → pydantic_avro-0.9.3}/src/pydantic_avro/from_avro/class_registery.py +0 -0
- {pydantic_avro-0.9.1 → pydantic_avro-0.9.3}/src/pydantic_avro/py.typed +0 -0
- {pydantic_avro-0.9.1 → pydantic_avro-0.9.3}/src/pydantic_avro/to_avro/__init__.py +0 -0
- {pydantic_avro-0.9.1 → pydantic_avro-0.9.3}/src/pydantic_avro/to_avro/config.py +0 -0
- {pydantic_avro-0.9.1 → pydantic_avro-0.9.3}/src/pydantic_avro/to_avro/types.py +0 -0
|
@@ -18,7 +18,7 @@ entry_points = \
|
|
|
18
18
|
|
|
19
19
|
setup_kwargs = {
|
|
20
20
|
'name': 'pydantic-avro',
|
|
21
|
-
'version': '0.9.
|
|
21
|
+
'version': '0.9.3',
|
|
22
22
|
'description': 'Converting pydantic classes to avro schemas',
|
|
23
23
|
'long_description': '[](https://github.com/godatadriven/pydantic-avro/actions/workflows/python-package.yml)\n[](https://codecov.io/gh/godatadriven/pydantic-avro)\n[](https://badge.fury.io/py/pydantic-avro)\n[](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### Specify expected Avro type\n\n```python\nfrom datetime import datetime\nfrom pydantic import Field\nfrom pydantic_avro.base import AvroBase \n\nclass ExampleModel(AvroBase):\n field1: int = Field(..., avro_type="long") # Explicitly set Avro type to "long"\n field2: datetime = Field(..., avro_type="timestamp-millis") # Explicitly set Avro type to "timestamp-millis"\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'",
|
|
@@ -132,7 +132,7 @@ def generate_field_string(field: dict) -> str:
|
|
|
132
132
|
|
|
133
133
|
def get_pydantic_type(t: Union[str, dict]) -> str:
|
|
134
134
|
"""Get the Pydantic type for a given Avro type"""
|
|
135
|
-
if isinstance(t, str):
|
|
135
|
+
if isinstance(t, str) or isinstance(t, list):
|
|
136
136
|
t = {"type": t}
|
|
137
137
|
|
|
138
138
|
if isinstance(t.get("type"), str):
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
from typing import Optional
|
|
1
|
+
from typing import Literal, Optional
|
|
2
2
|
|
|
3
3
|
from pydantic import BaseModel
|
|
4
4
|
|
|
@@ -10,14 +10,30 @@ class AvroBase(BaseModel):
|
|
|
10
10
|
"""This class provides functionality to convert a pydantic model to an Avro schema."""
|
|
11
11
|
|
|
12
12
|
@classmethod
|
|
13
|
-
def avro_schema(
|
|
13
|
+
def avro_schema(
|
|
14
|
+
cls,
|
|
15
|
+
by_alias: bool = True,
|
|
16
|
+
namespace: Optional[str] = None,
|
|
17
|
+
mode: Literal["validation", "serialization"] = "serialization",
|
|
18
|
+
) -> dict:
|
|
14
19
|
"""Returns the avro schema for the pydantic class
|
|
15
20
|
|
|
16
21
|
:param by_alias: generate the schemas using the aliases defined, if any
|
|
17
22
|
:param namespace: Provide an optional namespace string to use in schema generation
|
|
23
|
+
:param mode: The mode for generating the schema. Use 'validation' for input validation
|
|
24
|
+
or 'serialization' for output serialization. Defaults to 'serialization'.
|
|
25
|
+
Only applicable for Pydantic v2.
|
|
18
26
|
:return: dict with the Avro Schema for the model
|
|
19
27
|
"""
|
|
20
|
-
|
|
28
|
+
if PYDANTIC_V2:
|
|
29
|
+
schema = cls.model_json_schema(by_alias=by_alias, mode=mode)
|
|
30
|
+
else:
|
|
31
|
+
if mode != "serialization":
|
|
32
|
+
raise ValueError(
|
|
33
|
+
f"The 'mode' parameter is only supported in Pydantic v2. "
|
|
34
|
+
f"Pydantic v1 does not support different schema modes."
|
|
35
|
+
)
|
|
36
|
+
schema = cls.schema(by_alias=by_alias)
|
|
21
37
|
|
|
22
38
|
if namespace is None:
|
|
23
39
|
# Default namespace will be based on title
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|