Skip to main content

Pydantic Schemas Generator

https://img.shields.io/pypi/pyversions/pydantic-gen https://img.shields.io/badge/code%20style-black-000000.svg https://img.shields.io/pypi/v/pydantic-gen https://img.shields.io/pypi/dw/pydantic-gen https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Flicht1stein%2Fpydantic-gen%2Fbadge&style=flat https://readthedocs.org/projects/pydantic-gen/badge/?version=latest https://img.shields.io/badge/Support-With_coffee!-Green

What this package does

This is a code generation package that converts YML definitions to Pydantic models (either python code or python objects).

What is Pydantic

Pydantic is a python library for data validation and settings management using python type annotations.

Take a look at the official example from the Pydantic docs.

Why generate schemas?

Normally you just program the schemas within your program, but there are several use cases when code generation makes a lot of sense:

  • You’re programming several apps that use the same schema (think an API server and client library for it)

  • You’re programming in more than one programming language

Getting started

Installation

Using pip:

pip install pydantic-gen

Using poetry:

poetry add pydantic-gen

Usage

First you need to create a YAML file with your desired class schema. See example.yml file.

from pydantic_gen import SchemaGen

generated = SchemaGen('example.yml')

The code is now generated and stored in generated.code attribute. There are two ways to use the code:

  1. Save it to a file, and use the file in your program:

generated.to_file('example_output.py')

You can inspect the resulting file in the example_output.py

  1. Or directly import the generated classed directly without saving:

generated.to_sys(module_name='generated_schemas')

After running generated.to_sys(module_name=’generated_schemas’ your generated code will be available for import:

import generated_schemas as gs

schema = gs.GeneratedSchema1(id=1)

Usage pattern

Recommended usage pattern is creating the yaml files needed for your projects and storing them in a separate repository, to achieve maximum consistency across all projects.

YAML-file structure

schemas - list of all schemas described

name - name of the generated class

props - list of properties of the class using python type annotation. Fields: name - field name, type - field type, optional - bool, if True the type will be wrapped in Optional, default - default value for the field.

config - list of config settings from Model Config of pydantic.

Testing

Project is fully covered by tests and uses pytest. To run:

pytest

Packaging Notice

This project uses the excellent poetry for packaging. Please read about it and let’s all start using pyproject.toml files as a standard. Read more:

Metadata

Release files for pydantic-gen 0.3.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pydantic-gen 0.3.5
File Size Uploaded
pydantic-gen-0.3.5.tar.gz 5.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pydantic-gen 0.3.5
File Interpreter ABI Platform
pydantic_gen-0.3.5-py3-none-any.whl Python 3 none any Details

Total release size: 10.8 kB

Release files / pydantic-gen-0.3.5.tar.gz

Download URL pydantic-gen-0.3.5.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
1de7e7a398bd31e2c31d8d100bfba6f78159563b3f2da488ebf63fe5d4fcd0fe
BLAKE2b-256 checksum
How to use checksums
585874ddd2ab009af02d22e6966c55413f26cd2e11060f8675997beb8d3bcf54
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.5 CPython/3.7.7 Linux/5.3.0-1022-azure

Release files / pydantic_gen-0.3.5-py3-none-any.whl

Download URL pydantic_gen-0.3.5-py3-none-any.whl
Size 5.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8bf52be955881a14749102b28a77a5dc23014d942d25ec4a62d60d518cef7195
BLAKE2b-256 checksum
How to use checksums
089d6dd28d731c592f8cfe89f8d131cd1e8d86c0339ee9bee402201a6009acf9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.5 CPython/3.7.7 Linux/5.3.0-1022-azure

Release history Release notifications | RSS feed

This release

0.3.5 This release

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.8

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page