From d8bf6e2ec26a1bc392a0309a737d0300a42370a2 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Thu, 20 Feb 2025 14:06:50 -0800 Subject: [PATCH] Optimize mypy/solve.py with min instead of sort (#18688) The first value of a stable sort always equivalent to a linear min search (and uses less memory). --- mypy/solve.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy/solve.py b/mypy/solve.py index cac1a23c5a333..57988790a7277 100644 --- a/mypy/solve.py +++ b/mypy/solve.py @@ -350,7 +350,7 @@ def test(x: U) -> U: ... # For convenience with current type application machinery, we use a stable # choice that prefers the original type variables (not polymorphic ones) in SCC. - best = sorted(scc, key=lambda x: (x.id not in original_vars, x.id.raw_id))[0] + best = min(scc, key=lambda x: (x.id not in original_vars, x.id.raw_id)) if isinstance(best, TypeVarType): return best.copy_modified(values=values, upper_bound=common_upper_bound) if is_trivial_bound(common_upper_bound_p, allow_tuple=True):