Skip to main content

Metaclass for fabricating subclasses from dictionaries inside Pydantic models.

Project description

Pydantic Fabricated

A Python package that provides a metaclass for fabricating subclasses from dictionaries inside Pydantic models. This library seamlessly integrates with Pydantic's type system to enable dynamic object creation based on type definitions.

Installation

pip install pydantic-fabricated

Features

  • Dynamic object creation from type definitions
  • Seamless integration with Pydantic models
  • Type-safe parameter validation
  • Support for JSON serialization/deserialization
  • Discriminated unions for type-safe polymorphism

Quick Start

Here's a simple example of how to use Pydantic Fabricated:

import os

from pydantic_settings import BaseSettings, SettingsConfigDict

from pydantic_fabricated import PydanticFabricated


# Define a base class using the metaclass
class Shape(metaclass=PydanticFabricated):
    pass


# Define concrete implementations
class Circle(Shape):
    def __init__(self, radius: float):
        self.radius = radius


class Rectangle(Shape):
    def __init__(self, width: float, height: float):
        self.width = width
        self.height = height


# Create instances using the fabricate_from_type method
circle = Shape.fabricate_from_type('Circle', {'radius': 5.0})
rectangle = Shape.fabricate_from_type('Rectangle', {'width': 10.0, 'height': 20.0})


# Or use with Pydantic Settings
class Settings(BaseSettings):
    model_config = SettingsConfigDict(env_prefix='my_prefix_')

    shape: Shape


os.environ['MY_PREFIX_SHAPE'] = r'{"type": "Circle", "radius": 5.0}'
settings = Settings()

Requirements

  • Python ≥ 3.13
  • Pydantic ≥ 2.11.9

License

Apache License 2.0

Links

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Additional Documentation

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

pydantic_fabricated-1.0.1.tar.gz (54.4 kB view details)

Uploaded Source

Built Distribution

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

pydantic_fabricated-1.0.1-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_fabricated-1.0.1.tar.gz.

File metadata

  • Download URL: pydantic_fabricated-1.0.1.tar.gz
  • Upload date:
  • Size: 54.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.19

File hashes

Hashes for pydantic_fabricated-1.0.1.tar.gz
Algorithm Hash digest
SHA256 5e6a56f7f0adadc20f78e1cc6e992b8d597d88a881f1a70bf47040ad0d06cc5e
MD5 9ea0c65810777f5df902c5910d9495f8
BLAKE2b-256 db745eab73f4f571d91a7d14ba98ace22e69e917b798b806e9d8bc840052161d

See more details on using hashes here.

File details

Details for the file pydantic_fabricated-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_fabricated-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 28ccf1c9f58a339db7df4cd736a66c2fe0c24a79df6c31b148c753a570067b57
MD5 257e164051c92f51d1a0dba3712197b4
BLAKE2b-256 608a7a24dd494752bb97b327ae6713bface352e49aa9b0b27617602002842e03

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