Metadata-Version: 2.5
Name: extendable_pydantic
Version: 1.5.0
Summary: A lib to define pydantic models extendable at runtime.
Project-URL: Source, https://github.com/lmignon/extendable-pydantic
Author-email: Laurent Mignon <laurent.mignon@acsone.eu>
License: MIT License
        
        Copyright (c) 2021 Laurent Mignon (ACSONE)
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.10
Requires-Dist: extendable>=1.4.0.dev0
Requires-Dist: pydantic>=2.9
Description-Content-Type: text/markdown

[![CI](https://github.com/lmignon/pydantic-ext/actions/workflows/ci.yml/badge.svg)](https://github.com/lmignon/pydantic-ext/actions/workflows/ci.yml)
[![codecov](https://codecov.io/gh/lmignon/pydantic-ext/branch/master/graph/badge.svg?token=Z9FWM57T14)](https://codecov.io/gh/lmignon/pydantic-ext)

# Extendable Pydantic

This addons provides a new type used to declare [Pydantic](https://pypi.org/project/pydantic/)
model as [Extendable](https://pypi.org/project/extendable/) class.

From release 1.0.0 it only supports Pydantic >= 2.0.0.


```python
from pydantic import BaseModel
from extendable_pydantic import ExtendableModelMeta
from extendable import context, registry

class Location(BaseModel, metaclass=ExtendableModelMeta):
    lat = 0.1
    lng = 10.1

class ExtendedLocation(Location, extends=Location):
    name: str

_registry = registry.ExtendableClassesRegistry()
context.extendable_registry.set(_registry)
_registry.init_registry()

loc = Location(**{"lat": 12.3, "lng": 13.2, "name": "My Loc"})

loc.model_dump() == {"lat": 12.3, "lng": 13.2, "name": "My Loc"}
#> True

loc.model_json_schema()
#> {'title': 'Location', 'type': 'object', 'properties': {'lat': {'title': 'Lat', 'default': 0.1, 'type': 'number'}, 'lng': {'title': 'Lng', 'default': 10.1, 'type': 'number'}, 'name': {'title': 'Name', 'type': 'string'}}, 'required': ['name']}
```

## Usage with FastAPI

Extendable models can be used as request and response models or as dependencies
of FastAPI routes. Any reference to an extendable model is resolved to the final
class of the current registry when pydantic builds the schema. Therefore, the
routes must be added to the application (`app.include_router(...)`) once the
registry is initialized.

Requires FastAPI >= 0.138.

## Development

`pip install -e .`

## Release

The version of the package is computed from the git tags (hatch-vcs) and the
changelog is built from the news fragments (towncrier).

* Each change worth mentioning into the changelog comes with a news fragment
  into the `news` directory: `news/<issue number>.<type>`, or
  `news/+<name>.<type>` for a change without issue, where `<type>` is one of
  `feature`, `bugfix`, `doc`, `removal` or `misc`.
* To release a new version, run the `release` workflow from the GitHub "Actions"
  tab and choose the part of the version to increase (`patch`, `minor` or
  `major`). Once the run is approved (required reviewers of the
  `release-approval` environment), the workflow updates `HISTORY.rst`, tags the
  release, publishes the package to PyPI and creates the GitHub release.
