Coverage for haystack/utils/experimental.py: 100%

14 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-08-21 13:53 +0000

1# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai> 

2# 

3# SPDX-License-Identifier: Apache-2.0 

4 

5import functools 

6import warnings 

7from typing import Any, TypeVar 

8 

9T = TypeVar("T") 

10 

11 

12def _experimental(cls: type[T]) -> type[T]: 

13 """ 

14 Class decorator that marks a Haystack component as experimental. 

15 

16 Components decorated with @experimental are subject to breaking changes 

17 or removal in future releases without prior deprecation notice. 

18 

19 ## Usage example 

20 

21 @_experimental 

22 @component 

23 class MyComponent: 

24 ... 

25 """ 

26 # getattr/setattr are intentional here: direct attribute access (cls.__init__, cls.__init__ = ...) 

27 # triggers mypy [misc] and [attr-defined] errors because T is an unbound TypeVar. 

28 # noqa comments suppress ruff B009/B010 which would auto-revert these back to direct access. 

29 original_init: Any = getattr(cls, "__init__") # noqa: B009 

30 

31 @functools.wraps(original_init) 

32 def new_init(self: Any, *args: Any, **kwargs: Any) -> None: 

33 warnings.warn( 

34 f"'{cls.__name__}' is an experimental component and may change or be removed " 

35 "in future releases without prior deprecation notice. ", 

36 ExperimentalWarning, 

37 stacklevel=2, 

38 ) 

39 original_init(self, *args, **kwargs) 

40 

41 setattr(cls, "__init__", new_init) # noqa: B010 

42 setattr(cls, "__experimental__", True) # noqa: B010 

43 return cls 

44 

45 

46class ExperimentalWarning(UserWarning): 

47 """Warning emitted when an experimental Haystack component is instantiated."""