|
| 1 | +import libcst as cst |
| 2 | + |
| 3 | +from codemodder.codemods.libcst_transformer import ( |
| 4 | + LibcstResultTransformer, |
| 5 | + LibcstTransformerPipeline, |
| 6 | +) |
| 7 | +from codemodder.codemods.utils_mixin import NameAndAncestorResolutionMixin |
| 8 | +from core_codemods.api import Metadata, Reference, ReviewGuidance |
| 9 | +from core_codemods.api.core_codemod import CoreCodemod |
| 10 | + |
| 11 | + |
| 12 | +class FixFloatEqualityTransformer( |
| 13 | + LibcstResultTransformer, NameAndAncestorResolutionMixin |
| 14 | +): |
| 15 | + change_description = "Replace `==` or `!=` with `math.isclose`" |
| 16 | + |
| 17 | + def leave_Comparison( |
| 18 | + self, original_node: cst.Comparison, updated_node: cst.Comparison |
| 19 | + ) -> cst.BaseExpression: |
| 20 | + if not self.node_is_selected(original_node): |
| 21 | + return updated_node |
| 22 | + |
| 23 | + match original_node: |
| 24 | + case cst.Comparison( |
| 25 | + left=left, comparisons=[cst.ComparisonTarget() as target] |
| 26 | + ): |
| 27 | + if isinstance( |
| 28 | + target.operator, cst.Equal | cst.NotEqual |
| 29 | + ) and self.at_least_one_float(left, right := target.comparator): |
| 30 | + self.add_needed_import("math") |
| 31 | + isclose_call = self.make_isclose_call(left, right) |
| 32 | + self.report_change(original_node) |
| 33 | + return ( |
| 34 | + isclose_call |
| 35 | + if isinstance(target.operator, cst.Equal) |
| 36 | + else cst.UnaryOperation( |
| 37 | + operator=cst.Not(), |
| 38 | + expression=isclose_call, |
| 39 | + ) |
| 40 | + ) |
| 41 | + return updated_node |
| 42 | + |
| 43 | + def make_isclose_call(self, left, right): |
| 44 | + return cst.Call( |
| 45 | + func=cst.Attribute( |
| 46 | + value=cst.Name(value="math"), attr=cst.Name(value="isclose") |
| 47 | + ), |
| 48 | + args=[ |
| 49 | + cst.Arg(value=left), |
| 50 | + cst.Arg(value=right), |
| 51 | + cst.Arg( |
| 52 | + keyword=cst.Name(value="rel_tol"), |
| 53 | + value=cst.Float(value="1e-09"), |
| 54 | + equal=cst.AssignEqual( |
| 55 | + whitespace_before=cst.SimpleWhitespace(""), |
| 56 | + whitespace_after=cst.SimpleWhitespace(""), |
| 57 | + ), |
| 58 | + ), |
| 59 | + cst.Arg( |
| 60 | + keyword=cst.Name(value="abs_tol"), |
| 61 | + value=cst.Float(value="0.0"), |
| 62 | + equal=cst.AssignEqual( |
| 63 | + whitespace_before=cst.SimpleWhitespace(""), |
| 64 | + whitespace_after=cst.SimpleWhitespace(""), |
| 65 | + ), |
| 66 | + ), |
| 67 | + ], |
| 68 | + ) |
| 69 | + |
| 70 | + def at_least_one_float(self, left, right) -> bool: |
| 71 | + left_type = self.resolve_expression(left) |
| 72 | + right_type = self.resolve_expression(right) |
| 73 | + |
| 74 | + match (left_type, right_type): |
| 75 | + case (cst.Float(), _) | (_, cst.Float()): |
| 76 | + return True |
| 77 | + case (cst.BinaryOperation(), _): |
| 78 | + return self.at_least_one_float(left_type.left, left_type.right) |
| 79 | + case (_, cst.BinaryOperation()): |
| 80 | + return self.at_least_one_float(right_type.left, right_type.right) |
| 81 | + return False |
| 82 | + |
| 83 | + |
| 84 | +FixFloatEquality = CoreCodemod( |
| 85 | + metadata=Metadata( |
| 86 | + name="fix-float-equality", |
| 87 | + summary="Use `math.isclose` Instead of Direct Equality for Floats", |
| 88 | + review_guidance=ReviewGuidance.MERGE_AFTER_REVIEW, |
| 89 | + references=[ |
| 90 | + Reference( |
| 91 | + url="https://docs.python.org/3/tutorial/floatingpoint.html#floating-point-arithmetic-issues-and-limitations" |
| 92 | + ), |
| 93 | + Reference(url="https://docs.python.org/3/library/math.html#math.isclose"), |
| 94 | + ], |
| 95 | + ), |
| 96 | + transformer=LibcstTransformerPipeline(FixFloatEqualityTransformer), |
| 97 | + detector=None, |
| 98 | +) |
0 commit comments