This addons provides a new type used to declare Pydantic model as Extendable class.
From release 1.0.0 it only supports Pydantic >= 2.0.0.
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']}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.
pip install -e .
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
newsdirectory:news/<issue number>.<type>, ornews/+<name>.<type>for a change without issue, where<type>is one offeature,bugfix,doc,removalormisc. - To release a new version, run the
releaseworkflow from the GitHub "Actions" tab and choose the part of the version to increase (patch,minorormajor). The workflow prepares the release (HISTORY.rst, tag, package) and shows the release notes into the summary of the run. Once the release is approved (required reviewers of thereleaseenvironment), the workflow pushes the changelog and the tag, publishes the package to PyPI and creates the GitHub release. Nothing is pushed nor published before the approval.