Compiles Python type hints into strict, inspectable schemas
Project description
pytypehint
pytypehint compiles Python type hints into strict, inspectable schemas. A
hint carries everything there is to know about a field — its type, its limits,
and its presentation — so the dataclass is the single source of truth: the
core validates plain input data, fills fresh defaults and constructs dataclass
instances; wrapper authors read the same schema to render controls, coerce
external input and execute functions themselves. What the core hands them is
raw, inspectable structure, never an opinion about it: interpretive
conveniences belong to wrappers and to intermediate packages built on the core.
Stdlib only; Python 3.11+; py.typed included.
pip install pytypehint
from dataclasses import dataclass, field
from typing import Annotated
from pytypehint import Label, Max, Min, signature_of, struct_of
@dataclass(frozen=True)
class Page:
number: Annotated[int, Min(1)] = 1
size: Annotated[int, Min(1), Max(100), Label("Page size")] = 20
@dataclass
class Search:
query: str
page: Page = Page()
tags: list[str] = field(default_factory=list)
value = struct_of(Search).build({"query": "python", "page": {"size": 50}})
# Search(query='python', page=Page(number=1, size=50), tags=[])
try:
struct_of(Search).build({"query": "python", "page": {"size": 500}})
except ValueError as error:
assert str(error) == "page: size: too large: 500, maximum 100"
def search(query: str, page: Page = Page()): ...
kwargs = signature_of(search).build({"query": "python"})
search(**kwargs) # execution belongs to the caller
Guarantees
- Exact types:
type(value) is T; the core never coerces. - Data enters as dictionaries and lists; dataclass instances leave through
build. - Defaults are certified at compilation and rematerialized fresh per missing key.
- Invalid constraints fail while compiling the schema.
- Errors retain the complete field and list-index path, as the message text and
as data:
SchemaTypeErrorandSchemaValueErrorcarrypathandleaf, and subclassTypeErrorandValueError. - Notation atoms are stored and cross-checked but never affect validation; presentation belongs to the wrapper.
Struct,FieldandSignaturecompare by identity; compile once and share.buildvalidates the input once and constructs directly; the cost is linear.resolvevalidates and fills defaults without constructing nested dictionaries.Signature.buildreturns constructed keyword arguments and never invokes the function.
Vocabulary
| Hint | Shape | Input |
|---|---|---|
int |
Int |
exact int |
float |
Float |
exact float |
str |
Str |
exact str |
bool |
Bool |
exact bool |
date |
Date |
exact datetime.date |
time |
Time |
exact naive datetime.time |
Enum subclass |
EnumShape |
exact member type |
None |
NoneShape |
None |
list[X] |
List |
list; nesting and union items supported |
| dataclass | Struct |
dictionary; build constructs it |
A | B |
tuple of shapes | exact scalar type or routed dataclass dictionary |
Literal[...] |
Int or Str with Choices |
homogeneous int or str literals |
Public API
Everything public is exported from pytypehint:
struct_of,signature_of;Struct,Field,Signature;- errors:
SchemaTypeError,SchemaValueError; Shape,Int,Float,Str,Bool,Date,Time,List,NoneShape,EnumShape;- limits:
Min,Max,Choices,MultipleOf,Pattern,IsPathFile; - notation:
Label,Description,Placeholder,Step,Slider,IsPassword,Rows,Extra,OptionalToggle; MISSING.
Start with the design principles, then read build, resolve, defaults, vocabulary, atoms, restrictions, and comparison.
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 pytypehint-0.0.1.tar.gz.
File metadata
- Download URL: pytypehint-0.0.1.tar.gz
- Upload date:
- Size: 96.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
850ef51a1a7b43cb92ff9c482451d8669be4ffb41770dbb0cc7762370c7087b5
|
|
| MD5 |
cea7c9434f754cdc870f3571dc6d6728
|
|
| BLAKE2b-256 |
f798196e6f4034d60b71b5dfab2b660bb16c78894af63b0054d970f387b04ee8
|
Provenance
The following attestation bundles were made for pytypehint-0.0.1.tar.gz:
Publisher:
publish.yml on offerrall/pytypehint
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytypehint-0.0.1.tar.gz -
Subject digest:
850ef51a1a7b43cb92ff9c482451d8669be4ffb41770dbb0cc7762370c7087b5 - Sigstore transparency entry: 2173048017
- Sigstore integration time:
-
Permalink:
offerrall/pytypehint@8c581ec678ac3647603ebcd2069accf4c3b65ad4 -
Branch / Tag:
refs/tags/v0.0.1 - Owner: https://github.com/offerrall
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@8c581ec678ac3647603ebcd2069accf4c3b65ad4 -
Trigger Event:
release
-
Statement type:
File details
Details for the file pytypehint-0.0.1-py3-none-any.whl.
File metadata
- Download URL: pytypehint-0.0.1-py3-none-any.whl
- Upload date:
- Size: 19.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4fcbf448d9cca2997fb8d8f85488e1ff6ce255c3c0aecabda19ca7e7c2489771
|
|
| MD5 |
359633671715683bcd3bec6753c71a16
|
|
| BLAKE2b-256 |
0ed513dd27eb9d78c8dedc6033b898acc1e854f9b5ac82d1082ad771b6198772
|
Provenance
The following attestation bundles were made for pytypehint-0.0.1-py3-none-any.whl:
Publisher:
publish.yml on offerrall/pytypehint
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytypehint-0.0.1-py3-none-any.whl -
Subject digest:
4fcbf448d9cca2997fb8d8f85488e1ff6ce255c3c0aecabda19ca7e7c2489771 - Sigstore transparency entry: 2173048038
- Sigstore integration time:
-
Permalink:
offerrall/pytypehint@8c581ec678ac3647603ebcd2069accf4c3b65ad4 -
Branch / Tag:
refs/tags/v0.0.1 - Owner: https://github.com/offerrall
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@8c581ec678ac3647603ebcd2069accf4c3b65ad4 -
Trigger Event:
release
-
Statement type: