Unverified Commit 513c1fe2 authored by Harry Mellor's avatar Harry Mellor Committed by GitHub
Browse files

Only run `get_attr_docs` if generating help text (#23723)


Signed-off-by: default avatarHarry Mellor <19981378+hmellor@users.noreply.github.com>
parent fe8d7b6f
...@@ -152,9 +152,17 @@ def is_online_quantization(quantization: Any) -> bool: ...@@ -152,9 +152,17 @@ def is_online_quantization(quantization: Any) -> bool:
return quantization in ["inc"] 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) @functools.lru_cache(maxsize=30)
def _compute_kwargs(cls: ConfigType) -> dict[str, Any]: 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 = {} kwargs = {}
for field in fields(cls): for field in fields(cls):
# Get the set of possible types for the field # Get the set of possible types for the field
...@@ -172,7 +180,7 @@ def _compute_kwargs(cls: ConfigType) -> dict[str, Any]: ...@@ -172,7 +180,7 @@ def _compute_kwargs(cls: ConfigType) -> dict[str, Any]:
# Get the help text for the field # Get the help text for the field
name = field.name name = field.name
help = cls_docs[name].strip() help = cls_docs.get(name, "").strip()
# Escape % for argparse # Escape % for argparse
help = help.replace("%", "%%") help = help.replace("%", "%%")
...@@ -254,6 +262,9 @@ def _compute_kwargs(cls: ConfigType) -> dict[str, Any]: ...@@ -254,6 +262,9 @@ def _compute_kwargs(cls: ConfigType) -> dict[str, Any]:
def get_kwargs(cls: ConfigType) -> dict[str, Any]: def get_kwargs(cls: ConfigType) -> dict[str, Any]:
"""Return argparse kwargs for the given Config dataclass. """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 The heavy computation is cached via functools.lru_cache, and a deep copy
is returned so callers can mutate the dictionary without affecting the is returned so callers can mutate the dictionary without affecting the
cached version. cached version.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment