tidy(config): move config docstring builder to its script

This commit is contained in:
psychedelicious 2024-03-15 21:50:42 +11:00
parent e8b030427d
commit 4633242503
3 changed files with 38 additions and 35 deletions

View File

@ -7,7 +7,7 @@ import os
import re
from functools import lru_cache
from pathlib import Path
from typing import Any, Literal, Optional, get_args, get_type_hints
from typing import Any, Literal, Optional
import yaml
from pydantic import BaseModel, Field, PrivateAttr, field_validator
@ -321,38 +321,6 @@ class InvokeAIAppConfig(BaseSettings):
return root
def generate_config_docstrings() -> str:
"""Helper function for mkdocs. Generates a docstring for the InvokeAIAppConfig class.
You shouldn't run this manually. Instead, run `scripts/update-config-docstring.py` to update the docstring.
A makefile target is also available: `make update-config-docstring`.
See that script for more information about why this is necessary.
"""
docstring = ' """Invoke\'s global app configuration.\n\n'
docstring += " Typically, you won't need to interact with this class directly. Instead, use the `get_config` function from `invokeai.app.services.config` to get a singleton config object.\n\n"
docstring += " Attributes:\n"
field_descriptions: list[str] = []
type_hints = get_type_hints(InvokeAIAppConfig)
for k, v in InvokeAIAppConfig.model_fields.items():
if v.exclude:
continue
field_type = type_hints.get(k)
extra = ""
if getattr(field_type, "__origin__", None) is Literal:
# Get options for literals - the docs generator can't pull these out
options = [f"`{str(x)}`" for x in get_args(field_type)]
extra = f"<br>Valid values: {', '.join(options)}"
field_descriptions.append(f" {k}: {v.description}{extra}")
docstring += "\n".join(field_descriptions)
docstring += '\n """'
return docstring
def migrate_v3_config_dict(config_dict: dict[str, Any]) -> InvokeAIAppConfig:
"""Migrate a v3 config dictionary to a current config object.

View File

@ -1,4 +1,39 @@
import os
from typing import Literal, get_args, get_type_hints
from invokeai.app.services.config.config_default import InvokeAIAppConfig
def generate_config_docstrings() -> str:
"""Helper function for mkdocs. Generates a docstring for the InvokeAIAppConfig class.
You shouldn't run this manually. Instead, run `scripts/update-config-docstring.py` to update the docstring.
A makefile target is also available: `make update-config-docstring`.
See that script for more information about why this is necessary.
"""
docstring = ' """Invoke\'s global app configuration.\n\n'
docstring += " Typically, you won't need to interact with this class directly. Instead, use the `get_config` function from `invokeai.app.services.config` to get a singleton config object.\n\n"
docstring += " Attributes:\n"
field_descriptions: list[str] = []
type_hints = get_type_hints(InvokeAIAppConfig)
for k, v in InvokeAIAppConfig.model_fields.items():
if v.exclude:
continue
field_type = type_hints.get(k)
extra = ""
if getattr(field_type, "__origin__", None) is Literal:
# Get options for literals - the docs generator can't pull these out
options = [f"`{str(x)}`" for x in get_args(field_type)]
extra = f"<br>Valid values: {', '.join(options)}"
field_descriptions.append(f" {k}: {v.description}{extra}")
docstring += "\n".join(field_descriptions)
docstring += '\n """'
return docstring
# The pydantic app config can be documented automatically using mkdocs, but this requires that the docstring
# for the class is kept up to date. We use a pydantic model for the app config. Each config setting is a field
@ -14,7 +49,6 @@ def main():
# Change working directory to the repo root
os.chdir(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
from invokeai.app.services.config.config_default import generate_config_docstrings
docstring = generate_config_docstrings()

View File

@ -1,4 +1,5 @@
from invokeai.app.services.config.config_default import InvokeAIAppConfig, generate_config_docstrings
from invokeai.app.services.config.config_default import InvokeAIAppConfig
from scripts.update_config_docstring import generate_config_docstrings
def test_app_config_docstrings_are_current():