Skip to content

Don't rewrite it in Rust. Rewrite the 5% that's slow: profile your app, port the hot functions with your coding agent, prove they behave the same. Python, JS/TS, Ruby, Java, Kotlin, Go, PHP, C/C++, C#, R.

python: verify tells ints from floats

Mohit Mishra739016b14daa
diff --git a/src/python.rs b/src/python.rs
index b650063..c1e1a81 100644
--- a/src/python.rs
+++ b/src/python.rs
@@ -600,6 +600,29 @@ mod tests {
assert!(lib.contains("fn join_words(a: i64, b: i64)"), "{lib}");
}
+ #[test]
+ fn verify_tells_ints_from_floats() {
+ let Some(python) = ["python3", "python"]
+ .into_iter()
+ .find(|p| Command::new(p).arg("--version").output().is_ok_and(|o| o.status.success()))
+ else {
+ eprintln!("no python on PATH, skipping");
+ return;
+ };
+ let runner = std::env::temp_dir().join(format!("fa-runner-test-{}.py", std::process::id()));
+ fs::write(&runner, RUNNER).unwrap();
+ let check = "import runpy, sys\n\
+ same = runpy.run_path(sys.argv[1])['same']\n\
+ assert not same(3.0, 3) and not same(3, 3.0)\n\
+ assert not same([1.5, 2.0], [1.5, 2])\n\
+ assert not same({'k': 2.0}, {'k': 2})\n\
+ assert same(3.0, 3.0 + 1e-12) and same(3, 3) and same((1.0, 2), [1.0, 2])\n\
+ assert same(float('nan'), float('nan')) and not same(True, 1)\n";
+ let out = Command::new(python).args(["-I", "-c", check]).arg(&runner).output().unwrap();
+ let _ = fs::remove_file(&runner);
+ assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));
+ }
+
#[test]
fn spots_python() {
assert!(is_python("python"));
diff --git a/src/python/runner.py b/src/python/runner.py
index 30ded19..bfcff5d 100644
--- a/src/python/runner.py
+++ b/src/python/runner.py
@@ -72,11 +72,12 @@ def dec(v):
def same(a, b):
- """Equality the way a caller would see it: list == tuple, floats within 1e-9."""
+ """Equality the way a caller would see it: list == tuple, floats within 1e-9, but 3 != 3.0
+ (it prints differently)."""
if isinstance(a, bool) or isinstance(b, bool):
return type(a) is type(b) and a == b
if isinstance(a, float) or isinstance(b, float):
- if not (isinstance(a, (int, float)) and isinstance(b, (int, float))):
+ if not (isinstance(a, float) and isinstance(b, float)):
return False
if math.isnan(a) or math.isnan(b):
return math.isnan(a) and math.isnan(b)