Conversation
| readme = f.read() | ||
|
|
||
|
|
||
| readme = pathlib.Path(str(_ROOT / 'README.rst')).read_text() |
There was a problem hiding this comment.
Lines 68-71 refactored with the following changes:
- Simplify basic file reads with
pathlib(path-read)
| if git.returncode == 0: | ||
| commitish = git.stdout.strip().decode('ascii') | ||
| else: | ||
| commitish = 'unknown' | ||
|
|
||
| return commitish | ||
| return git.stdout.strip().decode('ascii') if git.returncode == 0 else 'unknown' |
There was a problem hiding this comment.
Function git_commitish refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp) - Inline variable that is immediately returned (
inline-immediately-returned-variable)
| with open(str(filename)) as f: | ||
| content = f.read() | ||
|
|
||
| content = pathlib.Path(str(filename)).read_text() | ||
| version_re = r"(.*__version__\s*=\s*)'[^']+'(.*)" | ||
| repl = r"\1'{}'\2".format(self.distribution.metadata.version) | ||
| repl = f"\1'{self.distribution.metadata.version}'\2" |
There was a problem hiding this comment.
Function VersionMixin._fix_version refactored with the following changes:
- Simplify basic file reads with
pathlib(path-read) - Replace call to format with f-string (
use-fstring-for-formatting)
| cfile = prefix + '.c' | ||
| cfile = f'{prefix}.c' | ||
|
|
||
| if os.path.exists(cfile) and not self.cython_always: | ||
| extension.sources[i] = cfile | ||
| else: | ||
| if os.path.exists(cfile): | ||
| cfiles[cfile] = os.path.getmtime(cfile) | ||
| else: | ||
| cfiles[cfile] = 0 | ||
| cfiles[cfile] = os.path.getmtime(cfile) if os.path.exists(cfile) else 0 |
There was a problem hiding this comment.
Function build_ext.finalize_options refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation) - Replace if statement with if expression (
assign-if-exp) - Replace call to format with f-string [×2] (
use-fstring-for-formatting)
| username = '@{}'.format(commit['author']['login']) | ||
| username = f"@{commit['author']['login']}" | ||
| else: | ||
| username = commit['commit']['author']['name'] | ||
| sha = commit["sha"][:8] | ||
|
|
||
| m = re.search(r'\#(?P<num>\d+)\b', message) | ||
| if m: | ||
| issue_num = m.group('num') | ||
| else: | ||
| issue_num = None | ||
|
|
||
| issue_num = ( | ||
| m['num'] | ||
| if (m := re.search(r'\#(?P<num>\d+)\b', message)) | ||
| else None | ||
| ) |
There was a problem hiding this comment.
Function main refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression) - Replace call to format with f-string (
use-fstring-for-formatting) - Replace m.group(x) with m[x] for re.Match objects (
use-getitem-for-re-match-groups) - Replace if statement with if expression (
assign-if-exp)
| if not self._pg_bin_dir: | ||
| raise ClusterError( | ||
| 'pg_config output did not provide the BINDIR value') | ||
| if not self._pg_bin_dir: | ||
| raise ClusterError( | ||
| 'pg_config output did not provide the BINDIR value') |
There was a problem hiding this comment.
Function Cluster._init_env refactored with the following changes:
- Hoist conditional out of nested conditional (
hoist-if-from-if)
| sockdir = lines[4] | ||
| hostaddr = lines[5] | ||
|
|
||
| if sockdir: | ||
| if sockdir := lines[4]: |
There was a problem hiding this comment.
Function Cluster._connection_addr_from_pidfile refactored with the following changes:
- Move assignment closer to its usage within a block (
move-assign-in-block) - Use named expression to simplify assignment and conditional (
use-named-expression)
| for i in range(timeout): | ||
| for _ in range(timeout): |
There was a problem hiding this comment.
Function Cluster._test_connection refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore)
| else: | ||
| config = {} | ||
| config = {} | ||
|
|
||
| for line in stdout.splitlines(): | ||
| k, eq, v = line.decode('utf-8').partition('=') | ||
| if eq: | ||
| config[k.strip().lower()] = v.strip() | ||
| for line in stdout.splitlines(): | ||
| k, eq, v = line.decode('utf-8').partition('=') | ||
| if eq: | ||
| config[k.strip().lower()] = v.strip() | ||
|
|
||
| return config | ||
| return config |
There was a problem hiding this comment.
Function Cluster._run_pg_config refactored with the following changes:
- Swap if/else branches [×2] (
swap-if-else-branches) - Remove unnecessary else after guard condition (
remove-unnecessary-else)
| pg_install = ( | ||
| os.environ.get('PGINSTALLATION') | ||
| or os.environ.get('PGBIN') | ||
| ) | ||
| if pg_install: | ||
| if pg_install := ( | ||
| os.environ.get('PGINSTALLATION') or os.environ.get('PGBIN') | ||
| ): |
There was a problem hiding this comment.
Function Cluster._find_pg_config refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression)
| 'could not find {} executable: '.format(binary) + | ||
| '{!r} does not exist or is not a file'.format(bpath)) | ||
| ( | ||
| f'could not find {binary} executable: ' | ||
| + '{!r} does not exist or is not a file'.format(bpath) | ||
| ) | ||
| ) |
There was a problem hiding this comment.
Function Cluster._find_pg_binary refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting)
| r = ctypes.windll.shell32.SHGetFolderPathW(0, CSIDL_APPDATA, 0, 0, buf) | ||
| if r: | ||
| if r := ctypes.windll.shell32.SHGetFolderPathW( | ||
| 0, CSIDL_APPDATA, 0, 0, buf | ||
| ): |
There was a problem hiding this comment.
Function get_pg_home_directory refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression)
| PGPASSFILE = 'pgpass.conf' | ||
| else: | ||
| PGPASSFILE = '.pgpass' | ||
| PGPASSFILE = 'pgpass.conf' if _system == 'Windows' else '.pgpass' |
There was a problem hiding this comment.
Lines 75-78 refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp)
| def _read_password_file(passfile: pathlib.Path) \ | ||
| -> typing.List[typing.Tuple[str, ...]]: | ||
| def _read_password_file(passfile: pathlib.Path) -> typing.List[typing.Tuple[str, ...]]: |
There was a problem hiding this comment.
Function _read_password_file refactored with the following changes:
- Merge nested if conditions (
merge-nested-ifs)
| if phost != '*' and phost != host: | ||
| if phost not in ['*', host]: | ||
| continue | ||
| if pport != '*' and pport != str(port): | ||
| if pport not in ['*', str(port)]: | ||
| continue | ||
| if pdatabase != '*' and pdatabase != database: | ||
| if pdatabase not in ['*', database]: | ||
| continue | ||
| if puser != '*' and puser != user: | ||
| if puser not in ['*', user]: |
There was a problem hiding this comment.
Function _read_password_from_pgpass refactored with the following changes:
- Replace multiple comparisons of same variable with
inoperator [×4] (merge-comparisons)
| tabname = utils._quote_ident(schema_name) + '.' + tabname | ||
| tabname = f'{utils._quote_ident(schema_name)}.{tabname}' | ||
|
|
||
| if columns: | ||
| cols = '({})'.format( | ||
| ', '.join(utils._quote_ident(c) for c in columns)) | ||
| cols = f"({', '.join(utils._quote_ident(c) for c in columns)})" |
There was a problem hiding this comment.
Function Connection.copy_from_table refactored with the following changes:
- Use f-string instead of string concatenation [×2] (
use-fstring-for-concatenation) - Replace call to format with f-string (
use-fstring-for-formatting)
| tabname = utils._quote_ident(schema_name) + '.' + tabname | ||
| tabname = f'{utils._quote_ident(schema_name)}.{tabname}' | ||
|
|
||
| if columns: | ||
| cols = '({})'.format( | ||
| ', '.join(utils._quote_ident(c) for c in columns)) | ||
| cols = f"({', '.join(utils._quote_ident(c) for c in columns)})" |
There was a problem hiding this comment.
Function Connection.copy_to_table refactored with the following changes:
- Use f-string instead of string concatenation [×2] (
use-fstring-for-concatenation) - Replace call to format with f-string (
use-fstring-for-formatting)
| tabname = utils._quote_ident(schema_name) + '.' + tabname | ||
| tabname = f'{utils._quote_ident(schema_name)}.{tabname}' | ||
|
|
||
| if columns: | ||
| col_list = ', '.join(utils._quote_ident(c) for c in columns) | ||
| cols = '({})'.format(col_list) | ||
| cols = f'({col_list})' |
There was a problem hiding this comment.
Function Connection.copy_records_to_table refactored with the following changes:
- Use f-string instead of string concatenation [×2] (
use-fstring-for-concatenation) - Replace call to format with f-string (
use-fstring-for-formatting)
| opts.append('{} {}'.format(k.upper(), v)) | ||
| opts.append(f'{k.upper()} {v}') | ||
|
|
||
| if opts: | ||
| return '(' + ', '.join(opts) + ')' | ||
| else: | ||
| return '' | ||
| return '(' + ', '.join(opts) + ')' if opts else '' |
There was a problem hiding this comment.
Function Connection._format_copy_opts refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting) - Replace if statement with if expression (
assign-if-exp)
| 'cannot use custom codec on non-scalar type {}.{}'.format( | ||
| schema, typename)) | ||
| f'cannot use custom codec on non-scalar type {schema}.{typename}' | ||
| ) |
There was a problem hiding this comment.
Function Connection.set_type_codec refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting)
| 'cannot alias non-scalar type {}.{}'.format( | ||
| schema, typename)) | ||
| f'cannot alias non-scalar type {schema}.{typename}' | ||
| ) |
There was a problem hiding this comment.
Function Connection.set_builtin_type_codec refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting)
| if self._proxy is None: | ||
| con_ref = self | ||
| else: | ||
| # `_proxy` is not None when the connection is a member | ||
| # of a connection pool. Which means that the user is working | ||
| # with a `PoolConnectionProxy` instance, and expects to see it | ||
| # (and not the actual Connection) in their event callbacks. | ||
| con_ref = self._proxy | ||
| return con_ref | ||
| return self if self._proxy is None else self._proxy |
There was a problem hiding this comment.
Function Connection._unwrap refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp) - Inline variable that is immediately returned (
inline-immediately-returned-variable)
This removes the following comments ( why? ):
# of a connection pool. Which means that the user is working
# with a `PoolConnectionProxy` instance, and expects to see it
# `_proxy` is not None when the connection is a member
# (and not the actual Connection) in their event callbacks.
| elif hasattr(connection_settings, 'crdb_version'): | ||
| elif hasattr(connection_settings, 'crdb_version') or hasattr( | ||
| connection_settings, 'crate_version' | ||
| ): | ||
| # CockroachDB detected. | ||
| advisory_locks = False | ||
| notifications = False | ||
| plpgsql = False | ||
| sql_reset = False | ||
| sql_close_all = False | ||
| elif hasattr(connection_settings, 'crate_version'): | ||
| # CrateDB detected. |
There was a problem hiding this comment.
Function _detect_server_capabilities refactored with the following changes:
- Merge duplicate blocks in conditional (
merge-duplicate-blocks) - Remove redundant conditional (
remove-redundant-if)
This removes the following comments ( why? ):
# CrateDB detected.
| 'cannot call {}.{}(): ' | ||
| 'the underlying connection has been released back ' | ||
| 'to the pool'.format(self.__class__.__name__, meth_name)) | ||
| f'cannot call {self.__class__.__name__}.{meth_name}(): the underlying connection has been released back to the pool' | ||
| ) | ||
|
|
||
| if self._connection.is_closed(): | ||
| raise exceptions.InterfaceError( | ||
| 'cannot call {}.{}(): ' | ||
| 'the underlying connection is closed'.format( | ||
| self.__class__.__name__, meth_name)) | ||
| f'cannot call {self.__class__.__name__}.{meth_name}(): the underlying connection is closed' | ||
| ) |
There was a problem hiding this comment.
Function ConnectionResource._check_conn_validity refactored with the following changes:
- Replace call to format with f-string [×2] (
use-fstring-for-formatting)
| buffer = await protocol.bind(self._state, self._args, | ||
| self._portal_name, | ||
| timeout) | ||
| return buffer | ||
| return await protocol.bind( | ||
| self._state, self._args, self._portal_name, timeout | ||
| ) |
There was a problem hiding this comment.
Function BaseCursor._bind refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| if self._nested: | ||
| query = 'ROLLBACK TO {};'.format(self._id) | ||
| else: | ||
| query = 'ROLLBACK;' | ||
|
|
||
| query = f'ROLLBACK TO {self._id};' if self._nested else 'ROLLBACK;' |
There was a problem hiding this comment.
Function Transaction.__rollback refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp) - Replace call to format with f-string (
use-fstring-for-formatting)
| attrs = [] | ||
| attrs.append('state:{}'.format(self._state.name.lower())) | ||
|
|
||
| attrs = [f'state:{self._state.name.lower()}'] |
There was a problem hiding this comment.
Function Transaction.__repr__ refactored with the following changes:
- Merge append into list declaration (
merge-list-append) - Replace call to format with f-string (
use-fstring-for-formatting)
| if not isinstance(other, Range): | ||
| return NotImplemented | ||
|
|
||
| return ( | ||
| self._lower, | ||
| self._upper, | ||
| self._lower_inc, | ||
| self._upper_inc, | ||
| self._empty | ||
| ) == ( | ||
| other._lower, | ||
| other._upper, | ||
| other._lower_inc, | ||
| other._upper_inc, | ||
| other._empty | ||
| ( | ||
| self._lower, | ||
| self._upper, | ||
| self._lower_inc, | ||
| self._upper_inc, | ||
| self._empty, | ||
| ) | ||
| == ( | ||
| other._lower, | ||
| other._upper, | ||
| other._lower_inc, | ||
| other._upper_inc, | ||
| other._empty, | ||
| ) | ||
| if isinstance(other, Range) | ||
| else NotImplemented |
There was a problem hiding this comment.
Function Range.__eq__ 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) - Swap if/else branches of if expression to remove negation (
swap-if-expression)
| if self._lower is None or not self._lower_inc: | ||
| lb = '(' | ||
| else: | ||
| lb = '[' | ||
|
|
||
| lb = '(' if self._lower is None or not self._lower_inc else '[' | ||
| if self._lower is not None: | ||
| lb += repr(self._lower) | ||
|
|
||
| if self._upper is not None: | ||
| ub = repr(self._upper) | ||
| else: | ||
| ub = '' | ||
|
|
||
| if self._upper is None or not self._upper_inc: | ||
| ub += ')' | ||
| else: | ||
| ub += ']' | ||
|
|
||
| desc = '{}, {}'.format(lb, ub) | ||
| ub = (repr(self._upper) if self._upper is not None else '') + ( | ||
| ')' if self._upper is None or not self._upper_inc else ']' | ||
| ) | ||
| desc = f'{lb}, {ub}' | ||
|
|
||
| return '<Range {}>'.format(desc) | ||
| return f'<Range {desc}>' |
There was a problem hiding this comment.
Function Range.__repr__ refactored with the following changes:
- Replace if statement with if expression [×3] (
assign-if-exp) - Replace call to format with f-string [×2] (
use-fstring-for-formatting) - Replace assignment and augmented assignment with single assignment (
merge-assign-and-aug-assign)
|
|
||
| def _quote_ident(ident): | ||
| return '"{}"'.format(ident.replace('"', '""')) | ||
| return f""""{ident.replace('"', '""')}\"""" |
There was a problem hiding this comment.
Function _quote_ident refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting)
Sourcery Code Quality Report✅ Merging this PR will increase code quality in the affected files by 0.07%.
Here are some functions in these files that still need a tune-up:
Legend and ExplanationThe emojis denote the absolute quality of the code:
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! |
Branch
masterrefactored 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
masterbranch, then run:Help us improve this pull request!