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
- Host: GitHub
- URL: https://github.com/zmievsa/type-inspector
- Owner: zmievsa
- License: mit
- Created: 2023-02-04T22:45:04.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-10-28T15:55:11.000Z (over 2 years ago)
- Last Synced: 2025-02-07T15:45:24.456Z (over 1 year ago)
- Language: Python
- Homepage:
- Size: 36.1 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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.
---
## 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.