-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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') | ||
self.reset() | ||
|
||
def __getitem__(self, n): | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def seek_next_non_empty_line(self): | ||
for l in self[self._l:]: | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def is_empty(self): | ||
return not ''.join(self._str).strip() | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
desc = r.read_to_next_unindented_line() | ||
desc = dedent_lines(desc) | ||
desc = strip_blank_lines(desc) | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
has_returns = 'Returns' in section_names | ||
has_yields = 'Yields' in section_names | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def _str_param_list(self, name): | ||
out = [] | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
for field, items in [('Methods', self.methods), | ||
('Attributes', self.properties)]: | ||
if not self[field]: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}', ''] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def _str_field_list(self, name): | ||
return [':' + name + ':'] | ||
return [f':{name}:'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def _str_indent(self, doc, indent=4): | ||
out = [] | ||
for line in doc: | ||
out += [' '*indent + line] | ||
return out | ||
return [' '*indent + line for line in doc] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def _str_signature(self): | ||
return [''] | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
for param in self[name]: | ||
param_type = param.type | ||
if param_type and self.xref_param_type: | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
else: | ||
return name | ||
|
||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ):
|
||
desc = m.group(1).strip() | ||
else: | ||
desc = desc.partition('\n')[0] | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
desc = " ".join(x.strip() | ||
for x in param.desc).strip() | ||
if param.type: | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def __str__(self, indent=0, func_role="obj"): | ||
ns = { | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
rendered = self.template.render(**ns) | ||
return '\n'.join(self._str_indent(rendered.split('\n'), indent)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
for i, line in enumerate(lines): | ||
lines[i] = lines[i].replace(f'[{r}]_', | ||
f'[{new_r}]_') | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
sig = re.sub("^[^(]*", "", sig) | ||
return sig, '' | ||
|
||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
def update_config(app, config=None): | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ):
|
||
raise ValueError( | ||
f"Unrecognized validation code(s) in numpydoc_validation_checks " | ||
f"config value: {invalid_error_codes}" | ||
|
@@ -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 | ||
|
||
|
||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
# 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(): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
assert aa == bb | ||
|
||
|
||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
else: | ||
assert role is None, str([func, role]) | ||
|
||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ):
|
||
|
||
def __set__(self, obj, value): | ||
obj._set_axis(self.axis, value) | ||
|
@@ -1387,7 +1386,7 @@ class Dummy: | |
|
||
|
||
def test_args_and_kwargs(): | ||
cfg = dict() | ||
cfg = {} | ||
Comment on lines
-1390
to
+1389
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
doc = SphinxDocString(""" | ||
Parameters | ||
---------- | ||
|
There was a problem hiding this comment.
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:assign-if-exp
)This removes the following comments ( why? ):