Skip to content
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

Sourcery refactored master branch #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

sourcery-ai[bot]
Copy link

@sourcery-ai sourcery-ai bot commented Dec 8, 2022

Branch master refactored by Sourcery.

If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.

See our documentation here.

Run Sourcery locally

Reduce the feedback loop during development by using the Sourcery editor plugin:

Review changes via command line

To manually merge these changes, make sure you're on the master branch, then run:

git fetch origin sourcery/master
git merge --ff-only FETCH_HEAD
git reset HEAD^

Help us improve this pull request!

@sourcery-ai sourcery-ai bot requested a review from HaoZeke December 8, 2022 16:07
Copy link
Author

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Due to GitHub API limits, only the first 60 comments can be shown.

@@ -22,6 +22,7 @@
Run a Unix socket forkserver.
"""


Copy link
Author

Choose a reason for hiding this comment

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

Lines 36-40 refactored with the following changes:

Comment on lines -173 to +180
if ignore_case:
attrs = [getattr(source, key) for key in dir(source)
if key.lower() == name.lower()]

if len(attrs) > 1:
raise ValueError(f"{source.__name__} contains multiple {name} functions.")
elif len(attrs) == 1:
return attrs[0]
else:
return None
else:
if not ignore_case:
return getattr(source, name, None)
attrs = [getattr(source, key) for key in dir(source)
if key.lower() == name.lower()]

if len(attrs) > 1:
raise ValueError(f"{source.__name__} contains multiple {name} functions.")
elif len(attrs) == 1:
return attrs[0]
else:
return None
Copy link
Author

Choose a reason for hiding this comment

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

Function _get_attr refactored with the following changes:

Comment on lines -285 to +287
if info.defaults is not None:
min_args = max_args - len(info.defaults)
else:
min_args = max_args

min_args = max_args if info.defaults is None else max_args - len(info.defaults)
if info.varargs is not None:
max_args = math.inf

ok = (min_args <= max_num_args) and (min_num_args <= max_args)
if not ok:
if min_args == max_args:
args_str = min_args
else:
args_str = f"{min_args}-{max_args}"
args_str = min_args if min_args == max_args else f"{min_args}-{max_args}"
Copy link
Author

Choose a reason for hiding this comment

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

Function check_num_args refactored with the following changes:

Comment on lines -313 to +309
match = address_regex.match(result)
if match:
suspected_address = match.group(2)
if match := address_regex.match(result):
suspected_address = match[2]
# Double check this is the actual address
default_result = object.__repr__(obj)
match2 = address_regex.match(default_result)
if match2:
known_address = match2.group(2)
if match2 := address_regex.match(default_result):
known_address = match2[2]
if known_address == suspected_address:
result = match.group(1) + match.group(3)
result = match[1] + match[3]
Copy link
Author

Choose a reason for hiding this comment

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

Function _repr_no_address refactored with the following changes:

Comment on lines -364 to +362
raise ValueError("%s.param_names is not a list of strings" % (name,))
raise ValueError(f"{name}.param_names is not a list of strings")

try:
self._params = list(self._params)
except ValueError:
raise ValueError("%s.params is not a list" % (name,))
raise ValueError(f"{name}.params is not a list")

if self._params and not isinstance(self._params[0], (tuple, list)):
# Accept a single list for one parameter only
self._params = [self._params]
else:
self._params = [[item for item in entry] for entry in self._params]
self._params = [list(entry) for entry in self._params]
Copy link
Author

Choose a reason for hiding this comment

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

Function Benchmark.__init__ refactored with the following changes:

stamp = path + ".timestamp"
stamp = f"{path}.timestamp"
Copy link
Author

Choose a reason for hiding this comment

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

Function BuildCache._get_cache_dir refactored with the following changes:

if hasattr(file, 'isatty'):
return file.isatty()
return False
return file.isatty() if hasattr(file, 'isatty') else False
Copy link
Author

Choose a reason for hiding this comment

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

Function isatty refactored with the following changes:

Comment on lines -104 to -109
try:
with contextlib.suppress(UnicodeError):
fileobj.write(s)
return
except UnicodeError:
pass

Copy link
Author

Choose a reason for hiding this comment

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

Function _write_with_fallback refactored with the following changes:

Comment on lines -159 to +164
if i + 1 == len(args):
color = ''
else:
color = args[i + 1]

color = '' if i + 1 == len(args) else args[i + 1]
if color:
msg = _color_text(msg, color)
_write_with_fallback(msg, file)

_write_with_fallback(end, file)
else:
for i in range(0, len(args), 2):
msg = args[i]
_write_with_fallback(msg, file)
_write_with_fallback(end, file)

_write_with_fallback(end, file)
Copy link
Author

Choose a reason for hiding this comment

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

Function color_print refactored with the following changes:

Comment on lines -183 to +174
if x.strip() == '':
return default
return x
return default if x.strip() == '' else x
Copy link
Author

Choose a reason for hiding this comment

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

Function get_answer_default refactored with the following changes:

Comment on lines -189 to +178
if len(s) > l:
return '...' + s[-(l - 3):]
else:
return s
return f'...{s[-(l - 3):]}' if len(s) > l else s
Copy link
Author

Choose a reason for hiding this comment

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

Function truncate_left refactored with the following changes:

Comment on lines -212 to +198
if len(parts) == 1:
rest = None
else:
rest = parts[1]

rest = None if len(parts) == 1 else parts[1]
Copy link
Author

Choose a reason for hiding this comment

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

Function Log._stream_formatter refactored with the following changes:

  • Replace if statement with if expression (assign-if-exp)
  • Simplify conditional into switch-like form [×2] (switch)
  • Move assignments closer to their usage (move-assign)

Comment on lines -272 to +257
if isatty(sys.stdout):
if time.time() > self._last_dot + 1.0:
color_print('.', 'darkgrey', end='')
sys.stdout.flush()
self._last_dot = time.time()
if isatty(sys.stdout) and time.time() > self._last_dot + 1.0:
color_print('.', 'darkgrey', end='')
sys.stdout.flush()
self._last_dot = time.time()
Copy link
Author

Choose a reason for hiding this comment

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

Function Log.dot refactored with the following changes:

for key in platform_keys.keys():
for key in platform_keys:
Copy link
Author

Choose a reason for hiding this comment

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

Function iter_matrix refactored with the following changes:

Comment on lines -173 to +176
# Check if spurious keys left
remaining_keys = tuple(matrix.keys())
if remaining_keys:
raise util.UserError('Unknown keys in "matrix" configuration: {}, expected: {}'.format(
remaining_keys, matrix_types + tuple(bare_keys)))
if remaining_keys := tuple(matrix.keys()):
raise util.UserError(
f'Unknown keys in "matrix" configuration: {remaining_keys}, expected: {matrix_types + tuple(bare_keys)}'
)
Copy link
Author

Choose a reason for hiding this comment

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

Function _parse_matrix refactored with the following changes:

This removes the following comments ( why? ):

# Check if spurious keys left

Comment on lines -223 to +224
filename = os.path.join(html_dir, self.path + ".json")
filename = os.path.join(html_dir, f"{self.path}.json")
Copy link
Author

Choose a reason for hiding this comment

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

Function Graph.save refactored with the following changes:

Comment on lines -286 to +287
if self.scalar_series:
return self._steps[0]
else:
return self._steps
return self._steps[0] if self.scalar_series else self._steps
Copy link
Author

Choose a reason for hiding this comment

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

Function Graph.get_steps refactored with the following changes:

Comment on lines -299 to +300
new_steps = []

for left, right, cur_val, cur_min, cur_err in steps:
new_steps.append((x[left], x[right - 1] + 1, cur_val, cur_min, cur_err))

return new_steps
return [
(x[left], x[right - 1] + 1, cur_val, cur_min, cur_err)
for left, right, cur_val, cur_min, cur_err in steps
]
Copy link
Author

Choose a reason for hiding this comment

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

Function _compute_graph_steps refactored with the following changes:

Comment on lines -425 to +421
ys = [[None] * len(x_idx) for j in range(n_series)]
ys = [[None] * len(x_idx) for _ in range(n_series)]
Copy link
Author

Choose a reason for hiding this comment

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

Function _combine_graph_data refactored with the following changes:

path = os.path.join(root, filename)
yield path
yield os.path.join(root, filename)
Copy link
Author

Choose a reason for hiding this comment

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

Function iter_machine_files refactored with the following changes:

Comment on lines -42 to +41
if _path is None:
path = cls.get_machine_file_path()
else:
path = _path

path = cls.get_machine_file_path() if _path is None else _path
Copy link
Author

Choose a reason for hiding this comment

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

Function MachineCollection.load refactored with the following changes:

Comment on lines -61 to +57
if _path is None:
path = cls.get_machine_file_path()
else:
path = _path
if os.path.isfile(path):
d = util.load_json(path)
else:
d = {}
path = cls.get_machine_file_path() if _path is None else _path
d = util.load_json(path) if os.path.isfile(path) else {}
Copy link
Author

Choose a reason for hiding this comment

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

Function MachineCollection.save refactored with the following changes:

Comment on lines -74 to +63
if _path is None:
path = cls.get_machine_file_path()
else:
path = _path
path = cls.get_machine_file_path() if _path is None else _path
Copy link
Author

Choose a reason for hiding this comment

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

Function MachineCollection.update refactored with the following changes:

Comment on lines -131 to +117
if cls.hardcoded_machine_name:
return cls.hardcoded_machine_name
return _get_unique_machine_name()
return cls.hardcoded_machine_name or _get_unique_machine_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 Machine.get_unique_machine_name refactored with the following changes:

Comment on lines -192 to +178
d.update(kwargs)
d |= kwargs
if (not len(d) and interactive) or force_interactive:
d.update(self.generate_machine_file(use_defaults=use_defaults))
d |= self.generate_machine_file(use_defaults=use_defaults)
Copy link
Author

Choose a reason for hiding this comment

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

Function Machine.load refactored with the following changes:

Comment on lines -1015 to +1007
if m and m.group(1).strip():
return m.group(1)
if m and m[1].strip():
return m[1]
Copy link
Author

Choose a reason for hiding this comment

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

Function _format_param_value refactored with the following changes:

if extra_params is None:
extra_params = {}
else:
extra_params = dict(extra_params)

extra_params = {} if extra_params is None else dict(extra_params)
Copy link
Author

Choose a reason for hiding this comment

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

Function run_benchmarks refactored with the following changes:

Comment on lines -447 to +439
result = [None for idx in params]
result = [None for _ in params]
Copy link
Author

Choose a reason for hiding this comment

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

Function fail_benchmark refactored with the following changes:

Comment on lines -592 to +584
if cwd is None:
real_cwd = tempfile.mkdtemp()
else:
real_cwd = cwd

real_cwd = tempfile.mkdtemp() if cwd is None else cwd
Copy link
Author

Choose a reason for hiding this comment

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

Function _run_benchmark_single_param refactored with the following changes:

Comment on lines -683 to +671
env_vars.update(self.env.env_vars)
env_vars |= self.env.env_vars
Copy link
Author

Choose a reason for hiding this comment

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

Function Spawner.create_setup_cache refactored with the following changes:

@sourcery-ai
Copy link
Author

sourcery-ai bot commented Dec 8, 2022

Sourcery Code Quality Report

✅  Merging this PR will increase code quality in the affected files by 0.12%.

Quality metrics Before After Change
Complexity 19.08 😞 18.61 😞 -0.47 👍
Method Length 81.23 🙂 80.65 🙂 -0.58 👍
Working memory 7.02 🙂 7.04 🙂 0.02 👎
Quality 56.14% 🙂 56.26% 🙂 0.12% 👍
Other metrics Before After Change
Lines 20082 19778 -304
Changed files Quality Before Quality After Quality Change
asv/benchmark.py 54.76% 🙂 54.84% 🙂 0.08% 👍
asv/benchmarks.py 38.82% 😞 40.09% 😞 1.27% 👍
asv/build_cache.py 82.30% ⭐ 82.29% ⭐ -0.01% 👎
asv/console.py 69.38% 🙂 69.66% 🙂 0.28% 👍
asv/environment.py 62.30% 🙂 62.18% 🙂 -0.12% 👎
asv/feed.py 55.20% 🙂 55.22% 🙂 0.02% 👍
asv/graph.py 67.97% 🙂 68.40% 🙂 0.43% 👍
asv/machine.py 73.89% 🙂 73.74% 🙂 -0.15% 👎
asv/plugin_manager.py 85.70% ⭐ 85.66% ⭐ -0.04% 👎
asv/repo.py 86.36% ⭐ 86.93% ⭐ 0.57% 👍
asv/results.py 44.31% 😞 45.26% 😞 0.95% 👍
asv/runner.py 36.14% 😞 36.32% 😞 0.18% 👍
asv/statistics.py 59.22% 🙂 60.60% 🙂 1.38% 👍
asv/step_detect.py 43.91% 😞 43.86% 😞 -0.05% 👎
asv/util.py 48.66% 😞 48.72% 😞 0.06% 👍
asv/commands/__init__.py 74.96% 🙂 75.81% ⭐ 0.85% 👍
asv/commands/common_args.py 67.85% 🙂 68.59% 🙂 0.74% 👍
asv/commands/compare.py 17.32% ⛔ 18.17% ⛔ 0.85% 👍
asv/commands/find.py 15.45% ⛔ 16.06% ⛔ 0.61% 👍
asv/commands/machine.py 79.71% ⭐ 80.25% ⭐ 0.54% 👍
asv/commands/preview.py 77.53% ⭐ 77.53% ⭐ 0.00%
asv/commands/profiling.py 25.09% 😞 26.40% 😞 1.31% 👍
asv/commands/publish.py 18.47% ⛔ 18.61% ⛔ 0.14% 👍
asv/commands/rm.py 26.65% 😞 28.38% 😞 1.73% 👍
asv/commands/run.py 18.15% ⛔ 17.30% ⛔ -0.85% 👎
asv/commands/show.py 50.74% 🙂 51.54% 🙂 0.80% 👍
asv/commands/update.py 38.60% 😞 36.43% 😞 -2.17% 👎
asv/extern/asizeof.py 69.30% 🙂 68.83% 🙂 -0.47% 👎
asv/extern/minify_json.py 47.10% 😞 47.49% 😞 0.39% 👍
asv/plugins/conda.py 68.84% 🙂 69.44% 🙂 0.60% 👍
asv/plugins/git.py 79.88% ⭐ 79.80% ⭐ -0.08% 👎
asv/plugins/mercurial.py 83.34% ⭐ 83.07% ⭐ -0.27% 👎
asv/plugins/regressions.py 34.78% 😞 35.35% 😞 0.57% 👍
asv/plugins/summarylist.py 25.09% 😞 24.74% ⛔ -0.35% 👎
asv/plugins/virtualenv.py 72.62% 🙂 73.28% 🙂 0.66% 👍
asv/template/benchmarks/benchmarks.py 97.00% ⭐ 97.32% ⭐ 0.32% 👍
test/conftest.py 69.32% 🙂 69.67% 🙂 0.35% 👍
test/test_benchmarks.py 64.88% 🙂 65.07% 🙂 0.19% 👍
test/test_compare.py 59.01% 🙂 58.99% 🙂 -0.02% 👎
test/test_continuous.py 69.67% 🙂 69.55% 🙂 -0.12% 👎
test/test_environment.py 66.86% 🙂 66.88% 🙂 0.02% 👍
test/test_feed.py 84.32% ⭐ 84.53% ⭐ 0.21% 👍
test/test_gh_pages.py 42.65% 😞 43.73% 😞 1.08% 👍
test/test_publish.py 56.22% 🙂 57.06% 🙂 0.84% 👍
test/test_repo.py 58.70% 🙂 58.77% 🙂 0.07% 👍
test/test_results.py 60.93% 🙂 60.99% 🙂 0.06% 👍
test/test_run.py 59.59% 🙂 59.55% 🙂 -0.04% 👎
test/test_runner.py 57.10% 🙂 57.17% 🙂 0.07% 👍
test/test_statistics.py 61.73% 🙂 62.19% 🙂 0.46% 👍
test/test_step_detect.py 60.17% 🙂 60.72% 🙂 0.55% 👍
test/test_subprocess.py 81.56% ⭐ 82.14% ⭐ 0.58% 👍
test/test_update.py 45.87% 😞 46.98% 😞 1.11% 👍
test/test_workflow.py 47.78% 😞 47.74% 😞 -0.04% 👎
test/tools.py 69.04% 🙂 69.12% 🙂 0.08% 👍
test/benchmark/params_examples.py 96.69% ⭐ 96.69% ⭐ 0.00%
test/benchmark/peakmem_examples.py 95.17% ⭐ 96.84% ⭐ 1.67% 👍
test/benchmark/time_examples.py 97.51% ⭐ 98.02% ⭐ 0.51% 👍
test/benchmark/subdir/time_subdir.py 98.46% ⭐ 99.52% ⭐ 1.06% 👍

Here are some functions in these files that still need a tune-up:

File Function Complexity Length Working Memory Quality Recommendation
asv/commands/run.py Run.run 214 ⛔ 1394 ⛔ 0.00% ⛔ Refactor to reduce nesting. Try splitting into smaller methods
asv/util.py check_output 144 ⛔ 992 ⛔ 0.02% ⛔ Refactor to reduce nesting. Try splitting into smaller methods
asv/runner.py run_benchmarks 97 ⛔ 984 ⛔ 0.30% ⛔ Refactor to reduce nesting. Try splitting into smaller methods
asv/commands/compare.py Compare.print_table 89 ⛔ 1014 ⛔ 0.48% ⛔ Refactor to reduce nesting. Try splitting into smaller methods
asv/commands/find.py Find.run 68 ⛔ 732 ⛔ 1.74% ⛔ Refactor to reduce nesting. Try splitting into smaller methods

Legend and Explanation

The emojis denote the absolute quality of the code:

  • ⭐ excellent
  • 🙂 good
  • 😞 poor
  • ⛔ very poor

The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request.


Please see our documentation here for details on how these metrics are calculated.

We are actively working on this report - lots more documentation and extra metrics to come!

Help us improve this quality report!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0 participants