Repository navigation
Sourcery refactored main branch - #1
sourcery-ai[bot] wants to merge 1 commit into
Conversation
| def sqlglotrs_version(): | ||
| with open("sqlglotrs/Cargo.toml") as fd: | ||
| for line in fd.readlines(): | ||
| for line in fd: |
There was a problem hiding this comment.
Function sqlglotrs_version refactored with the following changes:
- Iterate over files directly rather than using readlines() (
use-file-iterator)
|
|
||
| crazy = "SELECT 1+" | ||
| crazy += "+".join(str(i) for i in range(500)) | ||
| crazy = "SELECT 1+" + "+".join(str(i) for i in range(500)) |
There was a problem hiding this comment.
Lines 60-61 refactored with the following changes:
- Replace assignment and augmented assignment with single assignment (
merge-assign-and-aug-assign)
| return f" ({str(column / row['sqlglot'])[0:5]})" | ||
| return f" ({str(column / row['sqlglot'])[:5]})" |
There was a problem hiding this comment.
Function diff refactored with the following changes:
- Replace a[0:x] with a[:x] and a[x:len(a)] with a[x:] (
remove-redundant-slice-index)
| row[lib] = np.mean(timeit.repeat(lambda: globals()[lib + "_parse"](sql), number=3)) | ||
| row[lib] = np.mean( | ||
| timeit.repeat(lambda: globals()[f"{lib}_parse"](sql), number=3) | ||
| ) |
There was a problem hiding this comment.
Lines 222-238 refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation) - Remove unnecessary calls to
enumeratewhen the index is not used (remove-unused-enumerate) - Replace a[0:x] with a[:x] and a[x:len(a)] with a[x:] [×2] (
remove-redundant-slice-index) - Replace a for append loop with list extend (
for-append-to-extend)
| self._bigram_histo_cache: t.Dict[int, t.DefaultDict[str, int]] = {} | ||
|
|
||
| matching_set = self._compute_matching_set() | {(s, t) for s, t in pre_matched_nodes.items()} | ||
| matching_set = self._compute_matching_set() | set(pre_matched_nodes.items()) |
There was a problem hiding this comment.
Function ChangeDistiller.diff refactored with the following changes:
- Replace identity comprehension with call to collection constructor (
identity-comprehension)
| for node, _, _ in self.dfs(prune=lambda n, p, *_: p and not type(n) is self.__class__): | ||
| if not type(node) is self.__class__: | ||
| for node, _, _ in self.dfs(prune=lambda n, p, *_: p and type(n) is not self.__class__): | ||
| if type(node) is not self.__class__: |
There was a problem hiding this comment.
Function Expression.flatten refactored with the following changes:
- Simplify logical expression using De Morgan identities [×2] (
de-morgan)
| errors: t.List[str] = [] | ||
|
|
||
| for k in self.args: | ||
| if k not in self.arg_types: | ||
| errors.append(f"Unexpected keyword: '{k}' for {self.__class__}") | ||
| errors: t.List[str] = [ | ||
| f"Unexpected keyword: '{k}' for {self.__class__}" | ||
| for k in self.args | ||
| if k not in self.arg_types | ||
| ] |
There was a problem hiding this comment.
Function Expression.error_messages refactored with the following changes:
- Convert for loop into list comprehension (
list-comprehension)
| if not with_: | ||
| return [] | ||
| return with_.expressions | ||
| return [] if not with_ else with_.expressions |
There was a problem hiding this comment.
Function DDL.ctes 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)
| property_cls = cls.NAME_TO_PROPERTY.get(key.upper()) | ||
| if property_cls: | ||
| if property_cls := cls.NAME_TO_PROPERTY.get(key.upper()): |
There was a problem hiding this comment.
Function Properties.from_dict refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression)
| if not with_: | ||
| return [] | ||
| return with_.expressions | ||
| return [] if not with_ else with_.expressions |
There was a problem hiding this comment.
Function Subqueryable.ctes 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 isinstance(self.this, Func): | ||
| return "" | ||
| return self.this.name | ||
| return "" if isinstance(self.this, Func) else self.this.name |
There was a problem hiding this comment.
Function Table.name 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)
| col = column(*reversed(parts[0:4]), *parts[4:], copy=copy) # type: ignore | ||
| alias = self.args.get("alias") | ||
| if alias: | ||
| col = column(*reversed(parts[:4]), *parts[4:], copy=copy) | ||
| if alias := self.args.get("alias"): |
There was a problem hiding this comment.
Function Table.to_column refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression) - Replace a[0:x] with a[:x] and a[x:len(a)] with a[x:] (
remove-redundant-slice-index)
This removes the following comments ( why? ):
# type: ignore
| def build(self, expressions: t.Sequence[Expression]) -> Dot: | ||
| def build(cls, expressions: t.Sequence[Expression]) -> Dot: | ||
| """Build a Dot object with a sequence of expressions.""" | ||
| if len(expressions) < 2: | ||
| raise ValueError(f"Dot requires >= 2 expressions.") | ||
| raise ValueError("Dot requires >= 2 expressions.") |
There was a problem hiding this comment.
Function Dot.build refactored with the following changes:
- The first argument to class methods should be
cls(class-method-first-arg-name) - Replace f-string with no interpolated values with string (
remove-redundant-fstring)
| if copy: | ||
| return sql_or_expression.copy() | ||
| return sql_or_expression | ||
|
|
||
| return sql_or_expression.copy() if copy else sql_or_expression | ||
| if sql_or_expression is None: | ||
| raise ParseError(f"SQL cannot be None") | ||
| raise ParseError("SQL cannot be None") |
There was a problem hiding this comment.
Function maybe_parse refactored with the following changes:
- Swap positions of nested conditionals (
swap-nested-ifs) - Lift code into else after jump in control flow (
reintroduce-else) - Hoist nested repeated code outside conditional statements (
hoist-similar-statement-from-if) - Replace f-string with no interpolated values with string (
remove-redundant-fstring) - Replace if statement with if expression (
assign-if-exp)
| interval_parts = INTERVAL_STRING_RE.match(interval) # type: ignore | ||
|
|
||
| if not interval_parts: | ||
| if interval_parts := INTERVAL_STRING_RE.match(interval): | ||
| return Interval( | ||
| this=Literal.string(interval_parts.group(1)), | ||
| unit=Var(this=interval_parts.group(2).upper()), | ||
| ) | ||
| else: | ||
| raise ValueError("Invalid interval string.") | ||
|
|
||
| return Interval( | ||
| this=Literal.string(interval_parts.group(1)), | ||
| unit=Var(this=interval_parts.group(2).upper()), | ||
| ) |
There was a problem hiding this comment.
Function to_interval refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression) - Lift code into else after jump in control flow (
reintroduce-else) - Swap if/else branches (
swap-if-else-branches)
This removes the following comments ( why? ):
# type: ignore
| expressions = self.expressions(properties, sep=sep, indent=False) | ||
| if expressions: | ||
| if expressions := self.expressions(properties, sep=sep, indent=False): |
There was a problem hiding this comment.
Function Generator.properties refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression)
| when = self.sql(expression, "when") | ||
| if when: | ||
| if when := self.sql(expression, "when"): |
There was a problem hiding this comment.
Function Generator.table_sql refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression)
| else: | ||
| cube_sql = self.expressions(expression, key="cube", indent=False) | ||
| cube_sql = f"{self.seg('CUBE')} {self.wrap(cube_sql)}" if cube_sql else "" | ||
| cube_sql = self.expressions(expression, key="cube", indent=False) | ||
| cube_sql = f"{self.seg('CUBE')} {self.wrap(cube_sql)}" if cube_sql else "" | ||
|
|
||
| rollup = expression.args.get("rollup", []) | ||
| if seq_get(rollup, 0) is True: | ||
| return f"{group_by}{self.seg('WITH ROLLUP')}" | ||
| else: | ||
| rollup_sql = self.expressions(expression, key="rollup", indent=False) | ||
| rollup_sql = f"{self.seg('ROLLUP')} {self.wrap(rollup_sql)}" if rollup_sql else "" | ||
| rollup_sql = self.expressions(expression, key="rollup", indent=False) | ||
| rollup_sql = f"{self.seg('ROLLUP')} {self.wrap(rollup_sql)}" if rollup_sql else "" |
There was a problem hiding this comment.
Function Generator.group_sql refactored with the following changes:
- Remove unnecessary else after guard condition [×2] (
remove-unnecessary-else)
| if using: | ||
| on_sql = f"{space}USING ({on_sql})" | ||
| else: | ||
| on_sql = f"{space}ON {on_sql}" | ||
| on_sql = f"{space}USING ({on_sql})" if using else f"{space}ON {on_sql}" |
There was a problem hiding this comment.
Function Generator.join_sql refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp)
| if self.UNNEST_WITH_ORDINALITY: | ||
| if alias and isinstance(offset, exp.Expression): | ||
| if alias and isinstance(offset, exp.Expression): | ||
| if self.UNNEST_WITH_ORDINALITY: |
There was a problem hiding this comment.
Function Generator.unnest_sql refactored with the following changes:
- Swap positions of nested conditionals (
swap-nested-ifs) - Merge else clause's nested if statement into elif (
merge-else-if-into-elif)
| default = self.sql(expression, "default") | ||
|
|
||
| if default: | ||
| if default := self.sql(expression, "default"): |
There was a problem hiding this comment.
Function Generator.case_sql refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression)
| nested_schema = self.sql(expression, "nested_schema") | ||
|
|
||
| if nested_schema: | ||
| if nested_schema := self.sql(expression, "nested_schema"): |
There was a problem hiding this comment.
Function Generator.jsoncolumndef_sql refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression)
| dtype = self.sql(expression, "dtype") | ||
| if dtype: | ||
| if dtype := self.sql(expression, "dtype"): | ||
| collate = self.sql(expression, "collate") | ||
| collate = f" COLLATE {collate}" if collate else "" | ||
| using = self.sql(expression, "using") | ||
| using = f" USING {using}" if using else "" | ||
| return f"ALTER COLUMN {this} SET DATA TYPE {dtype}{collate}{using}" | ||
|
|
||
| default = self.sql(expression, "default") | ||
| if default: | ||
| if default := self.sql(expression, "default"): |
There was a problem hiding this comment.
Function Generator.altercolumn_sql refactored with the following changes:
- Use named expression to simplify assignment and conditional [×2] (
use-named-expression)
| for value in arg_value: | ||
| args.append(value) | ||
| args.extend(iter(arg_value)) |
There was a problem hiding this comment.
Function Generator.function_fallback_sql refactored with the following changes:
- Replace a for append loop with list extend (
for-append-to-extend) - Simplify generator expression (
simplify-generator)
| having = self.sql(expression, "having") | ||
|
|
||
| if having: | ||
| if having := self.sql(expression, "having"): |
There was a problem hiding this comment.
Function Generator.anyvalue_sql refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression)
| if before: | ||
| prop = self._parse_property_before() | ||
| else: | ||
| prop = self._parse_property() | ||
|
|
||
| prop = self._parse_property_before() if before else self._parse_property() | ||
| if not prop: | ||
| break | ||
| for p in ensure_list(prop): | ||
| properties.append(p) | ||
|
|
||
| properties.extend(iter(ensure_list(prop))) |
There was a problem hiding this comment.
Function Parser._parse_properties refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp) - Replace a for append loop with list extend (
for-append-to-extend) - Simplify generator expression (
simplify-generator)
| if not self._next: | ||
| return None | ||
|
|
||
| return self._parse_withisolatedloading() | ||
| return None if not self._next else self._parse_withisolatedloading() |
There was a problem hiding this comment.
Function Parser._parse_with_property 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 not user or not host: | ||
| return None | ||
|
|
||
| return exp.DefinerProperty(this=f"{user}@{host}") | ||
| return exp.DefinerProperty(this=f"{user}@{host}") if user and host else None |
There was a problem hiding this comment.
Function Parser._parse_definer refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else) - Swap if/else branches (
swap-if-else-branches) - Replace if statement with if expression (
assign-if-exp)
| autotemp = None | ||
| if self._match_text_seq("AUTOTEMP"): | ||
| autotemp = self._parse_schema() | ||
|
|
||
| autotemp = self._parse_schema() if self._match_text_seq("AUTOTEMP") else None |
There was a problem hiding this comment.
Function Parser._parse_blockcompression refactored with the following changes:
- Move setting of default value for variable into
elsebranch (introduce-default-else) - Replace if statement with if expression (
assign-if-exp)
|
|
||
| id_var = self._parse_id_var() | ||
| if not id_var: | ||
| return None | ||
| if id_var := self._parse_id_var(): | ||
| options.append( | ||
| self.expression(exp.Property, this=this, value=exp.var(id_var.this.upper())) | ||
| ) | ||
|
|
||
| options.append( | ||
| self.expression(exp.Property, this=this, value=exp.var(id_var.this.upper())) | ||
| ) | ||
| else: | ||
| return None | ||
|
|
There was a problem hiding this comment.
Function Parser._parse_create_like refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression) - Lift code into else after jump in control flow (
reintroduce-else) - Swap if/else branches (
swap-if-else-branches)
Branch
mainrefactored 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
mainbranch, then run:Help us improve this pull request!