From ee1fffb06234441bd7987d0538a5f676e7b9d01b Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Thu, 25 Jun 2026 18:51:48 +0000 Subject: [PATCH] [core] Defer requests import in framework_helpers framework_helpers imported requests at module top level, but it is only used by download_from_mirrors() to fetch toolchains during a build. The module is loaded during config validation (via the esp-idf framework, the default for esp32, and the host platform), so every such config paid the ~85ms requests import cost even though validation never downloads anything. Defer the import into download_from_mirrors(). Measured roughly 70ms (~16%) off esphome config wall time for an esp-idf/host config. --- esphome/framework_helpers.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/esphome/framework_helpers.py b/esphome/framework_helpers.py index 6bf389240b0..a8e5cf75a85 100644 --- a/esphome/framework_helpers.py +++ b/esphome/framework_helpers.py @@ -11,8 +11,6 @@ import sys import time from typing import IO -import requests - from esphome.helpers import ProgressBar, rmtree PathType = str | os.PathLike @@ -635,6 +633,10 @@ def download_from_mirrors( ValueError: If mirrors list is empty. Exception: If all download attempts fail. """ + # Imported lazily: requests is a heavy import (~85ms) and is only needed + # when actually downloading a toolchain, never during config validation. + import requests + # 1. Open target file for writing if path given with ExitStack() as stack: if isinstance(target, (str, os.PathLike)):