Skip to content

Avoid mutable default arguments #342

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions numpydoc/docscrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class NumpyDocString(Mapping):
'index': {}
}

def __init__(self, docstring, config={}):
def __init__(self, docstring, config=None):
orig_docstring = docstring
docstring = textwrap.dedent(docstring).split('\n')

Expand Down Expand Up @@ -553,14 +553,16 @@ def dedent_lines(lines):


class FunctionDoc(NumpyDocString):
def __init__(self, func, role='func', doc=None, config={}):
def __init__(self, func, role='func', doc=None, config=None):
self._f = func
self._role = role # e.g. "func" or "meth"

if doc is None:
if func is None:
raise ValueError("No function or docstring given")
doc = inspect.getdoc(func) or ''
if config is None:
config = {}
NumpyDocString.__init__(self, doc, config)

def get_func(self):
Expand Down Expand Up @@ -590,8 +592,10 @@ def __str__(self):


class ObjDoc(NumpyDocString):
def __init__(self, obj, doc=None, config={}):
def __init__(self, obj, doc=None, config=None):
self._f = obj
if config is None:
config = {}
NumpyDocString.__init__(self, doc, config=config)


Expand All @@ -600,7 +604,7 @@ class ClassDoc(NumpyDocString):
extra_public_methods = ['__call__']

def __init__(self, cls, doc=None, modulename='', func_doc=FunctionDoc,
config={}):
config=None):
if not inspect.isclass(cls) and cls is not None:
raise ValueError("Expected a class or None, but got %r" % cls)
self._cls = cls
Expand All @@ -610,6 +614,8 @@ def __init__(self, cls, doc=None, modulename='', func_doc=FunctionDoc,
else:
ALL = object()

if config is None:
config = {}
self.show_inherited_members = config.get(
'show_inherited_class_members', True)

Expand Down Expand Up @@ -679,7 +685,7 @@ def _is_show_member(self, name):
return True


def get_doc_object(obj, what=None, doc=None, config={}):
def get_doc_object(obj, what=None, doc=None, config=None):
if what is None:
if inspect.isclass(obj):
what = 'class'
Expand All @@ -689,6 +695,8 @@ def get_doc_object(obj, what=None, doc=None, config={}):
what = 'function'
else:
what = 'object'
if config is None:
config = {}

if what == 'class':
return ClassDoc(obj, func_doc=FunctionDoc, doc=doc, config=config)
Expand Down
20 changes: 15 additions & 5 deletions numpydoc/docscrape_sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@


class SphinxDocString(NumpyDocString):
def __init__(self, docstring, config={}):
def __init__(self, docstring, config=None):
if config is None:
config = {}
NumpyDocString.__init__(self, docstring, config=config)
self.load_config(config)

Expand Down Expand Up @@ -392,25 +394,31 @@ def __str__(self, indent=0, func_role="obj"):


class SphinxFunctionDoc(SphinxDocString, FunctionDoc):
def __init__(self, obj, doc=None, config={}):
def __init__(self, obj, doc=None, config=None):
if config is None:
config = {}
self.load_config(config)
FunctionDoc.__init__(self, obj, doc=doc, config=config)


class SphinxClassDoc(SphinxDocString, ClassDoc):
def __init__(self, obj, doc=None, func_doc=None, config={}):
def __init__(self, obj, doc=None, func_doc=None, config=None):
if config is None:
config = {}
self.load_config(config)
ClassDoc.__init__(self, obj, doc=doc, func_doc=None, config=config)


class SphinxObjDoc(SphinxDocString, ObjDoc):
def __init__(self, obj, doc=None, config={}):
def __init__(self, obj, doc=None, config=None):
if config is None:
config = {}
self.load_config(config)
ObjDoc.__init__(self, obj, doc=doc, config=config)


# TODO: refactor to use docscrape.get_doc_object
def get_doc_object(obj, what=None, doc=None, config={}, builder=None):
def get_doc_object(obj, what=None, doc=None, config=None, builder=None):
if what is None:
if inspect.isclass(obj):
what = 'class'
Expand All @@ -421,6 +429,8 @@ def get_doc_object(obj, what=None, doc=None, config={}, builder=None):
else:
what = 'object'

if config is None:
config = {}
template_dirs = [os.path.join(os.path.dirname(__file__), 'templates')]
if builder is not None:
template_loader = BuiltinTemplateLoader()
Expand Down
2 changes: 1 addition & 1 deletion numpydoc/tests/test_docscrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def test_returns(doc):
assert arg_type == 'ndarray'
assert desc[0].startswith('The drawn samples')
assert desc[-1].endswith('distribution.')

arg, arg_type, desc = doc['Returns'][1]
assert arg == ''
assert arg_type == 'list of str'
Expand Down
2 changes: 1 addition & 1 deletion numpydoc/xref.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def make_xref(param_type, xref_aliases, xref_ignore):
to fully qualified names that can be cross-referenced.
xref_ignore : set or "all"
A set containing words not to cross-reference. Instead of a set, the
string 'all' can be given to ignore all unrecognized terms.
string 'all' can be given to ignore all unrecognized terms.
Unrecognized terms include those that are not in `xref_aliases` and
are not already wrapped in a reST role.

Expand Down