From 8dcd3b5afddcccae967acf176235472bf9fbb153 Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Mon, 16 Feb 2026 17:28:00 +0000 Subject: [PATCH] Optimization: make State final and avoid construction via cls() Constructions via cls() was pretty slow due to the large number of keyword arguments, as this used a generic calling convention. --- mypy/build.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mypy/build.py b/mypy/build.py index bf56075427d0..97c8b9d5ef90 100644 --- a/mypy/build.py +++ b/mypy/build.py @@ -39,6 +39,7 @@ TypeAlias as _TypeAlias, TypedDict, cast, + final, ) from librt.base64 import b64encode @@ -2172,6 +2173,7 @@ def __init__(self, reason: int = SuppressionReason.NOT_FOUND) -> None: self.reason = reason +@final class State: """The state for a module. @@ -2258,9 +2260,8 @@ class State: # Mapping from line number to type ignore codes on this line (for imports only). imports_ignored: dict[int, list[str]] - @classmethod + @staticmethod def new_state( - cls, id: str | None, path: str | None, source: str | None, @@ -2350,7 +2351,7 @@ def new_state( error_lines = [] imports_ignored = {} - state = cls( + state = State( manager=manager, order=State.order_counter, id=id,