TypedDict
Use TypedDict replace pydantic definitions.
Why?
from pydantic import BaseModel
class User(BaseModel):
name: str
age: int = Field(default=0, ge=0)
email: Optional[str]
user: User = {"name": "John", "age": 30} # Type check, error!
print(repr(user))
In index.py or other framework, maybe you write the following code. And then got an type check error in Annotated[Message, ...], because the type of {"message": "..."} is not Message.
class Message(BaseModel):
message: str
@routes.http.post("/user")
async def create_user(
...
) -> Annotated[Message, JSONResponse[200, {}, Message]]:
...
return {"message": "Created successfully!"}
Usage
Use Annotated to provide extra information to pydantic.Field. Other than that, everything conforms to the general usage of TypedDict. Using to_pydantic will create a semantically equivalent pydantic model. You can use it in frameworks like index.py / fastapi / xpresso.
from typing_extensions import Annotated, NotRequired, TypedDict
import typeddict
from typeddict import Extra, Metadata
class User(TypedDict):
name: str
age: Annotated[int, Metadata(default=0), Extra(ge=0)]
email: NotRequired[Annotated[str, Extra(min_length=5, max_length=100)]]
class Book(TypedDict):
author: NotRequired[User]
user: User = {"name": "John", "age": 30} # Type check, pass!
print(repr(user))
# Then use it in fastapi / index.py or other frameworks
UserModel = typeddict.to_pydantic(User)
print(repr(UserModel.__signature__))
print(repr(UserModel.parse_obj(user)))
book: Book = {"author": user} # Type check, pass!
print(repr(book))
# Then use it in fastapi / index.py or other frameworks
BookModel = typeddict.to_pydantic(Book)
print(repr(BookModel.__signature__))
print(repr(BookModel.parse_obj(book)))
cast
Sometimes you may not need a pydantic model, you can directly use typeddict to parse the data.
import typeddict
class User(TypedDict):
name: str
age: Annotated[int, Metadata(default=0), Extra(ge=0)]
email: NotRequired[Annotated[str, Extra(min_length=5, max_length=100)]]
user = typeddict.cast(User, {"name": "John", "age": 30, "unused-info": "....."})
print(repr(user))
Metadata
Release files for typeddict 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| typeddict-0.3.0.tar.gz | 7.9 kB | Without provenance | Details for typeddict-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| typeddict-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for typeddict-0.3.0-py3-none-any.whl |
Total release size: 16.4 kB
Release files / typeddict-0.3.0.tar.gz
| Download URL | typeddict-0.3.0.tar.gz |
|---|---|
| Size | 7.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
41c10cf137a1df4d287aa147b1719f97ee465e46ec900ad483466c7c1b0692ed
|
|
BLAKE2b-256 checksum How to use checksums |
ed92f941620967dd4ae6db22b303c34d06f19a258f01fa6b1b513f6e62f82f8e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.3.2 CPython/3.7.15 Linux/5.15.0-1030-azure
|
Release files / typeddict-0.3.0-py3-none-any.whl
| Download URL | typeddict-0.3.0-py3-none-any.whl |
|---|---|
| Size | 8.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e979ebf7455a7e95b82479df1e2eba00699e2566a68fef92cb5c2cebca663f27
|
|
BLAKE2b-256 checksum How to use checksums |
2f10f5de15669213240596a80d691439ca94dc49223abb9b7b52f1a3a43c5a18
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.3.2 CPython/3.7.15 Linux/5.15.0-1030-azure
|
Security for typeddict 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.