Skip to content

Inspect pydantic models without passing data into them

License

Notifications You must be signed in to change notification settings

zmievsa/type-inspector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

type-inspector

Walk a deeply nested pydantic model, type alias or dataclass as if data was already put into it. While pydantic allows you to validate data with a model, type-inspector allows you to validate operations on data using a model before any data has been passed.


Test Coverage PyPI Supported Python versions

Installation

pip install type-inspector

Usage

Quickstart

Let's pick an inspector for a model and try doing a few operations on it:

from type_inspector import pick_inspector
from pydantic import BaseModel

class NestedModel(BaseModel):
    b: dict[str, int | list[bytes]]

class MyModel(BaseModel):
    a: list[NestedModel]

inspector = pick_inspector(MyModel, ["MyModel"])
result = inspector.a[1].b["hello"][11]
print(result.wrapped) # bytes

If you try to access a property or item that cannot exist, an InspectionError will be raised.

Note on notation

This library was originally created to mimic javascript syntax so Model["a"]["b"] is the same as Model.a.b. However, this notation is not necessarily correct in terms of python syntax. But it will definitely make sure that the operations you write will work on a JSON object that the model was created for.

About

Inspect pydantic models without passing data into them

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published