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.0.tar.gz (54.1 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.0-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pydantic_fabricated-1.0.0.tar.gz
Algorithm Hash digest
SHA256 990303cb3d636fe4a8711c64564fe35b570df747ed7528db5acb2083adb49ab2
MD5 683426439120db9e63b681513d283ac8
BLAKE2b-256 ea37145bf8af4bf2d12540b2164bae7ade67876a2b980cc5bb7acd4ddb0c8a3f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pydantic_fabricated-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 36fc973236c21a49d89f6127161cde23e8f227be1f973a32d5160d3ee109195b
MD5 82214bd70e5ca24653de3ef9309f600b
BLAKE2b-256 edddc322f78ca30f5c36107783a5f5514cfcc66a8c5e15577f62f33030eb7bf2

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