Skip to content

Commit becbaeb

Browse files
committed
test(tests\hooks\test_validate_hook.py): Changed constructor validation to use AST ancestry, fixed hook tests and added Windows support for test_utils
1 parent af84d77 commit becbaeb

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

numpydoc/tests/hooks/test_utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ def test_find_project_root(tmp_path, request, reason_file, files, expected_reaso
2323
(tmp_path / reason_file).touch()
2424

2525
if files:
26-
expected_dir = Path("/") if expected_reason == "file system root" else tmp_path
26+
expected_dir = (
27+
Path(tmp_path.anchor) if expected_reason == "file system root" else tmp_path
28+
)
2729
for file in files:
2830
(tmp_path / file).touch()
2931
else:

numpydoc/tests/hooks/test_validate_hook.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ def test_validate_hook(example_module, config, capsys):
4141
4242
numpydoc/tests/hooks/example_module.py:8: EX01 No examples section found
4343
44+
numpydoc/tests/hooks/example_module.py:11: GL08 The object does not have a docstring
45+
4446
numpydoc/tests/hooks/example_module.py:17: ES01 No extended summary found
4547
4648
numpydoc/tests/hooks/example_module.py:17: PR01 Parameters {'**kwargs'} not documented
@@ -94,6 +96,8 @@ def test_validate_hook_with_ignore(example_module, capsys):
9496
"""
9597
numpydoc/tests/hooks/example_module.py:4: PR01 Parameters {'name'} not documented
9698
99+
numpydoc/tests/hooks/example_module.py:11: GL08 The object does not have a docstring
100+
97101
numpydoc/tests/hooks/example_module.py:17: PR01 Parameters {'**kwargs'} not documented
98102
99103
numpydoc/tests/hooks/example_module.py:17: PR07 Parameter "*args" has no description

numpydoc/validate.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -660,24 +660,20 @@ def validate(obj_name, validator_cls=None, **validator_kwargs):
660660
# cls = Validator._load_obj(f"{doc.name[:-9]}.{cls_name}") ## Alternative
661661
cls_doc = Validator(get_doc_object(cls))
662662
elif isinstance(doc, AstValidator): # Supports class traversal for ASTs.
663-
names = doc._name.split(".")
664-
665-
if len(names) > 2: # i.e. module.class.__init__
666-
nested_cls_names = names[1:-1] # class1,class2 etc.
667-
cls_name = names[-2]
668-
filename = doc.source_file_name # from the AstValidator object
669-
670-
# Use AST to find the class node...
671-
with open(filename) as file:
672-
module_node = ast.parse(file.read(), filename)
673-
674-
# Recursively find each subclass from the module node.
675-
next_node = module_node
676-
for name in nested_cls_names:
677-
next_node = _find_class_node(next_node, name)
678-
# Get the documentation.
663+
ancestry = doc.ancestry
664+
if len(ancestry) > 2: # e.g. module.class.__init__
665+
parent = doc.ancestry[-1] # Get the parent
666+
cls_name = ".".join(
667+
[
668+
getattr(node, "name", node.__module__)
669+
for node in doc.ancestry
670+
]
671+
)
679672
cls_doc = AstValidator(
680-
ast_node=next_node, filename=filename, obj_name=cls_name
673+
ast_node=parent,
674+
filename=doc.source_file_name,
675+
obj_name=cls_name,
676+
ancestry=doc.ancestry[:-1],
681677
)
682678
else:
683679
# Ignore edge case: __init__ functions that don't belong to a class.

0 commit comments

Comments
 (0)