-
Notifications
You must be signed in to change notification settings - Fork 15
Add ExperimentalWarning class and indicate reactions and targeted messages as preview #306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
corinagum
wants to merge
6
commits into
main
Choose a base branch
from
cg/experimental-marking
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
79 changes: 79 additions & 0 deletions
79
packages/common/src/microsoft_teams/common/experimental.py
This file contains hidden or 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,79 @@ | ||
| """ | ||
| Copyright (c) Microsoft Corporation. All rights reserved. | ||
| Licensed under the MIT License. | ||
| """ | ||
|
|
||
| import functools | ||
| import inspect | ||
| import warnings | ||
| from typing import Any, Callable, Optional, TypeVar | ||
|
|
||
| F = TypeVar("F", bound=Callable[..., Any]) | ||
|
|
||
|
|
||
| class ExperimentalWarning(FutureWarning): | ||
| """Warning category for Teams SDK preview APIs. | ||
|
|
||
| Preview APIs may change in the future. | ||
| """ | ||
|
|
||
| pass | ||
|
|
||
|
|
||
| def experimental(diagnostic: str, *, message: Optional[str] = None) -> Callable[[F], F]: | ||
| """Mark a class or function as a preview API. | ||
|
|
||
| Emits an ExperimentalWarning when the decorated class is instantiated | ||
| or the decorated function is called. | ||
|
|
||
| Args: | ||
| diagnostic: The diagnostic code (e.g., "TEAMS0001") for granular opt-in. | ||
| message: Optional custom warning message. If not provided, a default message is used. | ||
|
|
||
| Usage:: | ||
|
|
||
| @experimental("TEAMS0001") | ||
| class ReactionClient: | ||
| ... | ||
|
|
||
| @experimental("TEAMS0002", message="Targeted messages are in preview.") | ||
| async def create_targeted(...): | ||
| ... | ||
| """ | ||
|
|
||
| def decorator(obj: F) -> F: | ||
| name = getattr(obj, "__qualname__", getattr(obj, "__name__", str(obj))) | ||
| warn_msg = message or ( | ||
| f"{name} is in preview and may change in the future. " | ||
| f"Diagnostic: {diagnostic}" | ||
| ) | ||
|
|
||
| if isinstance(obj, type): | ||
| original_init = obj.__init__ | ||
|
|
||
| @functools.wraps(original_init) | ||
| def new_init(self: Any, *args: Any, **kwargs: Any) -> None: | ||
| warnings.warn(warn_msg, ExperimentalWarning, stacklevel=2) | ||
| original_init(self, *args, **kwargs) | ||
|
|
||
| obj.__init__ = new_init # type: ignore[misc] | ||
| return obj # type: ignore[return-value] | ||
| else: | ||
| if inspect.iscoroutinefunction(obj): | ||
|
|
||
| @functools.wraps(obj) | ||
| async def async_wrapper(*args: Any, **kwargs: Any) -> Any: | ||
| warnings.warn(warn_msg, ExperimentalWarning, stacklevel=2) | ||
| return await obj(*args, **kwargs) | ||
|
|
||
| return async_wrapper # type: ignore[return-value] | ||
| else: | ||
|
|
||
| @functools.wraps(obj) | ||
| def wrapper(*args: Any, **kwargs: Any) -> Any: | ||
| warnings.warn(warn_msg, ExperimentalWarning, stacklevel=2) | ||
| return obj(*args, **kwargs) | ||
|
|
||
| return wrapper # type: ignore[return-value] | ||
|
|
||
| return decorator |
This file contains hidden or 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,88 @@ | ||
| """ | ||
| Copyright (c) Microsoft Corporation. All rights reserved. | ||
| Licensed under the MIT License. | ||
| """ | ||
|
|
||
| import asyncio | ||
| import warnings | ||
|
|
||
| from microsoft_teams.common.experimental import ExperimentalWarning, experimental | ||
|
|
||
|
|
||
| @experimental("TEST001") | ||
| class _PreviewClass: | ||
| def __init__(self, value: str): | ||
| self.value = value | ||
|
|
||
|
|
||
| @experimental("TEST002") | ||
| def _preview_sync_func(x: int) -> int: | ||
| return x * 2 | ||
|
|
||
|
|
||
| @experimental("TEST003") | ||
| async def _preview_async_func(x: int) -> int: | ||
| return x * 3 | ||
|
|
||
|
|
||
| class TestExperimentalWarning: | ||
| def test_class_instantiation_emits_warning(self): | ||
| with warnings.catch_warnings(record=True) as w: | ||
| warnings.simplefilter("always") | ||
| obj = _PreviewClass("test") | ||
| assert len(w) == 1 | ||
| assert issubclass(w[0].category, ExperimentalWarning) | ||
| assert "TEST001" in str(w[0].message) | ||
| assert "preview" in str(w[0].message).lower() | ||
| assert obj.value == "test" | ||
|
|
||
| def test_sync_function_emits_warning(self): | ||
| with warnings.catch_warnings(record=True) as w: | ||
| warnings.simplefilter("always") | ||
| result = _preview_sync_func(5) | ||
| assert len(w) == 1 | ||
| assert issubclass(w[0].category, ExperimentalWarning) | ||
| assert "TEST002" in str(w[0].message) | ||
| assert result == 10 | ||
|
|
||
| def test_async_function_emits_warning(self): | ||
| async def _run() -> int: | ||
| return await _preview_async_func(5) | ||
|
|
||
| with warnings.catch_warnings(record=True) as w: | ||
| warnings.simplefilter("always") | ||
| result = asyncio.run(_run()) | ||
| assert len(w) == 1 | ||
| assert issubclass(w[0].category, ExperimentalWarning) | ||
| assert "TEST003" in str(w[0].message) | ||
| assert result == 15 | ||
|
|
||
| def test_warning_is_suppressible(self): | ||
| with warnings.catch_warnings(record=True) as w: | ||
| warnings.filterwarnings("ignore", category=ExperimentalWarning) | ||
| _PreviewClass("suppressed") | ||
| assert len(w) == 0 | ||
|
|
||
| def test_warning_is_suppressible_by_message(self): | ||
| with warnings.catch_warnings(record=True) as w: | ||
| warnings.filterwarnings("ignore", message=".*TEST001.*", category=ExperimentalWarning) | ||
| _PreviewClass("suppressed") | ||
| result = _preview_sync_func(5) | ||
| assert len(w) == 1 # only TEST002 warning, not TEST001 | ||
| assert "TEST002" in str(w[0].message) | ||
| assert result == 10 | ||
|
|
||
| def test_custom_message(self): | ||
| @experimental("CUSTOM", message="Custom preview message.") | ||
| def custom_func() -> str: | ||
| return "ok" | ||
|
|
||
| with warnings.catch_warnings(record=True) as w: | ||
| warnings.simplefilter("always") | ||
| result = custom_func() | ||
| assert len(w) == 1 | ||
| assert str(w[0].message) == "Custom preview message." | ||
| assert result == "ok" | ||
|
|
||
| def test_warning_is_future_warning_subclass(self): | ||
| assert issubclass(ExperimentalWarning, FutureWarning) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.