From 6b7d286ca75f91e84a06868342cc8a6dab757c03 Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Sat, 12 Jul 2025 12:31:44 +0100 Subject: [PATCH] Speed up the default plugin Use precalculated set objects in more places. This is similar to #19385. Some cases were still unoptimized. I used trace logging (#19457) to identify functions where we were creating many set objects, and I noticed that these were unncessary. This is a part of a set of micro-optimizations that improve self check performance by ~5.5%. --- mypy/plugins/default.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mypy/plugins/default.py b/mypy/plugins/default.py index 3d27ca99302f..e492b8dd7335 100644 --- a/mypy/plugins/default.py +++ b/mypy/plugins/default.py @@ -82,6 +82,7 @@ TD_SETDEFAULT_NAMES: Final = {n + ".setdefault" for n in TPDICT_FB_NAMES} TD_POP_NAMES: Final = {n + ".pop" for n in TPDICT_FB_NAMES} +TD_DELITEM_NAMES: Final = {n + ".__delitem__" for n in TPDICT_FB_NAMES} TD_UPDATE_METHOD_NAMES: Final = ( {n + ".update" for n in TPDICT_FB_NAMES} @@ -144,11 +145,11 @@ def get_method_hook(self, fullname: str) -> Callable[[MethodContext], Type] | No return int_pos_callback elif fullname in ("builtins.tuple.__mul__", "builtins.tuple.__rmul__"): return tuple_mul_callback - elif fullname in {n + ".setdefault" for n in TPDICT_FB_NAMES}: + elif fullname in TD_SETDEFAULT_NAMES: return typed_dict_setdefault_callback - elif fullname in {n + ".pop" for n in TPDICT_FB_NAMES}: + elif fullname in TD_POP_NAMES: return typed_dict_pop_callback - elif fullname in {n + ".__delitem__" for n in TPDICT_FB_NAMES}: + elif fullname in TD_DELITEM_NAMES: return typed_dict_delitem_callback elif fullname == "_ctypes.Array.__getitem__": return array_getitem_callback