[Bugfix][CLI] Include inherited field docstrings in get_attr_docs (#49821)
Signed-off-by: Chenglun Hu <chenglunhu@gmail.com> Signed-off-by: hclsys <chenglunhu@gmail.com> Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com> Co-authored-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
This commit is contained in:
@@ -8,7 +8,12 @@ import pytest
|
|||||||
|
|
||||||
from vllm.config.cache import CacheConfig
|
from vllm.config.cache import CacheConfig
|
||||||
from vllm.config.scheduler import SchedulerConfig
|
from vllm.config.scheduler import SchedulerConfig
|
||||||
from vllm.config.utils import get_hash_factors, hash_factors, normalize_value
|
from vllm.config.utils import (
|
||||||
|
get_attr_docs,
|
||||||
|
get_hash_factors,
|
||||||
|
hash_factors,
|
||||||
|
normalize_value,
|
||||||
|
)
|
||||||
|
|
||||||
# Helpers
|
# Helpers
|
||||||
|
|
||||||
@@ -114,7 +119,7 @@ def test_normalize_value_uuid_and_to_json():
|
|||||||
[
|
[
|
||||||
(lambda x: x),
|
(lambda x: x),
|
||||||
(type("CallableInstance", (), {"__call__": lambda self: 0}))(),
|
(type("CallableInstance", (), {"__call__": lambda self: 0}))(),
|
||||||
(lambda: (lambda: 0))(), # nested function instance
|
(lambda: lambda: 0)(), # nested function instance
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_error_cases(bad):
|
def test_error_cases(bad):
|
||||||
@@ -190,6 +195,31 @@ def test_ir_config_hash_initializes_provider_and_tracks_implementation(monkeypat
|
|||||||
assert config.compute_hash() != first_hash
|
assert config.compute_hash() != first_hash
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_attr_docs_includes_inherited_fields():
|
||||||
|
"""get_attr_docs must collect attribute docstrings inherited from base
|
||||||
|
classes, not just those declared in the class's own body."""
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Base:
|
||||||
|
inherited: object = None
|
||||||
|
"""doc for inherited (base)."""
|
||||||
|
overridden: object = None
|
||||||
|
"""base version of overridden."""
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Derived(Base):
|
||||||
|
own: object = None
|
||||||
|
"""doc for own (derived)."""
|
||||||
|
overridden: object = None
|
||||||
|
"""derived override of overridden."""
|
||||||
|
|
||||||
|
docs = get_attr_docs(Derived)
|
||||||
|
|
||||||
|
assert docs["own"] == "doc for own (derived)."
|
||||||
|
assert docs["inherited"] == "doc for inherited (base)."
|
||||||
|
assert docs["overridden"] == "derived override of overridden."
|
||||||
|
|
||||||
|
|
||||||
def test_envs_compile_factors_stable():
|
def test_envs_compile_factors_stable():
|
||||||
"""Test that envs.compile_factors() hash is stable across fresh initializations.
|
"""Test that envs.compile_factors() hash is stable across fresh initializations.
|
||||||
|
|
||||||
@@ -325,3 +355,10 @@ print(hash_factors(envs.compile_factors()))
|
|||||||
"HOME relocation changed the compile-cache env hash - a "
|
"HOME relocation changed the compile-cache env hash - a "
|
||||||
"location-only derived var is leaking into the key"
|
"location-only derived var is leaking into the key"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_attr_docs_rejects_non_class():
|
||||||
|
"""The annotation forbids it, but the runtime guard still protects
|
||||||
|
untyped callers, so keep asserting the documented TypeError."""
|
||||||
|
with pytest.raises(TypeError, match="Given object was not a class."):
|
||||||
|
get_attr_docs(object()) # type: ignore[arg-type]
|
||||||
|
|||||||
+22
-5
@@ -158,17 +158,20 @@ def getattr_iter(
|
|||||||
return default_factory() if default_factory is not None else default
|
return default_factory() if default_factory is not None else default
|
||||||
|
|
||||||
|
|
||||||
def get_attr_docs(cls: type[Any]) -> dict[str, str]:
|
def _get_own_attr_docs(cls: type[Any], out: dict[str, str]) -> None:
|
||||||
"""Get any docstrings placed after attribute assignments in a class body.
|
"""Get any docstrings placed after attribute assignments in a class body.
|
||||||
|
|
||||||
https://davidism.com/mit-license/
|
https://davidism.com/mit-license/
|
||||||
"""
|
"""
|
||||||
cls_node = ast.parse(textwrap.dedent(inspect.getsource(cls))).body[0]
|
try:
|
||||||
|
source = inspect.getsource(cls)
|
||||||
|
except (OSError, TypeError):
|
||||||
|
# ``object`` and classes without retrievable source (e.g. builtins).
|
||||||
|
return
|
||||||
|
|
||||||
|
cls_node = ast.parse(textwrap.dedent(source)).body[0]
|
||||||
if not isinstance(cls_node, ast.ClassDef):
|
if not isinstance(cls_node, ast.ClassDef):
|
||||||
raise TypeError("Given object was not a class.")
|
return
|
||||||
|
|
||||||
out = {}
|
|
||||||
|
|
||||||
# Consider each pair of nodes.
|
# Consider each pair of nodes.
|
||||||
for a, b in pairwise(cls_node.body):
|
for a, b in pairwise(cls_node.body):
|
||||||
@@ -194,6 +197,20 @@ def get_attr_docs(cls: type[Any]) -> dict[str, str]:
|
|||||||
|
|
||||||
out[target.id] = doc
|
out[target.id] = doc
|
||||||
|
|
||||||
|
|
||||||
|
def get_attr_docs(cls: type[Any]) -> dict[str, str]:
|
||||||
|
"""Get any docstrings placed after attribute assignments in a class body or
|
||||||
|
its bases."""
|
||||||
|
if not isinstance(cls, type):
|
||||||
|
raise TypeError("Given object was not a class.")
|
||||||
|
|
||||||
|
out: dict[str, str] = {}
|
||||||
|
|
||||||
|
# Walk the MRO from the most-base class to ``cls`` so that a docstring
|
||||||
|
# redefined on a subclass overrides the one inherited from a base class.
|
||||||
|
for base in reversed(cls.__mro__):
|
||||||
|
_get_own_attr_docs(base, out)
|
||||||
|
|
||||||
return out
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user