Skip to main content

A library for converting avro schemas to python types.

Project description

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 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

class ExampleAddressUSRecord(TypedDict):
    streetaddress: str
    city: str


class ExampleOtherThing(TypedDict):
    thing1: str
    thing2: Optional[int]


class ExampleUser(TypedDict):
    name: str
    favorite_number: Optional[int]
    favorite_color: Optional[str]
    address: AddressUSRecord
    other_thing: OtherThing

Testing

To run unit tests, run poetry run pytest.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

avro-to-python-types-0.3.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

avro_to_python_types-0.3.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file avro-to-python-types-0.3.0.tar.gz.

File metadata

  • Download URL: avro-to-python-types-0.3.0.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.3 CPython/2.7.16 Darwin/19.6.0

File hashes

Hashes for avro-to-python-types-0.3.0.tar.gz
Algorithm Hash digest
SHA256 0ce1286b9357f65d9c8d10f0c211c595868e0bf431c8135d6570963136b1f73b
MD5 bd09added62086eea63513ac62230c25
BLAKE2b-256 3d625e016273c87880b7038249f39e66fce0fa6f587f4941ed279a2f7d7f1de9

See more details on using hashes here.

File details

Details for the file avro_to_python_types-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for avro_to_python_types-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 42a106324a4b2a7a33fd15891ef485bec8512e31af7f833786a08cb31e22cc80
MD5 c7f006b3be88a695c8189a7e9c20f2c1
BLAKE2b-256 c051502cf961b5d1bef9e30f0fa175a0d1565ee3ee52e77ff266bb3f6aa962d3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page