Skip to main content

A set of pydantic utilities, to extend models on package load

Project description

pydantic-modelable

A set of utilities around pydantic that allows to create extensible pydantic models, with little code, in an aim to have models extended by third-party python code.

Features

Using pydantic for type modelisation and validation has become a very common practice. That being said, some advanced uses are not natively supported, although the pydantic types are extremely flexible, such as dynamic extensibility of the models.

It can be very useful to define extensible models relying on this mechanism, and pydantic_modelable, as it may provide the following benefits:

  • Reduction of code maintenance (defining an "extension" registers it automatically wherever the base was setup)
  • Easy extension of a core library's models and features through the loading of extension modules
  • Automatically updated Model schemas for inclusion in any schema-based tooling or framework (ex: FastAPI's OpenAPI Schema generation tooling)

With a few additional parameters to your model's constructor, inheriting from pydantic_modelable.Modelable, you can thus configure specific behaviors for your extensible model:

  • discriminated union: discriminator=attr_name

You can then register other models into your base model using decorators embedded into your base model by the pydantic_modelable.Modelable class:

  • extends_enum
  • extends_union(dicriminated_union_attr_name: str)
  • as_attribute(attr_name: str, optional: bool, default_factory: Callable[[], BaseModel])

Static typing

Because the models are altered at runtime, a plain type-checker cannot, on its own, see the fields, union members or enum values an extension adds. pydantic-modelable closes most of that gap:

  • Any type-checker. The registration decorators are identity-preserving — decorating a class returns that same class, so its type is never erased. And pydantic_modelable.ModelableStrEnum is a typed base for extensible string enums: inherit it instead of spelling out the aenum bases and subclasses are understood as ordinary enums, with no # type: ignore.

  • mypy. Install the companion pydantic-modelable-mypy distribution and enable its plugin:

    [mypy]
    plugins = pydantic_modelable_mypy.plugin
    

    It teaches mypy about the runtime extensions — as_attribute fields, extends_union discriminated unions, extends_enum enum members — including when they are registered through a ModelableForwarder.

Remaining limitations

  • The plugin is mypy-specific; other checkers (e.g. pyright) see only what the "any type-checker" support above provides.
  • extends_enum member-name access (MyEnum.some_value) resolves on full runs but not on incremental / daemon runs, where a clean run is needed. Enum iteration, membership and construction, and all of as_attribute / extends_union, work in every mode.

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_modelable-0.4.0.tar.gz (48.2 kB view details)

Uploaded Source

Built Distribution

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

pydantic_modelable-0.4.0-py3-none-any.whl (15.3 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_modelable-0.4.0.tar.gz.

File metadata

  • Download URL: pydantic_modelable-0.4.0.tar.gz
  • Upload date:
  • Size: 48.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pydantic_modelable-0.4.0.tar.gz
Algorithm Hash digest
SHA256 60907650d341a01eb06a1f03cb0b989a48027c36ad28290830392c5794c1a066
MD5 a024c6922ef253a9acc95e78dca46cc4
BLAKE2b-256 bd3f62d4d2de65028bca3918d12a61f1503ec3e45bc3ba4768e8579b252e665d

See more details on using hashes here.

Provenance

The following attestation bundles were made for pydantic_modelable-0.4.0.tar.gz:

Publisher: workflow.yml on Joacchim/pydantic-modelable

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pydantic_modelable-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_modelable-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0bd9b98a90e9d4e4700b30eb19fdb0efcbdec32cc6a9638c79da2b6730fea813
MD5 495990fe5fd0dcd33603abcb4911f055
BLAKE2b-256 1e11668194633e6d85c3926f0c3fbe1883844c18d0d72d9a10cca3716d0bb47d

See more details on using hashes here.

Provenance

The following attestation bundles were made for pydantic_modelable-0.4.0-py3-none-any.whl:

Publisher: workflow.yml on Joacchim/pydantic-modelable

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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