-
-
Notifications
You must be signed in to change notification settings - Fork 170
Description
I was looking for a way to skip GL08 checks on class constructors (__init__
) throughout my code. This is particularly as the numpydoc recommendation for class strings documents the constructor:
Class docstring
Use the same sections as outlined above (all except Returns are applicable). The constructor (__init__) should also be documented here, the Parameters section of the docstring details the constructor’s parameters.
It seems that there ought to be a default check ignore for __init__
constructors if no docstring has been provided (and if a class docstring has been provided). Obviously if an (optional) docstring for __init__
has been created, then it ought to be checked.
This conditional checking doesn't seem possible with the default override_<code>
, exclude
toml tags.
Keen to hear others thoughts!
Activity
mattgebert commentedon Dec 11, 2024
In case it wasn't clear,
GL08
has the description "The object does not have a docstring".stefanv commentedon Dec 11, 2024
That makes sense to me.
mattgebert commentedon Mar 25, 2025
Hey all (@larsoner, @stefanv),
I have re-opened the issue as the current implementation is not working with the
AstValidator
. It is my understanding that theAstValidator
is the preferred validator class, used when running numpydoc linting and CI testing? (i.e. The modifications we made are not /will not be used in the majority of numpydoc validation).When we improved the class constructor init GL08 reporting, we only wrote tests for the
Validator
class which works perfectly. This is becauseValidator
uses theinspect
library to find the code_obj, we can determine the parent module / class to check if the__init__
is documented in the constructor.However, under the implementation of the abstract syntax tree (ast) validator (
numpydoc.hooks.validate_docstrings.AstValidator
) thecode_obj
attribute does not become defined, making the resolving of the parent class (that the constructor is defined on) difficult. I have done a preliminary check if I can still resolve the similar names required to run theValidator._load_obj
without thecode_obj
attribute.Here's the way we get currently (roughly) get the class reference from the constructor:
Any ideas what we should do for the
AstValidator
? Unfortunately it seems that the ast nodes, includingast.FunctionDef
, only have abody
attribute to see sub-nodes, but there's no link to the parent node.The
AstValidator
is given a_filename
attribute and_name
attribute. The latter is a concat list of the sub-module class nodes, which should allow us to track to the class structure. I've just tested the following to be working:This seems quite convoluted just to get the parent class for the constructor. What are your thoughts?
mattgebert commentedon Mar 25, 2025
Further discussion on this, should all tests in test_validate also be written/run with AstValidator in mind?
mattgebert commentedon Apr 6, 2025
@stefanv Is it just me or are we not properly testing hook validation at all? I searched the
run test suite
CI for references tohook
(i.e.numpydoc/tests/hooks/test_validate_hook.py
) but I don't believetest.yml
actually runs this folder subset.I also wonder if our hook tests ought to be consistent with the regular tests, particularly given the unique AST traversal? Wondering if we can consolidate tests into some example files so both AST and regular Validator functionality can be routinely verified?
(edit) Could #556 be the reason and affecting our CI testing as well?
stefanv commentedon Apr 6, 2025
@mattgebert We run the tests using two different invocations:
and
The latter happens on the
prerelease
job, and does execute the hooks tests.BTW, the naming of that job made perfect sense when @jarrodmillman added it. But, it looks like it has been modified in 342bdce not to install pre-releases, so now makes less sense. We should probably re-introduce the
--pre
flag there, and also unify the way we invoke the tests (pytest -v .
or similar), to ensure that the hook tests are run.I haven't looked at the AST vs regular validator for a long time, and the details escape me now. I thought the AST validator did additional tests on top of the docstring validation (such as whether all arguments are documented). Ideally, we then shouldn't need to test the same functionality twice, once in the Validator and another time in ASTValidator?
numpydoc
needs much love, so your input is much appreciated.mattgebert commentedon May 11, 2025
Sorry it's been a while. I've added (#622) a fix and updated test to ensure the AST Validation functionality is also checked.
Sounds good, let's fix this up and add some extra commits to #622 to ensure this is on track.
I don't believe there are any additional validation checks, but there are tests for the use of configs (toml, setup) which seem useful. (hook testing goes like:
hooks.validate_docstrings.py
therun_hook
which simply createsDocstringVisitor
instances for each file. Thevisit
method simply runs thevalidate
method invalidate.py
, and everything runs pretty simple imo.)I get the impression that the many docstring examples in
test_validate.py
, and theexample_module.py
(from hook tests) could be consolidated at some point but this would be a big job to update the testing suite such that examples (good and bad) were relocated, and all test modules point to such examples to run their comprehensive tests.For the meantime, I've modified the
example_module.py
(tested bytest_validate_hook.py
) to reflect the required testing from the GL08 changes. I think the testing intest_validate_hook.py
is sufficient, though certainly less comprehensive than the regularValidator
.ci(test.yml): Added --pre option to prerelease job to ensure pre-rele…