From e1e29145801693c455d7fd488f6a5f34f1d0892d Mon Sep 17 00:00:00 2001 From: Miguel Grinberg Date: Mon, 28 Jul 2025 15:59:56 +0100 Subject: [PATCH] add option to disable accurate warning file and line location (Fixes #3003) --- elasticsearch/compat.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/elasticsearch/compat.py b/elasticsearch/compat.py index 7639fd2bd..007971306 100644 --- a/elasticsearch/compat.py +++ b/elasticsearch/compat.py @@ -16,12 +16,15 @@ # under the License. import inspect +import os import sys from pathlib import Path from typing import Tuple, Type, Union string_types: Tuple[Type[str], Type[bytes]] = (str, bytes) +DISABLE_WARN_STACKLEVEL_ENV_VAR = "DISABLE_WARN_STACKLEVEL" + def to_str(x: Union[str, bytes], encoding: str = "ascii") -> str: if not isinstance(x, str): @@ -37,6 +40,8 @@ def to_bytes(x: Union[str, bytes], encoding: str = "ascii") -> bytes: def warn_stacklevel() -> int: """Dynamically determine warning stacklevel for warnings based on the call stack""" + if os.environ.get(DISABLE_WARN_STACKLEVEL_ENV_VAR) in ["1", "true", "True"]: + return 0 try: # Grab the root module from the current module '__name__' module_name = __name__.partition(".")[0]