Skip to content

Commit 851376e

Browse files
authored
fix type hint for event constraint to allow None subtypes (#616)
* fix type hint for message constraint to allow None subtypes Signed-off-by: Alexander Rashed <alexander.rashed@localstack.cloud> * revert changes to auto-generated docs Signed-off-by: Alexander Rashed <alexander.rashed@localstack.cloud>
1 parent 54114ca commit 851376e

4 files changed

Lines changed: 15 additions & 6 deletions

File tree

docs/_basic/listening_events.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def ask_for_introduction(event, say):
3636
The `message()` listener is equivalent to `event("message")`.
3737

3838
You can filter on subtypes of events by passing in the additional key `subtype`. Common message subtypes like `bot_message` and `message_replied` can be found [on the message event page](https://api.slack.com/events/message#message_subtypes).
39+
You can explicitly filter for events without a subtype by explicitly setting `None`.
3940

4041
</div>
4142

slack_bolt/app/app.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,9 @@ def custom_error_handler(error, body, logger):
724724
def event(
725725
self,
726726
event: Union[
727-
str, Pattern, Dict[str, Union[str, Sequence[Optional[Union[str, Pattern]]]]]
727+
str,
728+
Pattern,
729+
Dict[str, Optional[Union[str, Sequence[Optional[Union[str, Pattern]]]]]],
728730
],
729731
matchers: Optional[Sequence[Callable[..., bool]]] = None,
730732
middleware: Optional[Sequence[Union[Callable, Middleware]]] = None,

slack_bolt/app/async_app.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,9 @@ async def custom_error_handler(error, body, logger):
782782
def event(
783783
self,
784784
event: Union[
785-
str, Pattern, Dict[str, Union[str, Sequence[Optional[Union[str, Pattern]]]]]
785+
str,
786+
Pattern,
787+
Dict[str, Optional[Union[str, Sequence[Optional[Union[str, Pattern]]]]]],
786788
],
787789
matchers: Optional[Sequence[Callable[..., Awaitable[bool]]]] = None,
788790
middleware: Optional[Sequence[Union[Callable, AsyncMiddleware]]] = None,

slack_bolt/listener_matcher/builtins.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ async def async_fun(body: Dict[str, Any]) -> bool:
7878

7979
def event(
8080
constraints: Union[
81-
str, Pattern, Dict[str, Union[str, Sequence[Optional[Union[str, Pattern]]]]]
81+
str,
82+
Pattern,
83+
Dict[str, Optional[Union[str, Sequence[Optional[Union[str, Pattern]]]]]],
8284
],
8385
asyncio: bool = False,
8486
) -> Union[ListenerMatcher, "AsyncListenerMatcher"]:
@@ -110,7 +112,9 @@ def func(body: Dict[str, Any]) -> bool:
110112

111113

112114
def message_event(
113-
constraints: Dict[str, Union[str, Sequence[Optional[Union[str, Pattern]]]]],
115+
constraints: Dict[
116+
str, Optional[Union[str, Sequence[Optional[Union[str, Pattern]]]]]
117+
],
114118
keyword: Union[str, Pattern],
115119
asyncio: bool = False,
116120
) -> Union[ListenerMatcher, "AsyncListenerMatcher"]:
@@ -140,8 +144,8 @@ def _check_event_subtype(event_payload: dict, constraints: dict) -> bool:
140144
if not _matches(constraints["type"], event_payload["type"]):
141145
return False
142146
if "subtype" in constraints:
143-
expected_subtype: Union[
144-
str, Sequence[Optional[Union[str, Pattern]]]
147+
expected_subtype: Optional[
148+
Union[str, Sequence[Optional[Union[str, Pattern]]]]
145149
] = constraints["subtype"]
146150
if expected_subtype is None:
147151
# "subtype" in constraints is intentionally None for this pattern

0 commit comments

Comments
 (0)