-
Notifications
You must be signed in to change notification settings - Fork 0
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 main branch #1
Conversation
if hasattr(file, "isatty"): | ||
return file.isatty() | ||
return False | ||
return file.isatty() if hasattr(file, "isatty") else False |
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 isatty
refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else
) - Replace if statement with if expression (
assign-if-exp
)
try: | ||
with contextlib.suppress(UnicodeError): | ||
fileobj.write(s) | ||
return | ||
except UnicodeError: | ||
pass | ||
|
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 _write_with_fallback
refactored with the following changes:
- Use
contextlib
'ssuppress
method to silence an error (use-contextlib-suppress
)
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) |
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 color_print
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
) - Hoist repeated code outside conditional statement (
hoist-statement-from-if
)
if x.strip() == "": | ||
return default | ||
return x | ||
return default if x.strip() == "" else x |
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 get_answer_default
refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else
) - Replace if statement with if expression (
assign-if-exp
)
if len(s) > l: | ||
return "..." + s[-(l - 3) :] | ||
else: | ||
return s | ||
return f"...{s[-(l - 3):]}" if len(s) > l else s |
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 truncate_left
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
) - Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
else: | ||
str_err = human_float(err / units[i][1], 1, truncate_small=2) | ||
return f"{str_time:s}±{str_err:s}{units[i][0]}" | ||
str_err = human_float(err / units[i][1], 1, truncate_small=2) | ||
return f"{str_time:s}±{str_err:s}{units[i][0]}" |
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 human_time
refactored with the following changes:
- Remove unnecessary else after guard condition (
remove-unnecessary-else
)
@@ -32,6 +32,7 @@ | |||
adding a new submodule with an "export_as_benchmark" attribute. | |||
""" | |||
|
|||
|
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.
Lines 50-51
refactored with the following changes:
- Replace a for append loop with list extend (
for-append-to-extend
) - Simplify generator expression (
simplify-generator
)
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 |
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 _get_attr
refactored with the following changes:
- Swap if/else branches (
swap-if-else-branches
) - Remove unnecessary else after guard condition (
remove-unnecessary-else
)
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}" |
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 check_num_args
refactored with the following changes:
- Swap if/else branches (
swap-if-else-branches
) - Replace if statement with if expression [×2] (
assign-if-exp
)
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] |
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 _repr_no_address
refactored with the following changes:
- Use named expression to simplify assignment and conditional [×2] (
use-named-expression
) - Replace m.group(x) with m[x] for re.Match objects [×4] (
use-getitem-for-re-match-groups
)
self._params = [[item for item in entry] for entry in self._params] | ||
self._params = [list(entry) for entry in self._params] |
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 Benchmark.__init__
refactored with the following changes:
- Replace identity comprehension with call to collection constructor (
identity-comprehension
) - Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
root, self.name + ": setup_cache", self._setup_cache, 0 | ||
root, f"{self.name}: setup_cache", self._setup_cache, 0 | ||
) | ||
max_num_args += 1 | ||
|
||
for setup in self._setups: | ||
ok = ok and check_num_args( | ||
root, self.name + ": setup", setup, min_num_args, max_num_args | ||
root, f"{self.name}: setup", setup, min_num_args, max_num_args | ||
) | ||
|
||
ok = ok and check_num_args( | ||
root, self.name + ": call", self.func, min_num_args, max_num_args | ||
root, f"{self.name}: call", self.func, min_num_args, max_num_args | ||
) | ||
|
||
for teardown in self._teardowns: | ||
ok = ok and check_num_args( | ||
root, self.name + ": teardown", teardown, min_num_args, max_num_args | ||
root, | ||
f"{self.name}: teardown", | ||
teardown, | ||
min_num_args, | ||
max_num_args, |
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 Benchmark.check
refactored with the following changes:
- Use f-string instead of string concatenation [×4] (
use-fstring-for-concatenation
)
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.
Actually looks quite good.
MAINT: 'Refactored by Sourcery' (#1)
Branch
main
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
main
branch, then run:Help us improve this pull request!