Only run get_attr_docs if generating help text (#23723)

Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
This commit is contained in:
Harry Mellor 2025-08-27 14:55:12 +01:00 committed by GitHub
parent fe8d7b6f03
commit 513c1fe255
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -152,9 +152,17 @@ def is_online_quantization(quantization: Any) -> bool:
return quantization in ["inc"]
NEEDS_HELP = (
"--help" in (argv := sys.argv) # vllm SUBCOMMAND --help
or (argv0 := argv[0]).endswith("mkdocs") # mkdocs SUBCOMMAND
or argv0.endswith("mkdocs/__main__.py") # python -m mkdocs SUBCOMMAND
)
@functools.lru_cache(maxsize=30)
def _compute_kwargs(cls: ConfigType) -> dict[str, Any]:
cls_docs = get_attr_docs(cls)
# Save time only getting attr docs if we're generating help text
cls_docs = get_attr_docs(cls) if NEEDS_HELP else {}
kwargs = {}
for field in fields(cls):
# Get the set of possible types for the field
@ -172,7 +180,7 @@ def _compute_kwargs(cls: ConfigType) -> dict[str, Any]:
# Get the help text for the field
name = field.name
help = cls_docs[name].strip()
help = cls_docs.get(name, "").strip()
# Escape % for argparse
help = help.replace("%", "%%")
@ -254,6 +262,9 @@ def _compute_kwargs(cls: ConfigType) -> dict[str, Any]:
def get_kwargs(cls: ConfigType) -> dict[str, Any]:
"""Return argparse kwargs for the given Config dataclass.
If `--help` or `mkdocs` are not present in the command line command, the
attribute documentation will not be included in the help output.
The heavy computation is cached via functools.lru_cache, and a deep copy
is returned so callers can mutate the dictionary without affecting the
cached version.