Skip to content

Commit

Permalink
Python: Create an experimental class and function decorator. (#6298)
Browse files Browse the repository at this point in the history
### 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
moonbox3 authored May 16, 2024
1 parent aa98754 commit d9aa617
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
28 changes: 28 additions & 0 deletions python/semantic_kernel/utils/experimental_decorator.py
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
14 changes: 14 additions & 0 deletions python/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@ def decorated_native_function(self) -> str:
return CustomPlugin


@pytest.fixture(scope="session")
def experimental_plugin_class():
from semantic_kernel.functions.kernel_function_decorator import kernel_function
from semantic_kernel.utils.experimental_decorator import experimental_class

@experimental_class
class ExperimentalPlugin:
@kernel_function(name="getLightStatus")
def decorated_native_function(self) -> str:
return "test"

return ExperimentalPlugin


@pytest.fixture(scope="session")
def create_mock_function() -> Callable:
from semantic_kernel.contents.streaming_text_content import StreamingTextContent
Expand Down
31 changes: 31 additions & 0 deletions python/tests/unit/functions/test_kernel_experimental_decorator.py
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
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Copyright (c) Microsoft. All rights reserved.

from typing import TYPE_CHECKING, Annotated, Any, AsyncGenerator, AsyncIterable, Optional, Union

import pytest
Expand Down
10 changes: 10 additions & 0 deletions python/tests/unit/kernel/test_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,4 +589,14 @@ def test_instantiate_prompt_execution_settings_through_kernel(kernel_with_servic
assert settings.service_id == "service"


# endregion
# experimental class decorator


def test_experimental_class_has_decorator_and_flag(experimental_plugin_class):
assert hasattr(experimental_plugin_class, "is_experimental")
assert experimental_plugin_class.is_experimental
assert "This class is experimental and may change in the future." in experimental_plugin_class.__doc__


# endregion

0 comments on commit d9aa617

Please sign in to comment.