-
Couldn't load subscription status.
- Fork 552
chore(types): Type-clean /actions (189 errors) #1361
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
Changes from 28 commits
00032d8
0506a5f
437ead1
f073992
bf89bc2
0c5fdb5
c3e73fc
fbb0e75
f9fb809
4cf304e
6e80f28
70eaad7
c6489cd
6da7740
558e37a
56d46b2
5db9e6a
540534d
6db53f2
41430be
522032e
3102d9e
2264291
47d5dcc
4680781
96aafc6
f7cbcf8
281afc8
8ebbc22
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,27 +14,48 @@ | |
| # limitations under the License. | ||
|
|
||
| from dataclasses import dataclass, field | ||
| from typing import Any, Callable, List, Optional, TypedDict, Union | ||
|
|
||
|
|
||
| class ActionMeta(TypedDict, total=False): | ||
| from typing import ( | ||
| Any, | ||
| Callable, | ||
| List, | ||
| Optional, | ||
| Protocol, | ||
| Type, | ||
| TypedDict, | ||
| TypeVar, | ||
| Union, | ||
| cast, | ||
| ) | ||
|
|
||
|
|
||
| class ActionMeta(TypedDict): | ||
tgasser-nv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| name: str | ||
| is_system_action: bool | ||
| execute_async: bool | ||
| output_mapping: Optional[Callable[[Any], bool]] | ||
|
|
||
|
|
||
| class Actionable(Protocol): | ||
| """Protocol for any object with ActionMeta metadata (i.e. decorated with @action)""" | ||
|
||
|
|
||
| action_meta: ActionMeta | ||
|
|
||
|
|
||
| # Create a TypeVar to represent the decorated function or class | ||
| T = TypeVar("T", bound=Union[Callable[..., Any], Type[Any]]) | ||
|
|
||
|
|
||
| def action( | ||
| is_system_action: bool = False, | ||
| name: Optional[str] = None, | ||
| execute_async: bool = False, | ||
| output_mapping: Optional[Callable[[Any], bool]] = None, | ||
| ) -> Callable[[Union[Callable, type]], Union[Callable, type]]: | ||
| ) -> Callable[[T], T]: | ||
| """Decorator to mark a function or class as an action. | ||
|
|
||
| Args: | ||
| is_system_action (bool): Flag indicating if the action is a system action. | ||
| name (Optional[str]): The name to associate with the action. | ||
| name (str): The name to associate with the action. | ||
| execute_async: Whether the function should be executed in async mode. | ||
| output_mapping (Optional[Callable[[Any], bool]]): A function to interpret the action's result. | ||
| It accepts the return value (e.g. the first element of a tuple) and return True if the output | ||
|
|
@@ -44,16 +65,19 @@ def action( | |
| callable: The decorated function or class. | ||
| """ | ||
|
|
||
| def decorator(fn_or_cls: Union[Callable, type]) -> Union[Callable, type]: | ||
| def decorator(fn_or_cls: Union[Callable, Type]) -> Union[Callable, Type]: | ||
| """Inner decorator function to add metadata to the action. | ||
|
|
||
| Args: | ||
| fn_or_cls: The function or class being decorated. | ||
| """ | ||
| fn_or_cls_target = getattr(fn_or_cls, "__func__", fn_or_cls) | ||
|
|
||
| # Action name is optional for the decorator, but mandatory for ActionMeta TypedDict | ||
| action_name: str = cast(str, name or fn_or_cls.__name__) | ||
|
|
||
| action_meta: ActionMeta = { | ||
| "name": name or fn_or_cls.__name__, | ||
| "name": action_name, | ||
| "is_system_action": is_system_action, | ||
| "execute_async": execute_async, | ||
| "output_mapping": output_mapping, | ||
|
|
@@ -62,7 +86,7 @@ def decorator(fn_or_cls: Union[Callable, type]) -> Union[Callable, type]: | |
| setattr(fn_or_cls_target, "action_meta", action_meta) | ||
| return fn_or_cls | ||
|
|
||
| return decorator | ||
| return decorator # pyright: ignore (TODO - resolve how the Actionable Protocol doesn't resolve the issue) | ||
|
|
||
|
|
||
| @dataclass | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😆
I think the intent was to make it easier to traceback the problematic file.
So if there are multiple identical filenames, which one is problematic
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, I added this back in again with some extra checking to make it type-clean