Skip to content

Commit

Permalink
Use Assignment Expression (Walrus) In Conditional (#998)
Browse files Browse the repository at this point in the history
* Use Assignment Expression (Walrus) In Conditional

* 🎨 Apply formatting

---------

Co-authored-by: pixeebot[bot] <104101892+pixeebot[bot]@users.noreply.github.com>
  • Loading branch information
pixeebot[bot] authored Feb 20, 2025
1 parent 0e3722f commit 76220b4
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
3 changes: 1 addition & 2 deletions src/codemodder/codemodder.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ def apply_codemods(
for codemod in codemods_to_run:
# NOTE: this may be used as a progress indicator by upstream tools
logger.info("running codemod %s", codemod.id)
codemod_token_usage = codemod.apply(context)
if codemod_token_usage:
if codemod_token_usage := codemod.apply(context):
log_token_usage(f"Codemod {codemod.id}", codemod_token_usage)
token_usage += codemod_token_usage

Expand Down
10 changes: 6 additions & 4 deletions src/codemodder/codemods/utils_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,9 @@ def class_has_method(self, classdef: cst.ClassDef, method_name: str) -> bool:
"""Check if a given class definition implements a method of name `method_name`."""
for node in classdef.body.body:
match node:
case cst.FunctionDef(
name=cst.Name(value=value)
) if value == method_name:
case cst.FunctionDef(name=cst.Name(value=value)) if (
value == method_name
):
return True
return False

Expand All @@ -331,7 +331,9 @@ def is_value_of_assignment(
| cst.Assign(value=value)
| cst.WithItem(item=value)
| cst.NamedExpr(value=value)
) if expr == value: # type: ignore
) if (
expr == value
): # type: ignore
return parent
return None

Expand Down
2 changes: 1 addition & 1 deletion src/codemodder/utils/format_string_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def expressions_from_replacements(


def dict_to_values_dict(
expr_dict: dict[cst.BaseExpression, cst.BaseExpression]
expr_dict: dict[cst.BaseExpression, cst.BaseExpression],
) -> dict[str | cst.BaseExpression, cst.BaseExpression]:
return {
extract_raw_value(k): v
Expand Down

0 comments on commit 76220b4

Please sign in to comment.