-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Python: Create an experimental class and function decorator. (#6298)
### Motivation and Context There may be classes or functions inside of SK Python that should be deemed as experimental. Currently in Python there is no out-of-the-box way to get the decorator. <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> ### Description As there is a name clash with using `experimental` as the decorator name, we are introducing two decorators: one `experimental_class` and one `experimental_function`. The note that "This {function | class} is experimental and may change in the future" is included in the docstring and a bool `is_experimental` on the class is added. <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [X] The code builds clean without any errors or warnings - [X] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [X] All unit tests pass, and I have added new tests where possible - [X] I didn't break anyone 😄
- Loading branch information
Showing
5 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Copyright (c) Microsoft. All rights reserved. | ||
|
||
import types | ||
from typing import Callable, Type | ||
|
||
|
||
def experimental_function(func: Callable) -> Callable: | ||
if isinstance(func, types.FunctionType): | ||
if func.__doc__: | ||
func.__doc__ += "\n\nNote: This function is experimental and may change in the future." | ||
else: | ||
func.__doc__ = "Note: This function is experimental and may change in the future." | ||
|
||
func.is_experimental = True | ||
|
||
return func | ||
|
||
|
||
def experimental_class(cls: Type) -> Type: | ||
if isinstance(cls, type): | ||
if cls.__doc__: | ||
cls.__doc__ += "\n\nNote: This class is experimental and may change in the future." | ||
else: | ||
cls.__doc__ = "Note: This class is experimental and may change in the future." | ||
|
||
cls.is_experimental = True | ||
|
||
return cls |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
python/tests/unit/functions/test_kernel_experimental_decorator.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# # Copyright (c) Microsoft. All rights reserved. | ||
|
||
from semantic_kernel.utils.experimental_decorator import ( | ||
experimental_function, | ||
) | ||
|
||
|
||
@experimental_function | ||
def my_function() -> None: | ||
"""This is a sample function docstring.""" | ||
pass | ||
|
||
|
||
@experimental_function | ||
def my_function_no_doc_string() -> None: | ||
pass | ||
|
||
|
||
def test_function_experimental_decorator() -> None: | ||
assert ( | ||
my_function.__doc__ | ||
== "This is a sample function docstring.\n\nNote: This function is experimental and may change in the future." | ||
) # noqa: E501 | ||
assert hasattr(my_function, "is_experimental") | ||
assert my_function.is_experimental is True | ||
|
||
|
||
def test_function_experimental_decorator_with_no_doc_string() -> None: | ||
assert my_function_no_doc_string.__doc__ == "Note: This function is experimental and may change in the future." | ||
assert hasattr(my_function_no_doc_string, "is_experimental") | ||
assert my_function_no_doc_string.is_experimental is True |
2 changes: 2 additions & 0 deletions
2
python/tests/unit/functions/test_kernel_function_decorators.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters