nvidia-nat 1.3.0a20250910__py3-none-any.whl → 1.4.0a20251112__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- nat/agent/base.py +13 -8
- nat/agent/prompt_optimizer/prompt.py +68 -0
- nat/agent/prompt_optimizer/register.py +149 -0
- nat/agent/react_agent/agent.py +6 -5
- nat/agent/react_agent/register.py +49 -39
- nat/agent/reasoning_agent/reasoning_agent.py +17 -15
- nat/agent/register.py +2 -0
- nat/agent/responses_api_agent/__init__.py +14 -0
- nat/agent/responses_api_agent/register.py +126 -0
- nat/agent/rewoo_agent/agent.py +304 -117
- nat/agent/rewoo_agent/prompt.py +19 -22
- nat/agent/rewoo_agent/register.py +51 -38
- nat/agent/tool_calling_agent/agent.py +75 -17
- nat/agent/tool_calling_agent/register.py +46 -23
- nat/authentication/api_key/api_key_auth_provider.py +6 -11
- nat/authentication/api_key/api_key_auth_provider_config.py +8 -5
- nat/authentication/credential_validator/__init__.py +14 -0
- nat/authentication/credential_validator/bearer_token_validator.py +557 -0
- nat/authentication/http_basic_auth/http_basic_auth_provider.py +1 -1
- nat/authentication/interfaces.py +5 -2
- nat/authentication/oauth2/oauth2_auth_code_flow_provider.py +69 -36
- nat/authentication/oauth2/oauth2_auth_code_flow_provider_config.py +2 -1
- nat/authentication/oauth2/oauth2_resource_server_config.py +125 -0
- nat/builder/builder.py +55 -23
- nat/builder/component_utils.py +9 -5
- nat/builder/context.py +54 -15
- nat/builder/eval_builder.py +14 -9
- nat/builder/framework_enum.py +1 -0
- nat/builder/front_end.py +1 -1
- nat/builder/function.py +370 -0
- nat/builder/function_info.py +1 -1
- nat/builder/intermediate_step_manager.py +38 -2
- nat/builder/workflow.py +5 -0
- nat/builder/workflow_builder.py +306 -54
- nat/cli/cli_utils/config_override.py +1 -1
- nat/cli/commands/info/info.py +16 -6
- nat/cli/commands/mcp/__init__.py +14 -0
- nat/cli/commands/mcp/mcp.py +986 -0
- nat/cli/commands/optimize.py +90 -0
- nat/cli/commands/start.py +1 -1
- nat/cli/commands/workflow/templates/config.yml.j2 +14 -13
- nat/cli/commands/workflow/templates/register.py.j2 +2 -2
- nat/cli/commands/workflow/templates/workflow.py.j2 +35 -21
- nat/cli/commands/workflow/workflow_commands.py +60 -18
- nat/cli/entrypoint.py +15 -11
- nat/cli/main.py +3 -0
- nat/cli/register_workflow.py +38 -4
- nat/cli/type_registry.py +72 -1
- nat/control_flow/__init__.py +0 -0
- nat/control_flow/register.py +20 -0
- nat/control_flow/router_agent/__init__.py +0 -0
- nat/control_flow/router_agent/agent.py +329 -0
- nat/control_flow/router_agent/prompt.py +48 -0
- nat/control_flow/router_agent/register.py +91 -0
- nat/control_flow/sequential_executor.py +166 -0
- nat/data_models/agent.py +34 -0
- nat/data_models/api_server.py +199 -69
- nat/data_models/authentication.py +23 -9
- nat/data_models/common.py +47 -0
- nat/data_models/component.py +2 -0
- nat/data_models/component_ref.py +11 -0
- nat/data_models/config.py +41 -17
- nat/data_models/dataset_handler.py +4 -3
- nat/data_models/function.py +34 -0
- nat/data_models/function_dependencies.py +8 -0
- nat/data_models/intermediate_step.py +9 -1
- nat/data_models/llm.py +15 -1
- nat/data_models/openai_mcp.py +46 -0
- nat/data_models/optimizable.py +208 -0
- nat/data_models/optimizer.py +161 -0
- nat/data_models/span.py +41 -3
- nat/data_models/thinking_mixin.py +2 -2
- nat/embedder/azure_openai_embedder.py +2 -1
- nat/embedder/nim_embedder.py +3 -2
- nat/embedder/openai_embedder.py +3 -2
- nat/eval/config.py +1 -1
- nat/eval/dataset_handler/dataset_downloader.py +3 -2
- nat/eval/dataset_handler/dataset_filter.py +34 -2
- nat/eval/evaluate.py +10 -3
- nat/eval/evaluator/base_evaluator.py +1 -1
- nat/eval/rag_evaluator/evaluate.py +7 -4
- nat/eval/register.py +4 -0
- nat/eval/runtime_evaluator/__init__.py +14 -0
- nat/eval/runtime_evaluator/evaluate.py +123 -0
- nat/eval/runtime_evaluator/register.py +100 -0
- nat/eval/swe_bench_evaluator/evaluate.py +1 -1
- nat/eval/trajectory_evaluator/register.py +1 -1
- nat/eval/tunable_rag_evaluator/evaluate.py +1 -1
- nat/eval/usage_stats.py +2 -0
- nat/eval/utils/output_uploader.py +3 -2
- nat/eval/utils/weave_eval.py +17 -3
- nat/experimental/decorators/experimental_warning_decorator.py +27 -7
- nat/experimental/test_time_compute/functions/execute_score_select_function.py +1 -1
- nat/experimental/test_time_compute/functions/plan_select_execute_function.py +7 -3
- nat/experimental/test_time_compute/functions/ttc_tool_orchestration_function.py +1 -1
- nat/experimental/test_time_compute/functions/ttc_tool_wrapper_function.py +3 -3
- nat/experimental/test_time_compute/models/strategy_base.py +2 -2
- nat/experimental/test_time_compute/selection/llm_based_output_merging_selector.py +1 -1
- nat/front_ends/console/authentication_flow_handler.py +82 -30
- nat/front_ends/console/console_front_end_plugin.py +19 -7
- nat/front_ends/fastapi/auth_flow_handlers/http_flow_handler.py +1 -1
- nat/front_ends/fastapi/auth_flow_handlers/websocket_flow_handler.py +52 -17
- nat/front_ends/fastapi/dask_client_mixin.py +65 -0
- nat/front_ends/fastapi/fastapi_front_end_config.py +25 -3
- nat/front_ends/fastapi/fastapi_front_end_plugin.py +140 -3
- nat/front_ends/fastapi/fastapi_front_end_plugin_worker.py +445 -265
- nat/front_ends/fastapi/job_store.py +518 -99
- nat/front_ends/fastapi/main.py +11 -19
- nat/front_ends/fastapi/message_handler.py +69 -44
- nat/front_ends/fastapi/message_validator.py +8 -7
- nat/front_ends/fastapi/utils.py +57 -0
- nat/front_ends/mcp/introspection_token_verifier.py +73 -0
- nat/front_ends/mcp/mcp_front_end_config.py +71 -3
- nat/front_ends/mcp/mcp_front_end_plugin.py +85 -21
- nat/front_ends/mcp/mcp_front_end_plugin_worker.py +248 -29
- nat/front_ends/mcp/memory_profiler.py +320 -0
- nat/front_ends/mcp/tool_converter.py +78 -25
- nat/front_ends/simple_base/simple_front_end_plugin_base.py +3 -1
- nat/llm/aws_bedrock_llm.py +21 -8
- nat/llm/azure_openai_llm.py +14 -5
- nat/llm/litellm_llm.py +80 -0
- nat/llm/nim_llm.py +23 -9
- nat/llm/openai_llm.py +19 -7
- nat/llm/register.py +4 -0
- nat/llm/utils/thinking.py +1 -1
- nat/observability/exporter/base_exporter.py +1 -1
- nat/observability/exporter/processing_exporter.py +29 -55
- nat/observability/exporter/span_exporter.py +43 -15
- nat/observability/exporter_manager.py +2 -2
- nat/observability/mixin/redaction_config_mixin.py +5 -4
- nat/observability/mixin/tagging_config_mixin.py +26 -14
- nat/observability/mixin/type_introspection_mixin.py +420 -107
- nat/observability/processor/batching_processor.py +1 -1
- nat/observability/processor/processor.py +3 -0
- nat/observability/processor/redaction/__init__.py +24 -0
- nat/observability/processor/redaction/contextual_redaction_processor.py +125 -0
- nat/observability/processor/redaction/contextual_span_redaction_processor.py +66 -0
- nat/observability/processor/redaction/redaction_processor.py +177 -0
- nat/observability/processor/redaction/span_header_redaction_processor.py +92 -0
- nat/observability/processor/span_tagging_processor.py +21 -14
- nat/observability/register.py +16 -0
- nat/profiler/callbacks/langchain_callback_handler.py +32 -7
- nat/profiler/callbacks/llama_index_callback_handler.py +36 -2
- nat/profiler/callbacks/token_usage_base_model.py +2 -0
- nat/profiler/decorators/framework_wrapper.py +61 -9
- nat/profiler/decorators/function_tracking.py +35 -3
- nat/profiler/forecasting/models/linear_model.py +1 -1
- nat/profiler/forecasting/models/random_forest_regressor.py +1 -1
- nat/profiler/inference_optimization/bottleneck_analysis/nested_stack_analysis.py +1 -1
- nat/profiler/inference_optimization/experimental/prefix_span_analysis.py +1 -1
- nat/profiler/parameter_optimization/__init__.py +0 -0
- nat/profiler/parameter_optimization/optimizable_utils.py +93 -0
- nat/profiler/parameter_optimization/optimizer_runtime.py +67 -0
- nat/profiler/parameter_optimization/parameter_optimizer.py +189 -0
- nat/profiler/parameter_optimization/parameter_selection.py +107 -0
- nat/profiler/parameter_optimization/pareto_visualizer.py +460 -0
- nat/profiler/parameter_optimization/prompt_optimizer.py +384 -0
- nat/profiler/parameter_optimization/update_helpers.py +66 -0
- nat/profiler/utils.py +3 -1
- nat/registry_handlers/pypi/register_pypi.py +5 -3
- nat/registry_handlers/rest/register_rest.py +5 -3
- nat/retriever/milvus/retriever.py +1 -1
- nat/retriever/nemo_retriever/register.py +2 -1
- nat/runtime/loader.py +1 -1
- nat/runtime/runner.py +111 -6
- nat/runtime/session.py +49 -3
- nat/settings/global_settings.py +2 -2
- nat/tool/chat_completion.py +4 -1
- nat/tool/code_execution/code_sandbox.py +3 -6
- nat/tool/code_execution/local_sandbox/Dockerfile.sandbox +19 -32
- nat/tool/code_execution/local_sandbox/local_sandbox_server.py +6 -1
- nat/tool/code_execution/local_sandbox/sandbox.requirements.txt +2 -0
- nat/tool/code_execution/local_sandbox/start_local_sandbox.sh +10 -4
- nat/tool/datetime_tools.py +1 -1
- nat/tool/github_tools.py +450 -0
- nat/tool/memory_tools/add_memory_tool.py +3 -3
- nat/tool/memory_tools/delete_memory_tool.py +3 -4
- nat/tool/memory_tools/get_memory_tool.py +4 -4
- nat/tool/register.py +2 -7
- nat/tool/server_tools.py +15 -2
- nat/utils/__init__.py +76 -0
- nat/utils/callable_utils.py +70 -0
- nat/utils/data_models/schema_validator.py +1 -1
- nat/utils/decorators.py +210 -0
- nat/utils/exception_handlers/automatic_retries.py +278 -72
- nat/utils/io/yaml_tools.py +73 -3
- nat/utils/log_levels.py +25 -0
- nat/utils/responses_api.py +26 -0
- nat/utils/string_utils.py +16 -0
- nat/utils/type_converter.py +12 -3
- nat/utils/type_utils.py +6 -2
- nvidia_nat-1.4.0a20251112.dist-info/METADATA +197 -0
- {nvidia_nat-1.3.0a20250910.dist-info → nvidia_nat-1.4.0a20251112.dist-info}/RECORD +199 -165
- {nvidia_nat-1.3.0a20250910.dist-info → nvidia_nat-1.4.0a20251112.dist-info}/entry_points.txt +1 -0
- nat/cli/commands/info/list_mcp.py +0 -461
- nat/data_models/temperature_mixin.py +0 -43
- nat/data_models/top_p_mixin.py +0 -43
- nat/observability/processor/header_redaction_processor.py +0 -123
- nat/observability/processor/redaction_processor.py +0 -77
- nat/tool/code_execution/test_code_execution_sandbox.py +0 -414
- nat/tool/github_tools/create_github_commit.py +0 -133
- nat/tool/github_tools/create_github_issue.py +0 -87
- nat/tool/github_tools/create_github_pr.py +0 -106
- nat/tool/github_tools/get_github_file.py +0 -106
- nat/tool/github_tools/get_github_issue.py +0 -166
- nat/tool/github_tools/get_github_pr.py +0 -256
- nat/tool/github_tools/update_github_issue.py +0 -100
- nvidia_nat-1.3.0a20250910.dist-info/METADATA +0 -373
- /nat/{tool/github_tools → agent/prompt_optimizer}/__init__.py +0 -0
- {nvidia_nat-1.3.0a20250910.dist-info → nvidia_nat-1.4.0a20251112.dist-info}/WHEEL +0 -0
- {nvidia_nat-1.3.0a20250910.dist-info → nvidia_nat-1.4.0a20251112.dist-info}/licenses/LICENSE-3rd-party.txt +0 -0
- {nvidia_nat-1.3.0a20250910.dist-info → nvidia_nat-1.4.0a20251112.dist-info}/licenses/LICENSE.md +0 -0
- {nvidia_nat-1.3.0a20250910.dist-info → nvidia_nat-1.4.0a20251112.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
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 inspect
|
|
17
|
+
from collections.abc import Callable
|
|
18
|
+
from typing import Any
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
async def ainvoke_any(func: Callable[..., Any], *args: Any, **kwargs: Any) -> Any:
|
|
22
|
+
"""Execute any type of callable and return the result.
|
|
23
|
+
|
|
24
|
+
Handles synchronous functions, asynchronous functions, generators,
|
|
25
|
+
and async generators uniformly, returning the final result value.
|
|
26
|
+
|
|
27
|
+
Args:
|
|
28
|
+
func (Callable[..., Any]): The function to execute (sync/async function, generator, etc.)
|
|
29
|
+
|
|
30
|
+
Returns:
|
|
31
|
+
Any: The result of executing the callable
|
|
32
|
+
"""
|
|
33
|
+
# Execute the function
|
|
34
|
+
result_value = func(*args, **kwargs)
|
|
35
|
+
|
|
36
|
+
# Handle different return types
|
|
37
|
+
if inspect.iscoroutine(result_value):
|
|
38
|
+
# Async function - await the coroutine
|
|
39
|
+
return await result_value
|
|
40
|
+
|
|
41
|
+
if inspect.isgenerator(result_value):
|
|
42
|
+
# Sync generator - consume until StopIteration and get return value
|
|
43
|
+
try:
|
|
44
|
+
while True:
|
|
45
|
+
next(result_value)
|
|
46
|
+
except StopIteration as e:
|
|
47
|
+
# Return the generator's return value, or None if not provided
|
|
48
|
+
return e.value
|
|
49
|
+
|
|
50
|
+
if inspect.isasyncgen(result_value):
|
|
51
|
+
# Async generator - consume all values and return the last one
|
|
52
|
+
last_value = None
|
|
53
|
+
async for value in result_value:
|
|
54
|
+
last_value = value
|
|
55
|
+
return last_value
|
|
56
|
+
|
|
57
|
+
# Direct value from sync function (most common case)
|
|
58
|
+
return result_value
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def is_async_callable(func: Callable[..., Any]) -> bool:
|
|
62
|
+
"""Check if a function is async (coroutine function or async generator function).
|
|
63
|
+
|
|
64
|
+
Args:
|
|
65
|
+
func (Callable[..., Any]): The function to check
|
|
66
|
+
|
|
67
|
+
Returns:
|
|
68
|
+
bool: True if the function is async, False otherwise
|
|
69
|
+
"""
|
|
70
|
+
return inspect.iscoroutinefunction(func) or inspect.isasyncgenfunction(func)
|
nat/utils/decorators.py
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
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
|
+
"""Deprecation utilities.
|
|
16
|
+
|
|
17
|
+
This module provides helpers to standardize deprecation signaling across the
|
|
18
|
+
codebase:
|
|
19
|
+
|
|
20
|
+
- ``issue_deprecation_warning``: Builds and emits a single deprecation message
|
|
21
|
+
per function using the standard logging pipeline.
|
|
22
|
+
- ``deprecated``: A decorator that wraps sync/async functions and generators to
|
|
23
|
+
log a one-time deprecation message upon first use. It supports optional
|
|
24
|
+
metadata, a planned removal version, a suggested replacement, and an
|
|
25
|
+
optional feature name label.
|
|
26
|
+
|
|
27
|
+
Messages are emitted via ``logging.getLogger(__name__).warning`` (not
|
|
28
|
+
``warnings.warn``) so they appear in normal application logs and respect global
|
|
29
|
+
logging configuration. Each unique function logs at most once per process.
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
import functools
|
|
33
|
+
import inspect
|
|
34
|
+
import logging
|
|
35
|
+
from collections.abc import AsyncGenerator
|
|
36
|
+
from collections.abc import Callable
|
|
37
|
+
from collections.abc import Generator
|
|
38
|
+
from typing import Any
|
|
39
|
+
from typing import TypeVar
|
|
40
|
+
from typing import overload
|
|
41
|
+
|
|
42
|
+
logger = logging.getLogger(__name__)
|
|
43
|
+
|
|
44
|
+
_warning_issued = set()
|
|
45
|
+
|
|
46
|
+
# Type variables for overloads
|
|
47
|
+
F = TypeVar('F', bound=Callable[..., Any])
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def issue_deprecation_warning(function_name: str,
|
|
51
|
+
removal_version: str | None = None,
|
|
52
|
+
replacement: str | None = None,
|
|
53
|
+
reason: str | None = None,
|
|
54
|
+
feature_name: str | None = None,
|
|
55
|
+
metadata: dict[str, Any] | None = None) -> None:
|
|
56
|
+
"""
|
|
57
|
+
Log a deprecation warning message for the function.
|
|
58
|
+
|
|
59
|
+
A warning is emitted only once per function. When a ``metadata`` dict
|
|
60
|
+
is supplied, it is appended to the log entry to provide extra context
|
|
61
|
+
(e.g., version, author, feature flag).
|
|
62
|
+
|
|
63
|
+
Args:
|
|
64
|
+
function_name: The name of the deprecated function
|
|
65
|
+
removal_version: The version when the function will be removed
|
|
66
|
+
replacement: What to use instead of this function
|
|
67
|
+
reason: Why the function is being deprecated
|
|
68
|
+
feature_name: Optional name of the feature that is deprecated
|
|
69
|
+
metadata: Optional dictionary of metadata to log with the warning
|
|
70
|
+
"""
|
|
71
|
+
if function_name not in _warning_issued:
|
|
72
|
+
# Build the deprecation message
|
|
73
|
+
if feature_name:
|
|
74
|
+
warning_message = f"{feature_name} is deprecated"
|
|
75
|
+
else:
|
|
76
|
+
warning_message = f"Function {function_name} is deprecated"
|
|
77
|
+
|
|
78
|
+
if removal_version:
|
|
79
|
+
warning_message += f" and will be removed in version {removal_version}"
|
|
80
|
+
else:
|
|
81
|
+
warning_message += " and will be removed in a future release"
|
|
82
|
+
|
|
83
|
+
warning_message += "."
|
|
84
|
+
|
|
85
|
+
if reason:
|
|
86
|
+
warning_message += f" Reason: {reason}."
|
|
87
|
+
|
|
88
|
+
if replacement:
|
|
89
|
+
warning_message += f" Use '{replacement}' instead."
|
|
90
|
+
|
|
91
|
+
if metadata:
|
|
92
|
+
warning_message += f" | Metadata: {metadata}"
|
|
93
|
+
|
|
94
|
+
# Issue warning and save function name to avoid duplicate warnings
|
|
95
|
+
logger.warning(warning_message)
|
|
96
|
+
_warning_issued.add(function_name)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
# Overloads for different function types
|
|
100
|
+
@overload
|
|
101
|
+
def deprecated(func: F,
|
|
102
|
+
*,
|
|
103
|
+
removal_version: str | None = None,
|
|
104
|
+
replacement: str | None = None,
|
|
105
|
+
reason: str | None = None,
|
|
106
|
+
feature_name: str | None = None,
|
|
107
|
+
metadata: dict[str, Any] | None = None) -> F:
|
|
108
|
+
"""Overload for direct decorator usage (when called without parentheses)."""
|
|
109
|
+
...
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
@overload
|
|
113
|
+
def deprecated(*,
|
|
114
|
+
removal_version: str | None = None,
|
|
115
|
+
replacement: str | None = None,
|
|
116
|
+
reason: str | None = None,
|
|
117
|
+
feature_name: str | None = None,
|
|
118
|
+
metadata: dict[str, Any] | None = None) -> Callable[[F], F]:
|
|
119
|
+
"""Overload for decorator factory usage (when called with parentheses)."""
|
|
120
|
+
...
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def deprecated(func: Any = None,
|
|
124
|
+
*,
|
|
125
|
+
removal_version: str | None = None,
|
|
126
|
+
replacement: str | None = None,
|
|
127
|
+
reason: str | None = None,
|
|
128
|
+
feature_name: str | None = None,
|
|
129
|
+
metadata: dict[str, Any] | None = None) -> Any:
|
|
130
|
+
"""
|
|
131
|
+
Decorator that can wrap any type of function (sync, async, generator,
|
|
132
|
+
async generator) and logs a deprecation warning.
|
|
133
|
+
|
|
134
|
+
Args:
|
|
135
|
+
func: The function to be decorated.
|
|
136
|
+
removal_version: The version when the function will be removed
|
|
137
|
+
replacement: What to use instead of this function
|
|
138
|
+
reason: Why the function is being deprecated
|
|
139
|
+
feature_name: Optional name of the feature that is deprecated. If provided, the warning will be
|
|
140
|
+
prefixed with "The <feature_name> feature is deprecated".
|
|
141
|
+
metadata: Optional dictionary of metadata to log with the warning. This can include information
|
|
142
|
+
like version, author, etc. If provided, the metadata will be
|
|
143
|
+
logged alongside the deprecation warning.
|
|
144
|
+
"""
|
|
145
|
+
function_name: str = f"{func.__module__}.{func.__qualname__}" if func else "<unknown_function>"
|
|
146
|
+
|
|
147
|
+
# If called as @deprecated(...) but not immediately passed a function
|
|
148
|
+
if func is None:
|
|
149
|
+
|
|
150
|
+
def decorator_wrapper(actual_func):
|
|
151
|
+
return deprecated(actual_func,
|
|
152
|
+
removal_version=removal_version,
|
|
153
|
+
replacement=replacement,
|
|
154
|
+
reason=reason,
|
|
155
|
+
feature_name=feature_name,
|
|
156
|
+
metadata=metadata)
|
|
157
|
+
|
|
158
|
+
return decorator_wrapper
|
|
159
|
+
|
|
160
|
+
# --- Validate metadata ---
|
|
161
|
+
if metadata is not None:
|
|
162
|
+
if not isinstance(metadata, dict):
|
|
163
|
+
raise TypeError("metadata must be a dict[str, Any].")
|
|
164
|
+
if any(not isinstance(k, str) for k in metadata.keys()):
|
|
165
|
+
raise TypeError("All metadata keys must be strings.")
|
|
166
|
+
|
|
167
|
+
# --- Now detect the function type and wrap accordingly ---
|
|
168
|
+
if inspect.isasyncgenfunction(func):
|
|
169
|
+
# ---------------------
|
|
170
|
+
# ASYNC GENERATOR
|
|
171
|
+
# ---------------------
|
|
172
|
+
|
|
173
|
+
@functools.wraps(func)
|
|
174
|
+
async def async_gen_wrapper(*args, **kwargs) -> AsyncGenerator[Any, Any]:
|
|
175
|
+
issue_deprecation_warning(function_name, removal_version, replacement, reason, feature_name, metadata)
|
|
176
|
+
async for item in func(*args, **kwargs):
|
|
177
|
+
yield item # yield the original item
|
|
178
|
+
|
|
179
|
+
return async_gen_wrapper
|
|
180
|
+
|
|
181
|
+
if inspect.iscoroutinefunction(func):
|
|
182
|
+
# ---------------------
|
|
183
|
+
# ASYNC FUNCTION
|
|
184
|
+
# ---------------------
|
|
185
|
+
@functools.wraps(func)
|
|
186
|
+
async def async_wrapper(*args, **kwargs) -> Any:
|
|
187
|
+
issue_deprecation_warning(function_name, removal_version, replacement, reason, feature_name, metadata)
|
|
188
|
+
result = await func(*args, **kwargs)
|
|
189
|
+
return result
|
|
190
|
+
|
|
191
|
+
return async_wrapper
|
|
192
|
+
|
|
193
|
+
if inspect.isgeneratorfunction(func):
|
|
194
|
+
# ---------------------
|
|
195
|
+
# SYNC GENERATOR
|
|
196
|
+
# ---------------------
|
|
197
|
+
@functools.wraps(func)
|
|
198
|
+
def sync_gen_wrapper(*args, **kwargs) -> Generator[Any, Any, Any]:
|
|
199
|
+
issue_deprecation_warning(function_name, removal_version, replacement, reason, feature_name, metadata)
|
|
200
|
+
yield from func(*args, **kwargs) # yield the original item
|
|
201
|
+
|
|
202
|
+
return sync_gen_wrapper
|
|
203
|
+
|
|
204
|
+
@functools.wraps(func)
|
|
205
|
+
def sync_wrapper(*args, **kwargs) -> Any:
|
|
206
|
+
issue_deprecation_warning(function_name, removal_version, replacement, reason, feature_name, metadata)
|
|
207
|
+
result = func(*args, **kwargs)
|
|
208
|
+
return result
|
|
209
|
+
|
|
210
|
+
return sync_wrapper
|