|
19 | 19 | from ..constants import DEFAULT_WEAK_RANDOMNESS_FUNCTIONS |
20 | 20 |
|
21 | 21 |
|
22 | | -PY3 = sys.version_info[0] >= 3 |
23 | 22 | PY39_PLUS = sys.version_info >= (3, 9, 0) |
24 | 23 |
|
25 | 24 | _PREFIX = IAST.PATCH_ADDED_SYMBOL_PREFIX |
@@ -230,7 +229,7 @@ def _merge_dicts(*args_functions: Set[str]) -> Set[str]: |
230 | 229 |
|
231 | 230 | @staticmethod |
232 | 231 | def _is_string_node(node: Any) -> bool: |
233 | | - if PY3 and (isinstance(node, ast.Constant) and isinstance(node.value, IAST.TEXT_TYPES)): |
| 232 | + if isinstance(node, ast.Constant) and isinstance(node.value, IAST.TEXT_TYPES): |
234 | 233 | return True |
235 | 234 |
|
236 | 235 | return False |
@@ -652,46 +651,39 @@ def visit_AugAssign(self, augassign_node: ast.AugAssign) -> Any: |
652 | 651 | """ |
653 | 652 | Replace an inplace add or multiply (+= / *=) |
654 | 653 | """ |
655 | | - if isinstance(augassign_node.target, ast.Subscript): |
656 | | - # Can't augassign to function call, ignore this node |
657 | | - augassign_node.target.avoid_convert = True # type: ignore[attr-defined] |
658 | | - self.generic_visit(augassign_node) |
659 | | - return augassign_node |
660 | | - |
661 | 654 | self.generic_visit(augassign_node) |
| 655 | + |
662 | 656 | if augassign_node.op.__class__ == ast.Add: |
663 | 657 | # Optimization: ignore augassigns where the right side term is an integer since |
664 | 658 | # they can't apply to strings |
665 | 659 | if self._is_numeric_node(augassign_node.value): |
666 | 660 | return augassign_node |
667 | 661 |
|
668 | 662 | replacement_func = self._aspect_operators["INPLACE_ADD"] |
669 | | - else: |
670 | | - return augassign_node |
671 | | - |
672 | | - # We must change the ctx of the target and value to Load() for using |
673 | | - # them as function arguments while keeping it as Store() for the |
674 | | - # Assign.targets, thus the manual copy |
675 | 663 |
|
676 | | - func_arg1 = copy.deepcopy(augassign_node.target) |
677 | | - func_arg1.ctx = ast.Load() |
678 | | - func_arg2 = copy.deepcopy(augassign_node.value) |
679 | | - func_arg2.ctx = ast.Load() # type: ignore[attr-defined] |
| 664 | + # Regular inplace add for non-subscript targets |
| 665 | + func_arg1 = copy.deepcopy(augassign_node.target) |
| 666 | + func_arg1.ctx = ast.Load() |
| 667 | + func_arg2 = copy.deepcopy(augassign_node.value) |
| 668 | + if hasattr(func_arg2, "ctx"): |
| 669 | + func_arg2.ctx = ast.Load() |
| 670 | + |
| 671 | + call_node = self._call_node( |
| 672 | + augassign_node, |
| 673 | + func=self._attr_node(augassign_node, replacement_func), |
| 674 | + args=[func_arg1, func_arg2], |
| 675 | + ) |
680 | 676 |
|
681 | | - call_node = self._call_node( |
682 | | - augassign_node, |
683 | | - func=self._attr_node(augassign_node, replacement_func), |
684 | | - args=[func_arg1, func_arg2], |
685 | | - ) |
| 677 | + self.ast_modified = True |
| 678 | + return self._node( |
| 679 | + ast.Assign, |
| 680 | + augassign_node, |
| 681 | + targets=[augassign_node.target], |
| 682 | + value=call_node, |
| 683 | + type_comment=None, |
| 684 | + ) |
686 | 685 |
|
687 | | - self.ast_modified = True |
688 | | - return self._node( |
689 | | - ast.Assign, |
690 | | - augassign_node, |
691 | | - targets=[augassign_node.target], |
692 | | - value=call_node, |
693 | | - type_comment=None, |
694 | | - ) |
| 686 | + return augassign_node |
695 | 687 |
|
696 | 688 | def visit_FormattedValue(self, fmt_value_node: ast.FormattedValue) -> Any: |
697 | 689 | """ |
|
0 commit comments