Skip to content

Commit dce45c5

Browse files
authored
Fix #370 by adding an alias of next arg (next_) in middleware arguments (#394)
1 parent c61206d commit dce45c5

23 files changed

+198
-3
lines changed

slack_bolt/kwargs_injection/args.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ def handle_buttons(ack, respond, logger, context, body, client):
7272
# middleware
7373
next: Callable[[], None]
7474
"""`next()` utility function, which tells the middleware chain that it can continue with the next one"""
75+
next_: Callable[[], None]
76+
"""An alias of `next()` for avoiding the Python built-in method overrides in middleware functions"""
7577

7678
def __init__(
7779
self,
@@ -93,6 +95,9 @@ def __init__(
9395
ack: Ack,
9496
say: Say,
9597
respond: Respond,
98+
# As this method is not supposed to be invoked by bolt-python users,
99+
# the naming conflict with the built-in one affects
100+
# only the internals of this method
96101
next: Callable[[], None],
97102
**kwargs # noqa
98103
):
@@ -116,3 +121,4 @@ def __init__(
116121
self.say: Say = say
117122
self.respond: Respond = respond
118123
self.next: Callable[[], None] = next
124+
self.next_: Callable[[], None] = next

slack_bolt/kwargs_injection/async_args.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ async def handle_buttons(ack, respond, logger, context, body, client):
7171
# middleware
7272
next: Callable[[], Awaitable[None]]
7373
"""`next()` utility function, which tells the middleware chain that it can continue with the next one"""
74+
next_: Callable[[], Awaitable[None]]
75+
"""An alias of `next()` for avoiding the Python built-in method overrides in middleware functions"""
7476

7577
def __init__(
7678
self,
@@ -115,3 +117,4 @@ def __init__(
115117
self.say: AsyncSay = say
116118
self.respond: AsyncRespond = respond
117119
self.next: Callable[[], Awaitable[None]] = next
120+
self.next_: Callable[[], Awaitable[None]] = next

slack_bolt/kwargs_injection/async_utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def build_async_required_kwargs(
5252
"respond": request.context.respond,
5353
# middleware
5454
"next": next_func,
55+
"next_": next_func, # for the middleware using Python's built-in `next()` function
5556
}
5657
all_available_args["payload"] = (
5758
all_available_args["options"]

slack_bolt/kwargs_injection/utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def build_required_kwargs(
5252
"respond": request.context.respond,
5353
# middleware
5454
"next": next_func,
55+
"next_": next_func, # for the middleware using Python's built-in `next()` function
5556
}
5657
all_available_args["payload"] = (
5758
all_available_args["options"]

slack_bolt/listener/listener.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ def run_middleware(
4545
for m in self.middleware:
4646
middleware_state = {"next_called": False}
4747

48-
def next():
48+
def next_():
4949
middleware_state["next_called"] = True
5050

51-
resp = m.process(req=req, resp=resp, next=next)
51+
resp = m.process(req=req, resp=resp, next=next_)
5252
if not middleware_state["next_called"]:
5353
# next() was not called in this middleware
5454
return (resp, True)

slack_bolt/logger/messages.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def warning_unhandled_by_global_middleware( # type: ignore
9999
name: str, req: Union[BoltRequest, "AsyncBoltRequest"] # type: ignore
100100
) -> str: # type: ignore
101101
return (
102-
f"A global middleware ({name}) skipped calling `next()` "
102+
f"A global middleware ({name}) skipped calling either `next()` or `next_()` "
103103
f"without providing a response for the request ({req.body})"
104104
)
105105

slack_bolt/middleware/async_custom_middleware.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ async def async_process(
3131
*,
3232
req: AsyncBoltRequest,
3333
resp: BoltResponse,
34+
# As this method is not supposed to be invoked by bolt-python users,
35+
# the naming conflict with the built-in one affects
36+
# only the internals of this method
3437
next: Callable[[], Awaitable[BoltResponse]],
3538
) -> BoltResponse:
3639
return await self.func(

slack_bolt/middleware/async_middleware.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ async def async_process(
1414
*,
1515
req: AsyncBoltRequest,
1616
resp: BoltResponse,
17+
# As this method is not supposed to be invoked by bolt-python users,
18+
# the naming conflict with the built-in one affects
19+
# only the internals of this method
1720
next: Callable[[], Awaitable[BoltResponse]],
1821
) -> Optional[BoltResponse]:
1922
"""Processes a request data before other middleware and listeners.
@@ -24,6 +27,14 @@ async def simple_middleware(req, resp, next):
2427
# do something here
2528
await next()
2629
30+
This `async_process(req, resp, next)` method is supposed to be invoked only inside bolt-python.
31+
If you want to avoid the name `next()` in your middleware functions, you can use `next_()` method instead.
32+
33+
@app.middleware
34+
async def simple_middleware(req, resp, next_):
35+
# do something here
36+
await next_()
37+
2738
Args:
2839
req: The incoming request
2940
resp: The response

slack_bolt/middleware/authorization/async_multi_teams_authorization.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ async def async_process(
2828
*,
2929
req: AsyncBoltRequest,
3030
resp: BoltResponse,
31+
# As this method is not supposed to be invoked by bolt-python users,
32+
# the naming conflict with the built-in one affects
33+
# only the internals of this method
3134
next: Callable[[], Awaitable[BoltResponse]],
3235
) -> BoltResponse:
3336
if _is_no_auth_required(req):

slack_bolt/middleware/authorization/async_single_team_authorization.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ async def async_process(
2222
*,
2323
req: AsyncBoltRequest,
2424
resp: BoltResponse,
25+
# As this method is not supposed to be invoked by bolt-python users,
26+
# the naming conflict with the built-in one affects
27+
# only the internals of this method
2528
next: Callable[[], Awaitable[BoltResponse]],
2629
) -> BoltResponse:
2730
if _is_no_auth_required(req):

0 commit comments

Comments
 (0)