Skip to content

Commit f818f02

Browse files
committed
Add mechanism for throttled warnings and use that for the ExperimentalWarning
1 parent 8ada244 commit f818f02

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

src/neptune_query/internal/experimental.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,15 @@
1414
# limitations under the License.
1515

1616
import functools
17-
import warnings
1817
from typing import (
1918
Callable,
2019
ParamSpec,
2120
TypeVar,
2221
)
2322

23+
from neptune_query.internal.warnings import throttled_warn
2424
from neptune_query.warnings import ExperimentalWarning
2525

26-
# registry of functions already warned
27-
_warned_experimentals = set()
28-
2926
T = ParamSpec("T")
3027
R = TypeVar("R")
3128

@@ -38,14 +35,13 @@ def experimental(func: Callable[T, R]) -> Callable[T, R]:
3835

3936
@functools.wraps(func)
4037
def wrapper(*args: T.args, **kwargs: T.kwargs) -> R:
41-
if func not in _warned_experimentals:
42-
warnings.warn(
38+
throttled_warn(
39+
ExperimentalWarning(
4340
f"{func.__qualname__} is experimental and might change or be removed "
44-
"in a future minor release. Use with caution in production code.",
45-
category=ExperimentalWarning,
46-
stacklevel=2,
47-
)
48-
_warned_experimentals.add(func)
41+
"in a future minor release. Use with caution in production code."
42+
),
43+
stacklevel=3,
44+
)
4945
return func(*args, **kwargs)
5046

5147
return wrapper
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#
2+
# Copyright (c) 2025, Neptune Labs Sp. z o.o.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
import warnings
17+
from typing import Type
18+
19+
from neptune_query.warnings import ExperimentalWarning
20+
21+
# registry of warnings that were already emitted with the (type, message) tuple
22+
WARNING_CLASSES_EMITTED_ONCE_PER_MESSAGE = (ExperimentalWarning,)
23+
24+
_silence_warnings_msg: set[tuple[Type[Warning], str]] = set()
25+
26+
27+
def throttled_warn(warning: Warning, stacklevel: int = 2) -> None:
28+
if isinstance(warning, WARNING_CLASSES_EMITTED_ONCE_PER_MESSAGE):
29+
key = (type(warning), str(warning))
30+
if key in _silence_warnings_msg:
31+
return
32+
_silence_warnings_msg.add(key)
33+
34+
warnings.warn(warning, stacklevel=stacklevel)

0 commit comments

Comments
 (0)