From 092d08daf224db80bd5806d5925c6517757b5c43 Mon Sep 17 00:00:00 2001 From: "pixeebot[bot]" <104101892+pixeebot[bot]@users.noreply.github.com> Date: Tue, 12 Mar 2024 14:01:09 +0000 Subject: [PATCH] Use Assignment Expression (Walrus) In Conditional --- src/codemodder/utils/clean_code.py | 6 ++---- src/codemodder/utils/format_string_parser.py | 5 ++--- src/core_codemods/sql_parameterization.py | 3 +-- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/codemodder/utils/clean_code.py b/src/codemodder/utils/clean_code.py index 47891987..6d7bc4b1 100644 --- a/src/codemodder/utils/clean_code.py +++ b/src/codemodder/utils/clean_code.py @@ -209,8 +209,7 @@ def _handle_target(self, node): # return node.with_changes(elements = new_elements) # return None case cst.Name(): - target_acesses = self.find_accesses(node) - if target_acesses: + if target_acesses := self.find_accesses(node): return node else: return None @@ -230,8 +229,7 @@ def leave_Assign( new_targets = [] for target in original_node.targets: - new_target = self._handle_target(target.target) - if new_target: + if new_target := self._handle_target(target.target): new_targets.append(target.with_changes(target=new_target)) # remove everything if not new_targets: diff --git a/src/codemodder/utils/format_string_parser.py b/src/codemodder/utils/format_string_parser.py index d1a711bc..f0ef769a 100644 --- a/src/codemodder/utils/format_string_parser.py +++ b/src/codemodder/utils/format_string_parser.py @@ -157,10 +157,9 @@ def parse_formatted_string( for piece, piece_parts in parsed_pieces: match piece: case cst.SimpleString() | cst.FormattedStringText(): - maybe_conversion = _convert_piece_and_parts( + if maybe_conversion := _convert_piece_and_parts( piece, piece_parts, token_count, keys - ) - if maybe_conversion: + ): converted, token_count = maybe_conversion parts.extend(converted) else: diff --git a/src/core_codemods/sql_parameterization.py b/src/core_codemods/sql_parameterization.py index a13059b9..dc90d720 100644 --- a/src/core_codemods/sql_parameterization.py +++ b/src/core_codemods/sql_parameterization.py @@ -77,8 +77,7 @@ def extract_prefix(self, node: StringLiteralNodeType) -> str: def _extract_prefix_raw_value(self, node: StringLiteralNodeType) -> tuple[str, str]: raw_value = extract_raw_value(node) - prefix = self.extract_prefix(node) - if prefix is not None: + if (prefix := self.extract_prefix(node)) is not None: return prefix, raw_value return prefix, raw_value