Skip to content

Sourcery refactored main branch #1

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
41 changes: 11 additions & 30 deletions numpydoc/docscrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ def __init__(self, data):
String with lines separated by '\\n'.

"""
if isinstance(data, list):
self._str = data
else:
self._str = data.split('\n') # store string as list of lines

self._str = data if isinstance(data, list) else data.split('\n')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Reader.__init__ refactored with the following changes:

This removes the following comments ( why? ):

# store string as list of lines

self.reset()

def __getitem__(self, n):
Expand All @@ -47,12 +43,11 @@ def reset(self):
self._l = 0 # current line nr

def read(self):
if not self.eof():
out = self[self._l]
self._l += 1
return out
else:
if self.eof():
return ''
out = self[self._l]
self._l += 1
return out
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Reader.read refactored with the following changes:


def seek_next_non_empty_line(self):
for l in self[self._l:]:
Expand Down Expand Up @@ -88,10 +83,7 @@ def is_unindented(line):
return self.read_to_condition(is_unindented)

def peek(self, n=0):
if self._l + n < len(self._str):
return self[self._l + n]
else:
return ''
return self[self._l + n] if self._l + n < len(self._str) else ''
Comment on lines -91 to +86
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Reader.peek refactored with the following changes:


def is_empty(self):
return not ''.join(self._str).strip()
Expand Down Expand Up @@ -228,11 +220,7 @@ def _parse_param_list(self, content, single_element_is_type=False):
arg_name, arg_type = header.split(' :', maxsplit=1)
arg_name, arg_type = arg_name.strip(), arg_type.strip()
else:
if single_element_is_type:
arg_name, arg_type = '', header
else:
arg_name, arg_type = header, ''

arg_name, arg_type = ('', header) if single_element_is_type else (header, '')
Comment on lines -231 to +223
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function NumpyDocString._parse_param_list refactored with the following changes:

desc = r.read_to_next_unindented_line()
desc = dedent_lines(desc)
desc = strip_blank_lines(desc)
Expand Down Expand Up @@ -378,7 +366,7 @@ def _parse(self):
self._parse_summary()

sections = list(self._read_sections())
section_names = set([section for section, content in sections])
section_names = {section for section, content in sections}
Comment on lines -381 to +369
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function NumpyDocString._parse refactored with the following changes:


has_returns = 'Returns' in section_names
has_yields = 'Yields' in section_names
Expand Down Expand Up @@ -453,14 +441,10 @@ def _str_signature(self):
return ['']

def _str_summary(self):
if self['Summary']:
return self['Summary'] + ['']
return []
return self['Summary'] + [''] if self['Summary'] else []
Comment on lines -456 to +444
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function NumpyDocString._str_summary refactored with the following changes:


def _str_extended_summary(self):
if self['Extended Summary']:
return self['Extended Summary'] + ['']
return []
return self['Extended Summary'] + [''] if self['Extended Summary'] else []
Comment on lines -461 to +447
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function NumpyDocString._str_extended_summary refactored with the following changes:


def _str_param_list(self, name):
out = []
Expand Down Expand Up @@ -642,10 +626,7 @@ def __init__(self, cls, doc=None, modulename='', func_doc=FunctionDoc,

if config.get('show_class_members', True) and _exclude is not ALL:
def splitlines_x(s):
if not s:
return []
else:
return s.splitlines()
return [] if not s else s.splitlines()
Comment on lines -645 to +629
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ClassDoc.__init__.splitlines_x refactored with the following changes:

for field, items in [('Methods', self.methods),
('Attributes', self.properties)]:
if not self[field]:
Expand Down
68 changes: 30 additions & 38 deletions numpydoc/docscrape_sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,13 @@ def load_config(self, config):

# string conversion routines
def _str_header(self, name, symbol='`'):
return ['.. rubric:: ' + name, '']
return [f'.. rubric:: {name}', '']
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SphinxDocString._str_header refactored with the following changes:


def _str_field_list(self, name):
return [':' + name + ':']
return [f':{name}:']
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SphinxDocString._str_field_list refactored with the following changes:


def _str_indent(self, doc, indent=4):
out = []
for line in doc:
out += [' '*indent + line]
return out
return [' '*indent + line for line in doc]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SphinxDocString._str_indent refactored with the following changes:


def _str_signature(self):
return ['']
Expand All @@ -62,13 +59,13 @@ def _str_extended_summary(self):
return self['Extended Summary'] + ['']

def _str_returns(self, name='Returns'):
named_fmt = '**%s** : %s'
unnamed_fmt = '%s'

out = []
if self[name]:
out += self._str_field_list(name)
out += ['']
named_fmt = '**%s** : %s'
unnamed_fmt = '%s'

Comment on lines -65 to +68
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SphinxDocString._str_returns refactored with the following changes:

for param in self[name]:
param_type = param.type
if param_type and self.xref_param_type:
Expand All @@ -93,9 +90,9 @@ def _str_returns(self, name='Returns'):

def _escape_args_and_kwargs(self, name):
if name[:2] == '**':
return r'\*\*' + name[2:]
return f'\\*\\*{name[2:]}'
elif name[:1] == '*':
return r'\*' + name[1:]
return f'\\*{name[1:]}'
Comment on lines -96 to +95
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SphinxDocString._escape_args_and_kwargs refactored with the following changes:

else:
return name

Expand Down Expand Up @@ -154,22 +151,13 @@ def _process_param(self, param, desc, fake_autosummary):
if not (param_obj and obj_doc):
return display_param, desc

prefix = getattr(self, '_name', '')
if prefix:
link_prefix = f'{prefix}.'
else:
link_prefix = ''

link_prefix = f'{prefix}.' if (prefix := getattr(self, '_name', '')) else ''
# Referenced object has a docstring
display_param = f':obj:`{param} <{link_prefix}{param}>`'
if obj_doc:
# Overwrite desc. Take summary logic of autosummary
desc = re.split(r'\n\s*\n', obj_doc.strip(), 1)[0]
# XXX: Should this have DOTALL?
# It does not in autosummary
m = re.search(r"^([A-Z].*?\.)(?:\s|$)",
' '.join(desc.split()))
if m:
if m := re.search(r"^([A-Z].*?\.)(?:\s|$)", ' '.join(desc.split())):
Comment on lines -157 to +160
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SphinxDocString._process_param refactored with the following changes:

This removes the following comments ( why? ):

# XXX: Should this have DOTALL?
#      It does not in autosummary

desc = m.group(1).strip()
else:
desc = desc.partition('\n')[0]
Expand Down Expand Up @@ -268,12 +256,12 @@ def _str_member_list(self, name):
out += [''] + autosum

if others:
maxlen_0 = max(3, max([len(p.name) + 4 for p in others]))
maxlen_0 = max(3, max(len(p.name) + 4 for p in others))
hdr = "=" * maxlen_0 + " " + "=" * 10
fmt = '%%%ds %%s ' % (maxlen_0,)
out += ['', '', hdr]
for param in others:
name = "**" + param.name.strip() + "**"
name = f"**{param.name.strip()}**"
Comment on lines -271 to +264
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SphinxDocString._str_member_list refactored with the following changes:

desc = " ".join(x.strip()
for x in param.desc).strip()
if param.type:
Expand Down Expand Up @@ -337,26 +325,30 @@ def _str_references(self):
# so we need to insert links to it
out += ['.. only:: latex', '']
items = []
for line in self['References']:
m = re.match(r'.. \[([a-z0-9._-]+)\]', line, re.I)
if m:
items.append(m.group(1))
items.extend(
m.group(1)
for line in self['References']
if (m := re.match(r'.. \[([a-z0-9._-]+)\]', line, re.I))
)

Comment on lines -340 to +333
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SphinxDocString._str_references refactored with the following changes:

out += [' ' + ", ".join([f"[{item}]_" for item in items]), '']
return out

def _str_examples(self):
examples_str = "\n".join(self['Examples'])

if (self.use_plots and re.search(IMPORT_MATPLOTLIB_RE, examples_str)
and 'plot::' not in examples_str):
out = []
out += self._str_header('Examples')
out += ['.. plot::', '']
out += self._str_indent(self['Examples'])
out += ['']
return out
else:
if (
not self.use_plots
or not re.search(IMPORT_MATPLOTLIB_RE, examples_str)
or 'plot::' in examples_str
):
return self._str_section('Examples')
out = []
out += self._str_header('Examples')
out += ['.. plot::', '']
out += self._str_indent(self['Examples'])
out += ['']
return out
Comment on lines -350 to +351
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SphinxDocString._str_examples refactored with the following changes:


def __str__(self, indent=0, func_role="obj"):
ns = {
Expand All @@ -382,7 +374,7 @@ def __str__(self, indent=0, func_role="obj"):
else self._str_member_list('Attributes'),
'methods': self._str_member_list('Methods'),
}
ns = dict((k, '\n'.join(v)) for k, v in ns.items())
ns = {k: '\n'.join(v) for k, v in ns.items()}
Comment on lines -385 to +377
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SphinxDocString.__str__ refactored with the following changes:


rendered = self.template.render(**ns)
return '\n'.join(self._str_indent(rendered.split('\n'), indent))
Expand Down
34 changes: 16 additions & 18 deletions numpydoc/numpydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,19 @@ def rename_references(app, what, name, obj, options, lines):
references = set()
for line in lines:
line = line.strip()
m = re.match(r'^\.\. +\[(%s)\]' %
app.config.numpydoc_citation_re,
line, re.I)
if m:
if m := re.match(
r'^\.\. +\[(%s)\]' % app.config.numpydoc_citation_re, line, re.I
):
references.add(m.group(1))

if references:
# we use a hash to mangle the reference name to avoid invalid names
sha = hashlib.sha256()
sha.update(name.encode('utf8'))
prefix = 'R' + sha.hexdigest()[:HASH_LEN]
prefix = f'R{sha.hexdigest()[:HASH_LEN]}'

for r in references:
new_r = prefix + '-' + r
new_r = f'{prefix}-{r}'
Comment on lines -51 to +63
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function rename_references refactored with the following changes:

for i, line in enumerate(lines):
lines[i] = lines[i].replace(f'[{r}]_',
f'[{new_r}]_')
Expand Down Expand Up @@ -227,9 +226,10 @@ def mangle_signature(app, what, name, obj, options, sig, retann):
if not hasattr(obj, '__doc__'):
return
doc = get_doc_object(obj, config={'show_class_members': False})
sig = (doc['Signature']
or _clean_text_signature(getattr(obj, '__text_signature__', None)))
if sig:
if sig := (
doc['Signature']
or _clean_text_signature(getattr(obj, '__text_signature__', None))
):
Comment on lines -230 to +232
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function mangle_signature refactored with the following changes:

sig = re.sub("^[^(]*", "", sig)
return sig, ''

Expand Down Expand Up @@ -284,9 +284,8 @@ def setup(app, get_doc_object_=get_doc_object):
app.add_domain(NumpyPythonDomain)
app.add_domain(NumpyCDomain)

metadata = {'version': __version__,
return {'version': __version__,
'parallel_read_safe': True}
return metadata
Comment on lines -287 to -289
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function setup refactored with the following changes:



def update_config(app, config=None):
Expand All @@ -309,9 +308,10 @@ def update_config(app, config=None):
if "all" in config.numpydoc_validation_checks:
block = deepcopy(config.numpydoc_validation_checks)
config.numpydoc_validation_checks = valid_error_codes - block
# Ensure that the validation check set contains only valid error codes
invalid_error_codes = config.numpydoc_validation_checks - valid_error_codes
if invalid_error_codes:
if (
invalid_error_codes := config.numpydoc_validation_checks
- valid_error_codes
):
Comment on lines -312 to +314
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function update_config refactored with the following changes:

This removes the following comments ( why? ):

# Ensure that the validation check set contains only valid error codes

raise ValueError(
f"Unrecognized validation code(s) in numpydoc_validation_checks "
f"config value: {invalid_error_codes}"
Expand All @@ -325,9 +325,7 @@ def update_config(app, config=None):
)
config.numpydoc_validation_excluder = None
if config.numpydoc_validation_exclude:
exclude_expr = re.compile(
r"|".join(exp for exp in config.numpydoc_validation_exclude)
)
exclude_expr = re.compile(r"|".join(config.numpydoc_validation_exclude))
config.numpydoc_validation_excluder = exclude_expr


Expand Down Expand Up @@ -422,7 +420,7 @@ def match_items(lines, content_old):
lines_old = content_old.data
items_old = content_old.items
j = 0
for i, line in enumerate(lines):
for line in lines:
Comment on lines -425 to +423
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function match_items refactored with the following changes:

# go to next non-empty line in old:
# line.strip() checks whether the string is all whitespace
while j < len(lines_old) - 1 and not lines_old[j].strip():
Expand Down
21 changes: 10 additions & 11 deletions numpydoc/tests/test_docscrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def line_by_line_compare(a, b, n_lines=None):
a = [l.rstrip() for l in _strip_blank_lines(a).split('\n')][:n_lines]
b = [l.rstrip() for l in _strip_blank_lines(b).split('\n')][:n_lines]
assert len(a) == len(b)
for ii, (aa, bb) in enumerate(zip(a, b)):
for aa, bb in zip(a, b):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function line_by_line_compare refactored with the following changes:

assert aa == bb


Expand Down Expand Up @@ -805,14 +805,17 @@ def test_see_also(prefix):
else:
assert desc, str([func, desc])

if func == 'func_h':
if (
func == 'func_h'
or func not in ['baz.obj_q', '~baz.obj_r']
and func != 'class_j'
and func in ['func_h1', 'func_h2']
):
assert role == 'meth'
elif func == 'baz.obj_q' or func == '~baz.obj_r':
elif func in ['baz.obj_q', '~baz.obj_r']:
assert role == 'obj'
elif func == 'class_j':
assert role == 'class'
elif func in ['func_h1', 'func_h2']:
assert role == 'meth'
Comment on lines -808 to -815
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_see_also refactored with the following changes:

else:
assert role is None, str([func, role])

Expand Down Expand Up @@ -1369,11 +1372,7 @@ def __init__(self, axis=0, doc=""):
self.__doc__ = doc

def __get__(self, obj, type):
if obj is None:
# Only instances have actual _data, not classes
return self
else:
return obj._data.axes[self.axis]
return self if obj is None else obj._data.axes[self.axis]
Comment on lines -1372 to +1375
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_nonstandard_property.SpecialProperty.__get__ refactored with the following changes:

This removes the following comments ( why? ):

# Only instances have actual _data, not classes


def __set__(self, obj, value):
obj._set_axis(self.axis, value)
Expand All @@ -1387,7 +1386,7 @@ class Dummy:


def test_args_and_kwargs():
cfg = dict()
cfg = {}
Comment on lines -1390 to +1389
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_args_and_kwargs refactored with the following changes:

doc = SphinxDocString("""
Parameters
----------
Expand Down
Loading