Sourcery Starbot ⭐ refactored Tapuzi/cpython - #1
Conversation
SourceryAI
left a comment
There was a problem hiding this comment.
Sourcery timed out performing refactorings.
Due to GitHub API limits, only the first 60 comments can be shown.
| else: | ||
| sysroot = m.group(1) | ||
| return sysroot | ||
| return '/' if m is None else m.group(1) |
There was a problem hiding this comment.
Function macosx_sdk_root refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp) - Inline variable that is immediately returned (
inline-immediately-returned-variable)
| if ext.name == '_ctypes': | ||
| if not self.configure_ctypes(ext): | ||
| self.failed.append(ext.name) | ||
| return | ||
| if ext.name == '_ctypes' and not self.configure_ctypes(ext): | ||
| self.failed.append(ext.name) | ||
| return |
There was a problem hiding this comment.
Function find_module_file.PyBuildExt.build_extension refactored with the following changes:
- Merge nested if conditions (
merge-nested-ifs)
| if not (min_db_ver <= db_ver <= max_db_ver): | ||
| return False | ||
| return True | ||
| return min_db_ver <= db_ver <= max_db_ver |
There was a problem hiding this comment.
Function find_module_file.PyBuildExt.detect_modules.allow_db_ver refactored with the following changes:
- Simplify conditional into return statement (
return-identity)
| for node in nodelist: | ||
| if node.nodeType == node.TEXT_NODE: | ||
| rc.append(node.data) | ||
| rc = [node.data for node in nodelist if node.nodeType == node.TEXT_NODE] |
There was a problem hiding this comment.
Function getText refactored with the following changes:
- Convert for loop into list comprehension (
list-comprehension)
| except ZeroDivisionError: | ||
| if i == 5: | ||
| pass | ||
| pass |
There was a problem hiding this comment.
Function test refactored with the following changes:
- Remove redundant conditional (
remove-redundant-if)
| for v in self: | ||
| if v is value or v == value: | ||
| return True | ||
| return False | ||
| return any(v is value or v == value for v in self) |
There was a problem hiding this comment.
Function Sequence.__contains__ refactored with the following changes:
- Use any() instead of for loop (
use-any)
| return sum(1 for v in self if v is value or v == value) | ||
| return sum(v is value or v == value for v in self) |
There was a problem hiding this comment.
Function Sequence.count refactored with the following changes:
- Simplify constant sum() call (
simplify-constant-sum)
| REVERSE_IMPORT_MAPPING = dict((v, k) for (k, v) in IMPORT_MAPPING.items()) | ||
| REVERSE_IMPORT_MAPPING = {v: k for (k, v) in IMPORT_MAPPING.items()} | ||
| assert len(REVERSE_IMPORT_MAPPING) == len(IMPORT_MAPPING) | ||
| REVERSE_NAME_MAPPING = dict((v, k) for (k, v) in NAME_MAPPING.items()) | ||
| REVERSE_NAME_MAPPING = {v: k for (k, v) in NAME_MAPPING.items()} |
There was a problem hiding this comment.
Lines 165-167 refactored with the following changes:
- Replace list(), dict() or set() with comprehension (
collection-builtin-to-comprehension)
| if waitflag is None or waitflag: | ||
| if not self.locked_status or waitflag is None or waitflag: | ||
| self.locked_status = True | ||
| return True | ||
| else: | ||
| if not self.locked_status: | ||
| self.locked_status = True | ||
| return True | ||
| else: | ||
| if timeout > 0: | ||
| import time | ||
| time.sleep(timeout) | ||
| return False | ||
| if timeout > 0: | ||
| import time | ||
| time.sleep(timeout) | ||
| return False |
There was a problem hiding this comment.
Function LockType.acquire refactored with the following changes:
- Merge duplicate blocks in conditional (
merge-duplicate-blocks) - Remove redundant conditional (
remove-redundant-if)
| else: | ||
| global _interrupt | ||
| _interrupt = True | ||
| global _interrupt | ||
| _interrupt = True |
There was a problem hiding this comment.
Function interrupt_main refactored with the following changes:
- Remove unnecessary else after guard condition (
remove-unnecessary-else)
| if j < n: | ||
| if rawdata[j] == ">": | ||
| return j | ||
| self.updatepos(declstartpos, j) | ||
| self.error("unexpected char after internal subset") | ||
| else: | ||
| if j >= n: | ||
| return -1 | ||
| if rawdata[j] == ">": | ||
| return j | ||
| self.updatepos(declstartpos, j) | ||
| self.error("unexpected char after internal subset") |
There was a problem hiding this comment.
Function ParserBase._parse_doctype_subset refactored with the following changes:
- Swap if/else branches (
swap-if-else-branches) - Remove unnecessary else after guard condition (
remove-unnecessary-else)
| if not os.path.isfile(executable): | ||
| for p in paths: | ||
| f = os.path.join(p, executable) | ||
| if os.path.isfile(f): | ||
| # the file exists, we have a shot at spawn working | ||
| return f | ||
| return None | ||
| else: | ||
| if os.path.isfile(executable): | ||
| return executable | ||
|
|
||
| for p in paths: | ||
| f = os.path.join(p, executable) | ||
| if os.path.isfile(f): | ||
| # the file exists, we have a shot at spawn working | ||
| return f | ||
| return None |
There was a problem hiding this comment.
Function _find_executable refactored with the following changes:
- Swap if/else branches (
swap-if-else-branches) - Remove unnecessary else after guard condition (
remove-unnecessary-else)
| # User specified different -arch flags in the environ, | ||
| # see also distutils.sysconfig | ||
| compiler_so = compiler_so + os.environ['ARCHFLAGS'].split() | ||
| compiler_so += os.environ['ARCHFLAGS'].split() |
There was a problem hiding this comment.
Function compiler_fixup refactored with the following changes:
- Replace assignment with augmented assignment (
aug-assign)
| else: | ||
| machine = 'ppc' | ||
|
|
||
| machine = 'ppc64' if sys.maxsize >= 2**32 else 'ppc' |
There was a problem hiding this comment.
Function get_platform_osx refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp)
| self._sign = 1 | ||
| else: | ||
| self._sign = 0 | ||
| self._sign = 1 if m.group('sign') == "-" else 0 |
There was a problem hiding this comment.
Function Decimal.__new__ refactored with the following changes:
- Split conditional into multiple branches (
split-or-ifs) - Merge duplicate blocks in conditional (
merge-duplicate-blocks) - Remove redundant conditional (
remove-redundant-if) - Replace if statement with if expression (
assign-if-exp)
| for dig in self._int: | ||
| if dig not in '01': | ||
| return False | ||
| return True | ||
| return all(dig in '01' for dig in self._int) |
There was a problem hiding this comment.
Function Decimal._islogical refactored with the following changes:
- Use any() instead of for loop (
use-any) - Invert any/all to simplify comparisons (
invert-any-all)
| result = "".join([str(int(a)&int(b)) for a,b in zip(opa,opb)]) | ||
| result = "".join(str(int(a)&int(b)) for a,b in zip(opa,opb)) |
There was a problem hiding this comment.
Function Decimal.logical_and refactored with the following changes:
- Replace unneeded comprehension with generator (
comprehension-to-generator)
| result = "".join([str(int(a)|int(b)) for a,b in zip(opa,opb)]) | ||
| result = "".join(str(int(a)|int(b)) for a,b in zip(opa,opb)) |
There was a problem hiding this comment.
Function Decimal.logical_or refactored with the following changes:
- Replace unneeded comprehension with generator (
comprehension-to-generator)
| result = "".join([str(int(a)^int(b)) for a,b in zip(opa,opb)]) | ||
| result = "".join(str(int(a)^int(b)) for a,b in zip(opa,opb)) |
There was a problem hiding this comment.
Function Decimal.logical_xor refactored with the following changes:
- Replace unneeded comprehension with generator (
comprehension-to-generator)
| elif reading: | ||
| buffer = BufferedReader(raw, buffering) | ||
| else: | ||
| raise ValueError("unknown mode: %r" % mode) |
There was a problem hiding this comment.
Function open refactored with the following changes:
- Remove redundant conditional (
remove-redundant-if)
| if read1: | ||
| data = self.read1(len(b)) | ||
| else: | ||
| data = self.read(len(b)) | ||
| data = self.read1(len(b)) if read1 else self.read(len(b)) |
There was a problem hiding this comment.
Function BufferedIOBase._readinto refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp)
| os.set_inheritable(fd, False) | ||
|
|
||
| os.set_inheritable(owned_fd, False) |
There was a problem hiding this comment.
Function FileIO.__init__ refactored with the following changes:
- Use previously assigned local variable (
use-assigned-variable)
| if encoding is None: | ||
| try: | ||
| import locale | ||
| except ImportError: | ||
| # Importing locale may fail if Python is being built | ||
| encoding = "ascii" | ||
| else: | ||
| encoding = locale.getpreferredencoding(False) | ||
| if encoding is None: | ||
| try: | ||
| import locale | ||
| except ImportError: | ||
| # Importing locale may fail if Python is being built | ||
| encoding = "ascii" | ||
| else: | ||
| encoding = locale.getpreferredencoding(False) |
There was a problem hiding this comment.
Function TextIOWrapper.__init__ refactored with the following changes:
- Hoist conditional out of nested conditional (
hoist-if-from-if)
| if encoding is None: | ||
| errors = self._errors | ||
| else: | ||
| errors = 'strict' | ||
| errors = self._errors if encoding is None else 'strict' |
There was a problem hiding this comment.
Function TextIOWrapper.reconfigure refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp)
| skip_back = skip_back * 2 | ||
| skip_back *= 2 |
There was a problem hiding this comment.
Function TextIOWrapper.tell refactored with the following changes:
- Replace assignment with augmented assignment (
aug-assign)
| invocation_length = max([len(s) for s in invocations]) | ||
| invocation_length = max(len(s) for s in invocations) |
There was a problem hiding this comment.
Function HelpFormatter.add_argument refactored with the following changes:
- Replace unneeded comprehension with generator (
comprehension-to-generator)
| return ''.join([part | ||
| for part in part_strings | ||
| if part and part is not SUPPRESS]) | ||
| return ''.join(part for part in part_strings | ||
| if part and part is not SUPPRESS) |
There was a problem hiding this comment.
Function HelpFormatter._join_parts refactored with the following changes:
- Replace unneeded comprehension with generator (
comprehension-to-generator)
| if prefix is not None: | ||
| line_len = len(prefix) - 1 | ||
| else: | ||
| line_len = len(indent) - 1 | ||
| line_len = len(prefix) - 1 if prefix is not None else len(indent) - 1 |
There was a problem hiding this comment.
Function HelpFormatter._format_usage.get_lines refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp)
| result = '%s' % get_metavar(1) | ||
| return '%s' % get_metavar(1) | ||
| elif action.nargs == OPTIONAL: | ||
| result = '[%s]' % get_metavar(1) | ||
| return '[%s]' % get_metavar(1) | ||
| elif action.nargs == ZERO_OR_MORE: | ||
| result = '[%s [%s ...]]' % get_metavar(2) | ||
| return '[%s [%s ...]]' % get_metavar(2) | ||
| elif action.nargs == ONE_OR_MORE: | ||
| result = '%s [%s ...]' % get_metavar(2) | ||
| return '%s [%s ...]' % get_metavar(2) | ||
| elif action.nargs == REMAINDER: | ||
| result = '...' | ||
| return '...' | ||
| elif action.nargs == PARSER: | ||
| result = '%s ...' % get_metavar(1) | ||
| return '%s ...' % get_metavar(1) | ||
| elif action.nargs == SUPPRESS: | ||
| result = '' | ||
| return '' | ||
| else: | ||
| formats = ['%s' for _ in range(action.nargs)] | ||
| result = ' '.join(formats) % get_metavar(action.nargs) | ||
| return result | ||
| return ' '.join(formats) % get_metavar(action.nargs) |
There was a problem hiding this comment.
Function HelpFormatter._format_args refactored with the following changes:
- Lift return into if (
lift-return-into-if)
| choices_str = ', '.join([str(c) for c in params['choices']]) | ||
| choices_str = ', '.join(str(c) for c in params['choices']) |
There was a problem hiding this comment.
Function HelpFormatter._expand_help refactored with the following changes:
- Replace unneeded comprehension with generator (
comprehension-to-generator)
Thanks for starring sourcery-ai/sourcery ✨ 🌟 ✨
Here's your pull request refactoring your most popular Python repo.
If you want Sourcery to refactor all your Python repos and incoming pull requests install our bot.
Review changes via command line
To manually merge these changes, make sure you're on the
masterbranch, then run: