Safely compare two values, coercing types when possible.
(left: Any, right: Any, op: str)
| 409 | |
| 410 | |
| 411 | def _safe_compare(left: Any, right: Any, op: str) -> bool: |
| 412 | """Safely compare two values, coercing types when possible.""" |
| 413 | try: |
| 414 | if isinstance(left, str): |
| 415 | left = float(left) if "." in left else int(left) |
| 416 | if isinstance(right, str): |
| 417 | right = float(right) if "." in right else int(right) |
| 418 | except (ValueError, TypeError): |
| 419 | return False |
| 420 | try: |
| 421 | if op == ">": |
| 422 | return left > right # type: ignore[operator] |
| 423 | if op == "<": |
| 424 | return left < right # type: ignore[operator] |
| 425 | if op == ">=": |
| 426 | return left >= right # type: ignore[operator] |
| 427 | if op == "<=": |
| 428 | return left <= right # type: ignore[operator] |
| 429 | except TypeError: |
| 430 | return False |
| 431 | return False |
| 432 | |
| 433 | |
| 434 | def evaluate_expression(template: str, context: Any) -> Any: |
no outgoing calls
no test coverage detected