Skip to content

Commit 178adfb

Browse files
committed
Format examples for Pydantic
1 parent b8221e1 commit 178adfb

File tree

2 files changed

+65
-31
lines changed

2 files changed

+65
-31
lines changed

docs/docs/python-sdk/examples/pydantic_car.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
11
from __future__ import annotations
22

33
from asyncio import run as aiorun
4-
54
from typing import Annotated
65

7-
from pydantic import BaseModel, Field, ConfigDict
8-
from infrahub_sdk import InfrahubClient
6+
from pydantic import ConfigDict, Field
97
from rich import print as rprint
10-
from infrahub_sdk.schema import InfrahubAttributeParam as AttrParam, InfrahubRelationshipParam as RelParam, AttributeKind, from_pydantic, NodeSchema, NodeModel, GenericModel
8+
9+
from infrahub_sdk import InfrahubClient
10+
from infrahub_sdk.schema import (
11+
AttributeKind,
12+
GenericModel,
13+
NodeModel,
14+
NodeSchema,
15+
from_pydantic,
16+
)
17+
from infrahub_sdk.schema import (
18+
InfrahubAttributeParam as AttrParam,
19+
)
20+
from infrahub_sdk.schema import (
21+
InfrahubRelationshipParam as RelParam,
22+
)
1123

1224

1325
class Tag(NodeModel):
1426
model_config = ConfigDict(
1527
node_schema=NodeSchema(name="Tag", namespace="Test", human_readable_fields=["name__value"])
1628
)
17-
29+
1830
name: Annotated[str, AttrParam(unique=True), Field(description="The name of the tag")]
1931
label: str | None = Field(description="The label of the tag")
2032
description: Annotated[str | None, AttrParam(kind=AttributeKind.TEXTAREA)] = None
@@ -30,11 +42,12 @@ class TestCar(NodeModel):
3042
class TestPerson(GenericModel):
3143
name: str
3244

45+
3346
class TestCarOwner(NodeModel, TestPerson):
3447
cars: Annotated[list[TestCar] | None, RelParam(identifier="car__person")] = None
3548

3649

37-
async def main():
50+
async def main() -> None:
3851
client = InfrahubClient()
3952
schema = from_pydantic(models=[TestPerson, TestCar, Tag, TestPerson, TestCarOwner])
4053
rprint(schema.to_schema_dict())
Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,45 @@
11
from __future__ import annotations
22

3-
from asyncio import run as aiorun
4-
5-
from infrahub_sdk.async_typer import AsyncTyper
6-
73
from typing import Annotated
84

9-
from pydantic import BaseModel, Field, ConfigDict
10-
from infrahub_sdk import InfrahubClient
5+
from pydantic import ConfigDict, Field
116
from rich import print as rprint
12-
from infrahub_sdk.schema import InfrahubAttributeParam as AttrParam, InfrahubRelationshipParam as RelParam, AttributeKind, from_pydantic, NodeSchema, NodeModel, GenericSchema, GenericModel, RelationshipKind
137

8+
from infrahub_sdk import InfrahubClient
9+
from infrahub_sdk.async_typer import AsyncTyper
10+
from infrahub_sdk.schema import (
11+
GenericModel,
12+
GenericSchema,
13+
NodeModel,
14+
NodeSchema,
15+
RelationshipKind,
16+
from_pydantic,
17+
)
18+
from infrahub_sdk.schema import (
19+
InfrahubAttributeParam as AttrParam,
20+
)
21+
from infrahub_sdk.schema import (
22+
InfrahubRelationshipParam as RelParam,
23+
)
1424

1525
app = AsyncTyper()
1626

1727

1828
class Site(NodeModel):
1929
model_config = ConfigDict(
20-
node_schema=NodeSchema(name="Site", namespace="Infra", human_friendly_id=["name__value"], display_labels=["name__value"])
30+
node_schema=NodeSchema(
31+
name="Site", namespace="Infra", human_friendly_id=["name__value"], display_labels=["name__value"]
32+
)
2133
)
2234

2335
name: Annotated[str, AttrParam(unique=True)] = Field(description="The name of the site")
2436

2537

2638
class Vlan(NodeModel):
2739
model_config = ConfigDict(
28-
node_schema=NodeSchema(name="Vlan", namespace="Infra", human_friendly_id=["vlan_id__value"], display_labels=["vlan_id__value"])
40+
node_schema=NodeSchema(
41+
name="Vlan", namespace="Infra", human_friendly_id=["vlan_id__value"], display_labels=["vlan_id__value"]
42+
)
2943
)
3044

3145
name: str
@@ -35,39 +49,45 @@ class Vlan(NodeModel):
3549

3650
class Device(NodeModel):
3751
model_config = ConfigDict(
38-
node_schema=NodeSchema(name="Device", namespace="Infra", human_friendly_id=["name__value"], display_labels=["name__value"])
52+
node_schema=NodeSchema(
53+
name="Device", namespace="Infra", human_friendly_id=["name__value"], display_labels=["name__value"]
54+
)
3955
)
4056

4157
name: Annotated[str, AttrParam(unique=True)] = Field(description="The name of the car")
4258
site: Annotated[Site, RelParam(kind=RelationshipKind.ATTRIBUTE, identifier="device__site")]
43-
interfaces: Annotated[list[Interface], RelParam(kind=RelationshipKind.COMPONENT, identifier="device__interfaces")] = Field(default_factory=list)
59+
interfaces: Annotated[
60+
list[Interface], RelParam(kind=RelationshipKind.COMPONENT, identifier="device__interfaces")
61+
] = Field(default_factory=list)
4462

4563

4664
class Interface(GenericModel):
4765
model_config = ConfigDict(
48-
generic_schema=GenericSchema(name="Interface", namespace="Infra", human_friendly_id=["device__name__value", "name__value"], display_labels=["name__value"])
66+
generic_schema=GenericSchema(
67+
name="Interface",
68+
namespace="Infra",
69+
human_friendly_id=["device__name__value", "name__value"],
70+
display_labels=["name__value"],
71+
)
4972
)
5073

5174
device: Annotated[Device, RelParam(kind=RelationshipKind.PARENT, identifier="device__interfaces")]
5275
name: str
5376
description: str | None = None
5477

78+
5579
class L2Interface(Interface):
56-
model_config = ConfigDict(
57-
node_schema=NodeSchema(name="L2Interface", namespace="Infra")
58-
)
59-
80+
model_config = ConfigDict(node_schema=NodeSchema(name="L2Interface", namespace="Infra"))
81+
6082
vlans: list[Vlan] = Field(default_factory=list)
6183

84+
6285
class LoopbackInterface(Interface):
63-
model_config = ConfigDict(
64-
node_schema=NodeSchema(name="LoopbackInterface", namespace="Infra")
65-
)
66-
86+
model_config = ConfigDict(node_schema=NodeSchema(name="LoopbackInterface", namespace="Infra"))
6787

6888

6989
@app.command()
70-
async def load_schema():
90+
async def load_schema() -> None:
7191
client = InfrahubClient()
7292
schema = from_pydantic(models=[Site, Device, Interface, L2Interface, LoopbackInterface, Vlan])
7393
rprint(schema.to_schema_dict())
@@ -76,7 +96,7 @@ async def load_schema():
7696

7797

7898
@app.command()
79-
async def load_data():
99+
async def load_data() -> None:
80100
client = InfrahubClient()
81101

82102
atl = await client.create("InfraSite", name="ATL")
@@ -100,14 +120,15 @@ async def load_data():
100120

101121

102122
@app.command()
103-
async def query_data():
123+
async def query_data() -> None:
104124
client = InfrahubClient()
105125
sites = await client.all(kind=Site)
126+
rprint(sites)
106127

107-
breakpoint()
108128
devices = await client.all(kind=Device)
109129
for device in devices:
110130
rprint(device)
111131

132+
112133
if __name__ == "__main__":
113-
app()
134+
app()

0 commit comments

Comments
 (0)