Pydantic BaseModel supercharged with helper methods for generic and annotation support
Project description
Super Model for Pydantic
A lightweight extension of Pydantic's BaseModel that adds generic type introspection and retrieval of fields annotated with typing.Annotated.
Installation
Install with pip
pip install pydantic-super-model
Features
- Generic support
- Able to retrieve field(s) with a specific Annotation
Generic Example
from pydantic_super_model import SuperModel
class UserWithType[T](SuperModel):
"""User model with a generic type."""
id: T
name: str
user = UserWithType[int](id=1, name="John Doe")
user_type = user.get_type() # int
Annotation Example
from typing import Annotated
from pydantic_super_model import SuperModel
class _PrimaryKeyAnnotation:
pass
PrimaryKey = Annotated[int, _PrimaryKeyAnnotation]
class UserWithAnnotation(SuperModel):
"""User model with an Annotation for a field."""
id: PrimaryKey
name: str
user = UserWithAnnotation(id=1, name="John Doe")
annotations = user.get_annotated_fields(PrimaryKey)
# {"id": 1}
Documentation
Get Annotated Fields
SuperModel.get_annotated_fields(*annotations) returns a dict of field names to values for fields whose type hints carry any of the provided annotations (either the Annotated[...] alias or the meta annotation type).
It includes falsy values (like 0 or an empty string) and includes None only when the field was explicitly provided. Unset default None values are omitted.
from typing import Annotated
from pydantic_super_model import SuperModel
class _PrimaryKey:
pass
PrimaryKey = Annotated[int, _PrimaryKey]
class User(SuperModel):
id: PrimaryKey
name: str
u = User(id=1, name="Jane")
assert u.get_annotated_fields(PrimaryKey) == {"id": 1}
Explicit None vs. unset default:
class UserOptional(SuperModel):
id: PrimaryKey | None = None
name: str
# Unset default None is omitted
assert not UserOptional(name="A").get_annotated_fields(PrimaryKey)
# Explicit None is included
assert UserOptional(id=None, name="B").get_annotated_fields(PrimaryKey) == {"id": None}
FieldNotImplemented Annotation
You can use the FieldNotImplemented annotation to mark fields that should not be set. An example
use case are experimental fields that you intend on implementing later.
from typing import Annotated
from pydantic_super_model import SuperModel, FieldNotImplemented
class Experimental(SuperModel):
test_field: Annotated[int, FieldNotImplemented]
name: str
# Raises NotImplementedError because the field is provided
Experimental(test_field=1, name="x")
# Optional + unset default is allowed
class ExperimentalOptional(SuperModel):
test_field: Annotated[int | None, FieldNotImplemented] = None
name: str
ExperimentalOptional(name="ok") # ok (field is unset)
Generics
Use get_type() to retrieve the concrete generic type parameter supplied at instantiation time.
from pydantic_super_model import SuperModel
class UserWithType[T](SuperModel):
id: T
name: str
u = UserWithType[int](id=1, name="Charlie")
assert u.get_type() is int
Run Tests
- Install with the
devextra:pip install pydantic-super-model[dev] - Run tests with
pytest .
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pydantic_super_model-1.1.2.tar.gz.
File metadata
- Download URL: pydantic_super_model-1.1.2.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2046135567b980394c579609003de93ed4693a0034d5134e1965deb4f77a28f9
|
|
| MD5 |
dc00b408ccb45672f9242e801a14e614
|
|
| BLAKE2b-256 |
a0241e89341f643a1ae731cd34a0a9368642fbb9958984dfefe4a0aeb090b75f
|
Provenance
The following attestation bundles were made for pydantic_super_model-1.1.2.tar.gz:
Publisher:
publish-to-pypi.yml on julien777z/pydantic-super-model
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pydantic_super_model-1.1.2.tar.gz -
Subject digest:
2046135567b980394c579609003de93ed4693a0034d5134e1965deb4f77a28f9 - Sigstore transparency entry: 598088958
- Sigstore integration time:
-
Permalink:
julien777z/pydantic-super-model@236f79fa005b1b74e57bcb76d8586c6912248133 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/julien777z
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@236f79fa005b1b74e57bcb76d8586c6912248133 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file pydantic_super_model-1.1.2-py3-none-any.whl.
File metadata
- Download URL: pydantic_super_model-1.1.2-py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2b1a0518b0cddfd02a245279d870b33f727a5e1d88de65b734e0351676435902
|
|
| MD5 |
bf75ea27baaad450ff822528de2bfe59
|
|
| BLAKE2b-256 |
a19a2784bca7efbad5e66fbb0f94e1c821e747f14d6592f71128fb58053049d3
|
Provenance
The following attestation bundles were made for pydantic_super_model-1.1.2-py3-none-any.whl:
Publisher:
publish-to-pypi.yml on julien777z/pydantic-super-model
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pydantic_super_model-1.1.2-py3-none-any.whl -
Subject digest:
2b1a0518b0cddfd02a245279d870b33f727a5e1d88de65b734e0351676435902 - Sigstore transparency entry: 598088962
- Sigstore integration time:
-
Permalink:
julien777z/pydantic-super-model@236f79fa005b1b74e57bcb76d8586c6912248133 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/julien777z
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@236f79fa005b1b74e57bcb76d8586c6912248133 -
Trigger Event:
workflow_dispatch
-
Statement type: