Skip to content

Commit

Permalink
fix: Fix various things
Browse files Browse the repository at this point in the history
- Fix AttributeError when docstring is empty
- Fix attribute dispatch on module itself (add self in path map)
- Fix filter method when no filters are used
  • Loading branch information
pawamoy committed Jan 2, 2020
1 parent 5780345 commit affbf06
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/mkdocstrings/docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def default_string(self):

class Docstring:
def __init__(self, value, signature=None):
self.original_value = value
self.original_value = value or ""
self.signature = signature
self.return_object = None
self.blocks = self.parse()
Expand Down
16 changes: 9 additions & 7 deletions src/mkdocstrings/documenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
[`Method`][mkdocstrings.documenter.Method], [`Function`][mkdocstrings.documenter.Function],
or [`Attribute`][mkdocstrings.documenter.Attribute]) with the documenter's `get_object_documentation` method.
The method takes the dotted-path to an object as argument.
3. get Markdown contents by calling the documented object's [`render`][mkdocstrings.documenter.Object.render] method
with more configuration options (should be refactored).
Here is how we proceed:
Expand All @@ -27,8 +25,8 @@
dispatching them onto the object thanks to their dotted-paths.
3. Each docstring is parsed to build a list of "docstring sections". Such a section can be a markdown block of lines,
a block of parameters, a block of exceptions, an admonition, or the information about the return value.
Exceptions and the return value are instances of [`AnnotatedObject`][mkdocstrings.documenter.AnnotatedObject],
while parameters are instances of the [`Parameter`][mkdocstrings.documenter.Parameter] class, which is a subclass of
Exceptions and the return value are instances of [`AnnotatedObject`][mkdocstrings.docstrings.AnnotatedObject],
while parameters are instances of the [`Parameter`][mkdocstrings.docstrings.Parameter] class, which is a subclass of
`AnnotatedObject`. To build these sections, we search for blocks matching the
[Google style](https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html) for docstrings.
Expand Down Expand Up @@ -145,7 +143,7 @@ def __init__(
self.parent = None
self.source = source

self._path_map = {}
self._path_map = {self.path: self}

self.attributes: List[Attribute] = []
"""List of all the object's attributes."""
Expand Down Expand Up @@ -288,7 +286,9 @@ class Attribute(Object):
class Documenter:
"""Class that contains the object documentation loading mechanisms."""

def __init__(self, global_filters):
def __init__(self, global_filters=None):
if not global_filters:
global_filters = []
self.global_filters = [(f, re.compile(f.lstrip("!"))) for f in global_filters]

def get_object_documentation(self, import_string: str) -> Union[Attribute, Method, Function, Module, Class]:
Expand Down Expand Up @@ -317,7 +317,7 @@ def get_module_documentation(self, module: ModuleType) -> Module:
root_object = Module(
name=name, path=path, file_path=module.__file__, docstring=Docstring(inspect.getdoc(module))
)
for member_name, member in filter(lambda m: not self.filter_name_out(m[0]), inspect.getmembers(module)):
for member_name, member in (m for m in inspect.getmembers(module) if not self.filter_name_out(m[0])):
if inspect.isclass(member) and inspect.getmodule(member) == module:
root_object.add_child(self.get_class_documentation(member, module))
elif inspect.isfunction(member) and inspect.getmodule(member) == module:
Expand Down Expand Up @@ -406,6 +406,8 @@ def get_function_documentation(self, function: Callable, module: Optional[Module

@lru_cache(maxsize=None)
def filter_name_out(self, name: str) -> bool:
if not self.global_filters:
return False
keep = True
for f, regex in self.global_filters:
is_matching = bool(regex.match(name))
Expand Down

0 comments on commit affbf06

Please sign in to comment.