An open API service indexing awesome lists of open source software.

https://github.com/zmievsa/type-inspector

Inspect pydantic models without passing data into them
https://github.com/zmievsa/type-inspector

Last synced: about 1 year ago
JSON representation

Inspect pydantic models without passing data into them

Awesome Lists containing this project

README

          

# 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

```bash
pip install type-inspector
```

## Usage

### Quickstart

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

```python
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.