avro-to-python-types
A library for converting avro schemas to python types.
Currently, it supports converting records to TypedDict. If you would like to see more features added, please open up an issue.
Why would I want this?
This library is targeted to people writing code generation for python apps that are using avro.
Usage
This library does one thing, it converts Avro schemas to python types.
To get up and running quickly, you can use this to simply load schemas and print out the python code that is generated.
import glob
from avro_to_python_types import typed_dict_from_schema_file
schema_files = glob.glob("schemas/*.avsc")
for schema_file in schema_files:
types = typed_dict_from_schema_file(schema_file)
print(types)
For a real world example of syncing a directory of schemas into a directory of matching python typed dictionaries check out the example app here
To try it out, simply clone this repo and run:
poetry env use 3.9
- This must be done as this library only supports Python 3.9 and above for type generation. You can still use this library in apps that use a lower Python version, as long as Python 3.9 is the active version when the types are generated (either locally or in your CI system).
poetry install
poetry run sync-example
For some more advanced examples, like referencing other schema files by their full name take a look at the tests here
Referencing schemas
This library supports referencing schemas in different files by their fullname.
In order for this behaviour to work, all schemas must be in the same directory and use the following naming convention: namespace.name.avsc. Note that is the same as fullname.avsc
For more on this checkout the docs for fastavro here.
An example of this can be found in the tests.
Example output
The following example shows the type generated for a given schema.
{
"namespace": "example",
"type": "record",
"name": "User",
"fields": [
{ "name": "name", "type": "string" },
{ "name": "favorite_number", "type": ["null", "int"] },
{ "name": "favorite_color", "type": ["null", "string"] },
{
"name": "address",
"type": {
"type": "record",
"name": "AddressUSRecord",
"fields": [
{ "name": "streetaddress", "type": "string" },
{ "name": "city", "type": "string" }
]
}
},
{
"name": "other_thing",
"type": {
"type": "record",
"name": "OtherThing",
"fields": [
{ "name": "thing1", "type": "string" },
{ "name": "thing2", "type": ["null", "int"] }
]
}
}
]
}
from typing import TypedDict, Optional
# total=False allows us to skip passing optional fields into the constructor
class ExampleAddressUSRecord(TypedDict, total=False):
streetaddress: str
city: str
class ExampleOtherThing(TypedDict, total=False):
thing1: str
thing2: Optional[int]
class ExampleUser(TypedDict, total=False):
name: str
favorite_number: Optional[int]
favorite_color: Optional[str]
address: AddressUSRecord
other_thing: OtherThing
Testing
To run unit tests, run poetry run pytest.
You can also run tests in docker via make test
Requirements
Python 3.9 or greater.
Metadata
Release files for avro-to-python-types 0.12.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| avro-to-python-types-0.12.1.tar.gz | 9.7 kB | Without provenance | Details for avro-to-python-types-0.12.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| avro_to_python_types-0.12.1-py3-none-any.whl | Python 3 | none | any | Details for avro_to_python_types-0.12.1-py3-none-any.whl |
Total release size: 19.3 kB
Release files / avro-to-python-types-0.12.1.tar.gz
| Download URL | avro-to-python-types-0.12.1.tar.gz |
|---|---|
| Size | 9.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
54f58ecb8b98b7f16fbfc4b538fe344b1041c7bbb33c39940d29a51badc9b376
|
|
BLAKE2b-256 checksum How to use checksums |
3f2a21e735b43d072d60c53ae7b4b8eafb24b20e72cb26d1577aa8e80d4f381b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.12 CPython/3.9.9 Linux/5.11.0-1025-azure
|
Release files / avro_to_python_types-0.12.1-py3-none-any.whl
| Download URL | avro_to_python_types-0.12.1-py3-none-any.whl |
|---|---|
| Size | 9.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d2291da4c90b352314b3e38babc25f5ffe3ec4f9e76fb4f33999b0da8348eac5
|
|
BLAKE2b-256 checksum How to use checksums |
2ab9781285e01caa06c4d2429ee5ec55f7b60da413a5aa34713f2f7c89c2f876
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.12 CPython/3.9.9 Linux/5.11.0-1025-azure
|
Security for avro-to-python-types 0.12.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.