Is there a way to automatically exclude files without docstring? #547
-
Hello, is it possible to automatically exclude Python files without docstrings in it? |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 16 replies
-
Hello! Yes, there's a way if you use the automatic reference recipe, though note that it will make collection happen twice: once to filter out modules without any docstrings, and a second time when actually rendering modules to HTML. In your script that generates the reference pages, collect data for your package and check if each module has any docstrings in it. Something like this (not tested): from pathlib import Path
import griffe
import mkdocs_gen_files
nav = mkdocs_gen_files.Nav()
data = griffe.load("your_package")
for path in sorted(Path("src").rglob("*.py")):
module_path = path.relative_to("src").with_suffix("")
doc_path = path.relative_to("src").with_suffix(".md")
full_doc_path = Path("reference", doc_path)
mod_parts = module_path.parts[1:]
mod_parts[-1] = mod_parts[-1][:-3] # remove .py
if not data[mod_parts].has_docstrings:
continue
# rest of the recipe... |
Beta Was this translation helpful? Give feedback.
-
@pawamoy I keep getting a key error on the mod_parts can you explain what the mod parts is supposed to do because you say it is to remove the .py but you are already doing that in the module_path = part can you give an example of what you are supposed to modify at mod_parts and then what are you supposed to modify and save as mod_parts[-1] = |
Beta Was this translation helpful? Give feedback.
-
@pawamoy so now i am running into an error where every time it is reading any of my init files it says that they have doc strings ... the .has_docstrings is returning true even when they don't have doc strings ... but other files that aren't init files that don't have doc strings the has_docstrings returns false so it is working on non init files ... am i doing something wrong or is it the griffe code? |
Beta Was this translation helpful? Give feedback.
-
@pawamoy i think you are answering my old question that i deleted ... do you mind taking a look at the new updated question about the init files always returning True even when there is no doc strings in the file because no matter if i use has_docstrings or has_docstring i get a true for strings and a false for string when looking at init files |
Beta Was this translation helpful? Give feedback.
-
@pawamoy so i have now removed everything from the init file and the docstrings is still returning true ... not even a line of code in the init file and still returns true this is also in reference to my comment i made earlier #547 (comment) |
Beta Was this translation helpful? Give feedback.
Hello! Yes, there's a way if you use the automatic reference recipe, though note that it will make collection happen twice: once to filter out modules without any docstrings, and a second time when actually rendering modules to HTML.
In your script that generates the reference pages, collect data for your package and check if each module has any docstrings in it. Something like this (not tested):