Skip to content

Commit 9ba7ebc

Browse files
authored
Merge pull request #3 from credativ/model
Add the ability to obtain `pydantic`-modelled data
2 parents e98923f + fe52952 commit 9ba7ebc

File tree

11 files changed

+101884
-16920
lines changed

11 files changed

+101884
-16920
lines changed

README.md

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Type annotations for data obtained by `proxmoxer.ProxmoxAPI` calls.
44

55
## Usage
66

7+
### Annotations only
8+
79
```python
810
import typing
911
import proxmoxer
@@ -43,10 +45,67 @@ typing.reveal_type(api.cluster.replication("some-id").get())
4345
```
4446

4547
```
46-
legacy.py:10: note: Revealed type is "builtins.dict[Any, Any]"
48+
legacy.py:10: note: Revealed type is "builtins.dict[builtins.str, Any]"
4749
Success: no issues found in 1 source file
4850
```
4951

52+
#### Dependencies
53+
54+
- For type checking: `proxmoxer-stubs`, `pydantic`
55+
- At runtime: None
56+
57+
### Wrapper mode
58+
59+
Example from [proxmoxer](https://github.com/proxmoxer/proxmoxer):
60+
61+
```
62+
from proxmoxer import ProxmoxAPI
63+
64+
proxmox = ProxmoxAPI(
65+
"proxmox_host", user="admin@pam", password="secret_word", verify_ssl=False
66+
)
67+
68+
for node in proxmox.nodes.get():
69+
for vm in proxmox.nodes(node["node"]).qemu.get():
70+
print(f"{vm['vmid']}. {vm['name']} => {vm['status']}")
71+
```
72+
73+
The above works the same in wrapper mode:
74+
75+
```
76+
from proxmoxer_types.v9 import ProxmoxAPI
77+
78+
proxmox = ProxmoxAPI(
79+
"proxmox_host", user="admin@pam", password="secret_word", verify_ssl=False
80+
)
81+
82+
for node in proxmox.nodes.get():
83+
for vm in proxmox.nodes(node["node"]).qemu.get():
84+
print(f"{vm['vmid']}. {vm['name']} => {vm['status']}")
85+
```
86+
87+
The returned objects in both above cases are built-in types, possibly nested in
88+
`list` or `dict`. Working with those may be inconvenient, as optional
89+
`dict` keys may not exist at all. For convenience the following is possible:
90+
91+
```
92+
for node in proxmox.nodes.get.model():
93+
for vm in proxmox.nodes(node.node).qemu.get.model():
94+
print(f"{vm.vmid}. {vm.name} => {vm.status}")
95+
```
96+
97+
Whenever a `method(...)` call - `method` being `get`, `post`, `put`, `delete`,
98+
`set` or `create` - returns a structure that is or contains a
99+
`TypedDict`-annotated `dict`, `method.model(...)` returns a
100+
`pydantic.BaseModel` instead.
101+
102+
Values of optional fields are possibly `None` in the model instance.
103+
104+
#### Additional dependencies
105+
106+
- For type checking: `proxmoxer-stubs`, `pydantic`
107+
- At runtime: `proxmoxer-stubs`, `pydantic`
108+
50109
## Caveats
51110

52111
`proxmoxer.ProxmoxAPI` has several ways of expressing the same endpoint due to its magic implementation.
@@ -64,4 +123,8 @@ ProxmoxResource (/cluster/replication/some-id)
64123

65124
Only the first form will produce useful typing insights.
66125

67-
Parameters to `get`, `post`, `put`, `delete` are currently not individually annotated.
126+
Parameters to `get`, `post`, `put`, `delete`, `set`, `create` are currently not individually annotated.
127+
128+
The [API documentation](https://pve.proxmox.com/pve-docs/api-viewer/) is
129+
occasionally wrong or incomplete. In wrapper mode, `pydantic` will `raise` a
130+
`ValidationError` if the documentation is wrong.

0 commit comments

Comments
 (0)