Skip to content

Commit

Permalink
add possible values
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenbal committed Jan 9, 2025
1 parent d7a327e commit c830e3d
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions django_setup_configuration/documentation/model_directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import textwrap
from dataclasses import dataclass
from enum import Enum
from typing import Annotated, Any, Dict, Type, Union, get_args, get_origin
from typing import Annotated, Any, Dict, Type, Union, get_args, get_origin, Literal

import ruamel.yaml
from docutils import nodes
Expand Down Expand Up @@ -75,10 +75,17 @@ def insert_example_with_comments(
yaml_set_comment_with_max_length(
example_data, field_name, field_info.description, 80, indent=depth * 2
)

if default := get_default_from_field_info(field_info):
example_data.yaml_set_comment_before_after_key(
field_name, f"default value: {default}", indent=depth * 2
)

if get_origin(field_info.annotation) == Literal:
example_data.yaml_set_comment_before_after_key(
field_name, f"possible values: {get_args(field_info.annotation)}", indent=depth * 2
)

example_data.yaml_set_comment_before_after_key(
field_name, f"required: {field_info.is_required()}", indent=depth * 2
)
Expand Down Expand Up @@ -166,15 +173,16 @@ def process_field_type(
]
return PolymorphicExample(example=data, commented_out_examples=other)

# Handle Pydantic models
if isinstance(field_type, type) and issubclass(field_type, BaseModel):
return generate_model_example(field_type, depth=depth)

# Handle lists
if get_origin(field_type) == list:
list_type = get_args(field_type)[0]
return [process_field_type(list_type, field_info, field_name, depth + 1)]

# Handle Pydantic models
if isinstance(field_type, type) and issubclass(field_type, BaseModel):
return generate_model_example(field_type, depth=depth)


def generate_basic_example(field_type: Any, field_info: FieldInfo) -> Any:
"""
Expand Down

0 comments on commit c830e3d

Please sign in to comment.