Skip to content

Commit cbcc396

Browse files
committed
docs: Fix examples
1 parent 5dcc7eb commit cbcc396

File tree

5 files changed

+53
-5
lines changed

5 files changed

+53
-5
lines changed

docs/examples/model_noext.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

docs/index.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,33 @@ hide:
2020

2121
/// tab | Pydantic model
2222

23-
```python exec="1" result="python"
24-
print('--8<-- "docs/examples/model_ext.py"')
23+
```python
24+
--8<-- "examples/model_ext.py"
2525
```
2626

2727
///
2828

2929
/// tab | Without extension
3030

31+
```md exec="true" updatetoc="false"
3132
::: model_noext.ExampleModel
3233
options:
3334
heading_level: 3
35+
skip_local_inventory: true
36+
```
3437

3538
///
3639

37-
3840
/// tab | With extension
3941

42+
```md exec="true" updatetoc="false"
4043
::: model_ext.ExampleModel
4144
options:
4245
heading_level: 3
4346
extensions:
4447
- griffe_pydantic
48+
skip_local_inventory: true
49+
```
4550

4651
///
4752

examples/model_noext.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from typing import Any
2+
from pydantic import field_validator, model_validator, ConfigDict, BaseModel, Field
3+
4+
5+
class ExampleModel(BaseModel):
6+
"""An example model."""
7+
8+
model_config = ConfigDict(frozen=False)
9+
10+
field_without_default: str
11+
"""Shows the *[Required]* marker in the signature."""
12+
13+
field_plain_with_validator: int = 100
14+
"""Show standard field with type annotation."""
15+
16+
field_with_validator_and_alias: str = Field("FooBar", alias="BarFoo", validation_alias="BarFoo")
17+
"""Shows corresponding validator with link/anchor."""
18+
19+
field_with_constraints_and_description: int = Field(
20+
default=5, ge=0, le=100, description="Shows constraints within doc string."
21+
)
22+
23+
@field_validator("field_with_validator_and_alias", "field_without_default", mode="before")
24+
@classmethod
25+
def check_max_length_ten(cls, v) -> str:
26+
"""Show corresponding field with link/anchor."""
27+
if len(v) >= 10:
28+
raise ValueError("No more than 10 characters allowed")
29+
return v
30+
31+
@model_validator(mode="before")
32+
@classmethod
33+
def lowercase_only(cls, data: dict[str, Any]) -> dict[str, Any]:
34+
"""Ensure that the field without a default is lowercase."""
35+
if isinstance(data.get("field_without_default"), str):
36+
data["field_without_default"] = data["field_without_default"].lower()
37+
return data

zensical.toml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,13 @@ name = "python"
147147
class = "python"
148148
validator = "markdown_exec.validator"
149149
format = "markdown_exec.formatter"
150+
[[project.markdown_extensions.pymdownx.superfences.custom_fences]]
151+
name = "md"
152+
class = "markdown"
153+
validator = "markdown_exec.validator"
154+
format = "markdown_exec.formatter"
155+
[project.markdown_extensions.pymdownx.blocks.tab]
156+
alternate_style = true
150157
[project.markdown_extensions.pymdownx.tabbed]
151158
alternate_style = true
152159
[project.markdown_extensions.pymdownx.tasklist]
@@ -159,7 +166,7 @@ configurations = [{targets.include = ["reference/api.md"]}]
159166
# Plugins configuration
160167
# ----------------------------------------------------------------------------
161168
[project.plugins.mkdocstrings.handlers.python]
162-
paths = ["src"]
169+
paths = ["src", "examples"]
163170
inventories = ["https://docs.python.org/3/objects.inv"]
164171

165172
[project.plugins.mkdocstrings.handlers.python.options]

0 commit comments

Comments
 (0)