Skip to content

Commit

Permalink
Merge pull request #42 from rsokl/autoformat
Browse files Browse the repository at this point in the history
run isort and black on everything
  • Loading branch information
rsokl authored Aug 28, 2021
2 parents 072d240 + 2802388 commit 7e08197
Show file tree
Hide file tree
Showing 11 changed files with 516 additions and 451 deletions.
49 changes: 31 additions & 18 deletions src/custom_inherit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

from abc import ABCMeta as _ABCMeta

from . import _style_store
from ._decorator_base import DocInheritDecorator as _DocInheritDecorator
from ._metaclass_base import DocInheritorBase as _DocInheritorBase
from . import _style_store
from ._style_store import (
google, numpy, numpy_napoleon, parent, reST,
google_with_merge, numpy_napoleon_with_merge, numpy_with_merge
google,
google_with_merge,
numpy,
numpy_napoleon,
numpy_napoleon_with_merge,
numpy_with_merge,
parent,
reST,
)
from ._version import get_versions

Expand All @@ -31,11 +37,11 @@ def _check_style_function(style_func):


class _Store(object):
""" A dictionary-like object that stores the styles available for the doc-inheritance metaclass and decorator,
respectively.
"""A dictionary-like object that stores the styles available for the doc-inheritance metaclass and decorator,
respectively.
Only callable objects with the signature: f(Optional[str], Optional[str]) -> Optional[str]
can be stored. If f is a valid callable, then _Store()[f] -> f."""
Only callable objects with the signature: f(Optional[str], Optional[str]) -> Optional[str]
can be stored. If f is a valid callable, then _Store()[f] -> f."""

def __init__(self, *args, **kwargs):
self._store = dict()
Expand All @@ -50,7 +56,7 @@ def __str__(self):
return "\n".join((out_str, styles))

def __setitem__(self, style_name, style_func):
""" Make available a new function for merging a 'parent' and 'child' docstring.
"""Make available a new function for merging a 'parent' and 'child' docstring.
Parameters
----------
Expand All @@ -68,7 +74,7 @@ def __setitem__(self, style_name, style_func):
self._store[style_name] = style_func

def __getitem__(self, item):
""" Given a valid style-ID, retrieve a stored style. If a valid function (callable) is
"""Given a valid style-ID, retrieve a stored style. If a valid function (callable) is
supplied, return it in place.
Parameters
Expand All @@ -91,8 +97,8 @@ def keys(self):
return self._store.keys()

def pop(self, *args):
""" D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
If key is not found, d is returned if given, otherwise KeyError is raised. """
"""D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
If key is not found, d is returned if given, otherwise KeyError is raised."""
if len(args) < 3:
return self._store.pop(*args)
else:
Expand All @@ -101,7 +107,7 @@ def pop(self, *args):
)

def update(self, *args, **kwargs):
""" D.update([E, ]**F) -> None. Update D from dict/iterable E and F.
"""D.update([E, ]**F) -> None. Update D from dict/iterable E and F.
If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]"""
if len(args) > 1:
raise TypeError("update expected at most 1 arguments, got %d" % len(args))
Expand All @@ -117,11 +123,12 @@ def items(self):
""" D.items() -> a set-like object providing a view on D's items"""
return self._store.items()


store = _Store([(key, getattr(_style_store, key)) for key in _style_store.__all__])


def add_style(style_name, style_func):
""" Make available a new function for merging a 'parent' and 'child' docstring.
"""Make available a new function for merging a 'parent' and 'child' docstring.
Parameters
----------
Expand All @@ -133,7 +140,7 @@ def add_style(style_name, style_func):


def remove_style(style):
""" Remove the specified style from the style store.
"""Remove the specified style from the style store.
Parameters
----------
Expand All @@ -143,8 +150,10 @@ def remove_style(style):
store.pop(style)


def DocInheritMeta(style="parent", abstract_base_class=False, include_special_methods=False):
""" A metaclass that merges the respective docstrings of a parent class and of its child, along with their
def DocInheritMeta(
style="parent", abstract_base_class=False, include_special_methods=False
):
"""A metaclass that merges the respective docstrings of a parent class and of its child, along with their
properties, methods (including classmethod, staticmethod, decorated methods).
Parameters
Expand All @@ -169,7 +178,11 @@ def DocInheritMeta(style="parent", abstract_base_class=False, include_special_me
custom_inherit.DocInheritorBase"""

merge_func = store[style]
metaclass = type(_DocInheritorBase.__name__, _DocInheritorBase.__bases__, dict(_DocInheritorBase.__dict__))
metaclass = type(
_DocInheritorBase.__name__,
_DocInheritorBase.__bases__,
dict(_DocInheritorBase.__dict__),
)
metaclass.include_special_methods = include_special_methods
metaclass.class_doc_inherit = staticmethod(merge_func)
metaclass.attr_doc_inherit = staticmethod(merge_func)
Expand All @@ -182,7 +195,7 @@ def DocInheritMeta(style="parent", abstract_base_class=False, include_special_me


def doc_inherit(parent, style="parent"):
""" Returns a function/method decorator that, given `parent`, updates the docstring of the decorated
"""Returns a function/method decorator that, given `parent`, updates the docstring of the decorated
function/method based on the specified style and the corresponding attribute of `parent`.
Parameters
Expand Down
4 changes: 2 additions & 2 deletions src/custom_inherit/_decorator_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


class DocInheritDecorator(object):
""" A decorator that merges provided parent docstring with the docstring of the decorated
"""A decorator that merges provided parent docstring with the docstring of the decorated
function/method/property.
Methods
Expand Down Expand Up @@ -50,7 +50,7 @@ def __call__(self, func):

@staticmethod
def doc_merger(prnt_attr_doc, child_doc):
""" Merges the parent and child docstrings into a single docstring.
"""Merges the parent and child docstrings into a single docstring.
Parameters
----------
Expand Down
3 changes: 1 addition & 2 deletions src/custom_inherit/_doc_parse_tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import absolute_import

from .napoleon_parse_tools import (merge_google_napoleon_docs,
merge_numpy_napoleon_docs)
from .napoleon_parse_tools import merge_google_napoleon_docs, merge_numpy_napoleon_docs
from .numpy_parse_tools import merge_numpy_docs
from .rest_parse_tools import merge_rest_docs

Expand Down
28 changes: 16 additions & 12 deletions src/custom_inherit/_doc_parse_tools/napoleon_parse_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@


def parse_napoleon_doc(doc, style):
""" Extract the text from the various sections of a numpy-formatted docstring.
"""Extract the text from the various sections of a numpy-formatted docstring.
Parameters
----------
Expand Down Expand Up @@ -96,7 +96,7 @@ def parse_napoleon_doc(doc, style):


def merge_section(key, prnt_sec, child_sec, style, merge_within_sections=False):
""" Synthesize a output napoleon docstring section.
"""Synthesize a output napoleon docstring section.
Parameters
----------
Expand Down Expand Up @@ -133,7 +133,7 @@ def merge_section(key, prnt_sec, child_sec, style, merge_within_sections=False):


def merge_all_sections(prnt_sctns, child_sctns, style, merge_within_sections=False):
""" Merge the doc-sections of the parent's and child's attribute into a single docstring.
"""Merge the doc-sections of the parent's and child's attribute into a single docstring.
Parameters
----------
Expand All @@ -158,15 +158,17 @@ def merge_all_sections(prnt_sctns, child_sctns, style, merge_within_sections=Fal
prnt_sctns[key],
child_sctns[key],
style,
merge_within_sections=merge_within_sections
merge_within_sections=merge_within_sections,
)
if sect is not None:
doc.append(sect)
return "\n\n".join(doc) if doc else None


def merge_numpy_napoleon_docs(prnt_doc=None, child_doc=None, merge_within_sections=False):
""" Merge two numpy-style docstrings into a single docstring, according to napoleon docstring sections.
def merge_numpy_napoleon_docs(
prnt_doc=None, child_doc=None, merge_within_sections=False
):
"""Merge two numpy-style docstrings into a single docstring, according to napoleon docstring sections.
Given the numpy-style docstrings from a parent and child's attributes, merge the docstring
sections such that the child's section is used, wherever present, otherwise the parent's
Expand All @@ -187,18 +189,20 @@ def merge_numpy_napoleon_docs(prnt_doc=None, child_doc=None, merge_within_sectio
Returns
-------
Union[str, None]
The merged docstring. """
The merged docstring."""
style = "numpy"
return merge_all_sections(
parse_napoleon_doc(prnt_doc, style),
parse_napoleon_doc(child_doc, style),
style,
merge_within_sections=merge_within_sections
merge_within_sections=merge_within_sections,
)


def merge_google_napoleon_docs(prnt_doc=None, child_doc=None, merge_within_sections=False):
""" Merge two google-style docstrings into a single docstring, according to napoleon docstring sections.
def merge_google_napoleon_docs(
prnt_doc=None, child_doc=None, merge_within_sections=False
):
"""Merge two google-style docstrings into a single docstring, according to napoleon docstring sections.
Given the google-style docstrings from a parent and child's attributes, merge the docstring
sections such that the child's section is used, wherever present, otherwise the parent's
Expand All @@ -219,11 +223,11 @@ def merge_google_napoleon_docs(prnt_doc=None, child_doc=None, merge_within_secti
Returns
-------
Union[str, None]
The merged docstring. """
The merged docstring."""
style = "google"
return merge_all_sections(
parse_napoleon_doc(prnt_doc, style),
parse_napoleon_doc(child_doc, style),
style,
merge_within_sections=merge_within_sections
merge_within_sections=merge_within_sections,
)
14 changes: 7 additions & 7 deletions src/custom_inherit/_doc_parse_tools/numpy_parse_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


def parse_numpy_doc(doc):
""" Extract the text from the various sections of a numpy-formatted docstring.
"""Extract the text from the various sections of a numpy-formatted docstring.
Parameters
----------
Expand Down Expand Up @@ -69,7 +69,7 @@ def parse_numpy_doc(doc):


def merge_section(key, prnt_sec, child_sec, merge_within_sections=False):
""" Synthesize a output numpy docstring section.
"""Synthesize a output numpy docstring section.
Parameters
----------
Expand Down Expand Up @@ -101,7 +101,7 @@ def merge_section(key, prnt_sec, child_sec, merge_within_sections=False):


def merge_all_sections(prnt_sctns, child_sctns, merge_within_sections=False):
""" Merge the doc-sections of the parent's and child's attribute into a single docstring.
"""Merge the doc-sections of the parent's and child's attribute into a single docstring.
Parameters
----------
Expand All @@ -125,15 +125,15 @@ def merge_all_sections(prnt_sctns, child_sctns, merge_within_sections=False):
key,
prnt_sctns[key],
child_sctns[key],
merge_within_sections=merge_within_sections
merge_within_sections=merge_within_sections,
)
if sect is not None:
doc.append(sect)
return "\n\n".join(doc) if doc else None


def merge_numpy_docs(prnt_doc=None, child_doc=None, merge_within_sections=False):
""" Merge two numpy-style docstrings into a single docstring.
"""Merge two numpy-style docstrings into a single docstring.
Given the numpy-style docstrings from a parent and child's attributes, merge the docstring
sections such that the child's section is used, wherever present, otherwise the parent's
Expand All @@ -153,9 +153,9 @@ def merge_numpy_docs(prnt_doc=None, child_doc=None, merge_within_sections=False)
-------
Union[str, None]
The merged docstring.
"""
"""
return merge_all_sections(
parse_numpy_doc(prnt_doc),
parse_numpy_doc(child_doc),
merge_within_sections=merge_within_sections
merge_within_sections=merge_within_sections,
)
4 changes: 2 additions & 2 deletions src/custom_inherit/_doc_parse_tools/rest_parse_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ def is_delimiter(line):


def parse_rest_doc(doc):
""" Extract the headers, delimiters, and text from reST-formatted docstrings.
"""Extract the headers, delimiters, and text from reST-formatted docstrings.
Parameters
----------
doc: Union[str, None]
Returns
-------
Dict[str, Section] """
Dict[str, Section]"""

class Section(object):
def __init__(self, header=None, body=None):
Expand Down
2 changes: 1 addition & 1 deletion src/custom_inherit/_doc_parse_tools/section_items.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""This module handles sections with items."""

from collections import OrderedDict
import inspect
import re
from collections import OrderedDict

try:
from textwrap import indent
Expand Down
Loading

0 comments on commit 7e08197

Please sign in to comment.