Skip to content

Commit 8908885

Browse files
chore: improve type checking behavior (#1470)
1 parent f11dbfb commit 8908885

File tree

10 files changed

+58
-28
lines changed

10 files changed

+58
-28
lines changed

.github/workflows/ci-build.yml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,19 @@ jobs:
4444
uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
4545
with:
4646
python-version: ${{ env.LATEST_SUPPORTED_PY }}
47-
- name: Run mypy verification
48-
run: ./scripts/run_mypy.sh
47+
- name: Install synchronous dependencies
48+
run: |
49+
pip install -U pip
50+
pip install -U .
51+
pip install -r requirements/tools.txt
52+
- name: Type check synchronous modules
53+
run: mypy --config-file pyproject.toml --exclude "async_|/adapter/"
54+
- name: Install async and adapter dependencies
55+
run: |
56+
pip install -r requirements/async.txt
57+
pip install -r requirements/adapter.txt
58+
- name: Type check all modules
59+
run: mypy --config-file pyproject.toml
4960

5061
unittest:
5162
name: Unit tests

slack_bolt/app/async_server.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
import logging
2-
from typing import Optional
2+
from typing import Optional, TYPE_CHECKING
33

44
from aiohttp import web
55

66
from slack_bolt.adapter.aiohttp import to_bolt_request, to_aiohttp_response
77
from slack_bolt.response import BoltResponse
88
from slack_bolt.util.utils import get_boot_message
99

10+
if TYPE_CHECKING:
11+
from slack_bolt.app.async_app import AsyncApp
12+
1013

1114
class AsyncSlackAppServer:
1215
port: int
1316
path: str
1417
host: str
15-
bolt_app: "AsyncApp" # type: ignore[name-defined]
18+
bolt_app: "AsyncApp"
1619
web_app: web.Application
1720

1821
def __init__(
1922
self,
2023
port: int,
2124
path: str,
22-
app: "AsyncApp", # type: ignore[name-defined]
25+
app: "AsyncApp",
2326
host: Optional[str] = None,
2427
):
2528
"""Standalone AIOHTTP Web Server.
@@ -34,7 +37,7 @@ def __init__(
3437
self.port = port
3538
self.path = path
3639
self.host = host if host is not None else "0.0.0.0"
37-
self.bolt_app: "AsyncApp" = app # type: ignore[name-defined]
40+
self.bolt_app: "AsyncApp" = app
3841
self.web_app = web.Application()
3942
self._bolt_oauth_flow = self.bolt_app.oauth_flow
4043
if self._bolt_oauth_flow:

slack_bolt/context/async_context.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Optional
1+
from typing import Optional, TYPE_CHECKING
22

33
from slack_sdk.web.async_client import AsyncWebClient
44

@@ -16,6 +16,9 @@
1616
from slack_bolt.context.set_title.async_set_title import AsyncSetTitle
1717
from slack_bolt.util.utils import create_copy
1818

19+
if TYPE_CHECKING:
20+
from slack_bolt.listener.asyncio_runner import AsyncioListenerRunner
21+
1922

2023
class AsyncBoltContext(BaseContext):
2124
"""Context object associated with a request from Slack."""
@@ -42,7 +45,7 @@ def to_copyable(self) -> "AsyncBoltContext":
4245

4346
# The return type is intentionally string to avoid circular imports
4447
@property
45-
def listener_runner(self) -> "AsyncioListenerRunner": # type: ignore[name-defined]
48+
def listener_runner(self) -> "AsyncioListenerRunner":
4649
"""The properly configured listener_runner that is available for middleware/listeners."""
4750
return self["listener_runner"]
4851

slack_bolt/context/context.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Optional
1+
from typing import Optional, TYPE_CHECKING
22

33
from slack_sdk import WebClient
44

@@ -16,6 +16,9 @@
1616
from slack_bolt.context.set_title import SetTitle
1717
from slack_bolt.util.utils import create_copy
1818

19+
if TYPE_CHECKING:
20+
from slack_bolt.listener.thread_runner import ThreadListenerRunner
21+
1922

2023
class BoltContext(BaseContext):
2124
"""Context object associated with a request from Slack."""
@@ -43,7 +46,7 @@ def to_copyable(self) -> "BoltContext":
4346

4447
# The return type is intentionally string to avoid circular imports
4548
@property
46-
def listener_runner(self) -> "ThreadListenerRunner": # type: ignore[name-defined]
49+
def listener_runner(self) -> "ThreadListenerRunner":
4750
"""The properly configured listener_runner that is available for middleware/listeners."""
4851
return self["listener_runner"]
4952

slack_bolt/listener/async_listener_error_handler.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ async def handle(
4848
)
4949
returned_response = await self.func(**kwargs)
5050
if returned_response is not None and isinstance(returned_response, BoltResponse):
51-
response.status = returned_response.status # type: ignore[union-attr]
52-
response.headers = returned_response.headers # type: ignore[union-attr]
53-
response.body = returned_response.body # type: ignore[union-attr]
51+
assert response is not None, "response must be provided when returning a BoltResponse from an error handler"
52+
response.status = returned_response.status
53+
response.headers = returned_response.headers
54+
response.body = returned_response.body
5455

5556

5657
class AsyncDefaultListenerErrorHandler(AsyncListenerErrorHandler):

slack_bolt/listener/listener_error_handler.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ def handle(
4848
)
4949
returned_response = self.func(**kwargs)
5050
if returned_response is not None and isinstance(returned_response, BoltResponse):
51-
response.status = returned_response.status # type: ignore[union-attr]
52-
response.headers = returned_response.headers # type: ignore[union-attr]
53-
response.body = returned_response.body # type: ignore[union-attr]
51+
assert response is not None, "response must be provided when returning a BoltResponse from an error handler"
52+
response.status = returned_response.status
53+
response.headers = returned_response.headers
54+
response.body = returned_response.body
5455

5556

5657
class DefaultListenerErrorHandler(ListenerErrorHandler):

slack_bolt/middleware/async_middleware_error_handler.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ async def handle(
4848
)
4949
returned_response = await self.func(**kwargs)
5050
if returned_response is not None and isinstance(returned_response, BoltResponse):
51-
response.status = returned_response.status # type: ignore[union-attr]
52-
response.headers = returned_response.headers # type: ignore[union-attr]
53-
response.body = returned_response.body # type: ignore[union-attr]
51+
assert response is not None, "response must be provided when returning a BoltResponse from an error handler"
52+
response.status = returned_response.status
53+
response.headers = returned_response.headers
54+
response.body = returned_response.body
5455

5556

5657
class AsyncDefaultMiddlewareErrorHandler(AsyncMiddlewareErrorHandler):

slack_bolt/middleware/middleware_error_handler.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ def handle(
4848
)
4949
returned_response = self.func(**kwargs)
5050
if returned_response is not None and isinstance(returned_response, BoltResponse):
51-
response.status = returned_response.status # type: ignore[union-attr]
52-
response.headers = returned_response.headers # type: ignore[union-attr]
53-
response.body = returned_response.body # type: ignore[union-attr]
51+
assert response is not None, "response must be provided when returning a BoltResponse from an error handler"
52+
response.status = returned_response.status
53+
response.headers = returned_response.headers
54+
response.body = returned_response.body
5455

5556

5657
class DefaultMiddlewareErrorHandler(MiddlewareErrorHandler):

slack_bolt/oauth/async_callback_options.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22
from logging import Logger
3-
from typing import Optional, Callable, Awaitable
3+
from typing import Optional, Callable, Awaitable, TYPE_CHECKING
44

55
from slack_sdk.oauth import RedirectUriPageRenderer, OAuthStateUtils
66
from slack_sdk.oauth.installation_store import Installation
@@ -9,14 +9,17 @@
99
from slack_bolt.request.async_request import AsyncBoltRequest
1010
from slack_bolt.response import BoltResponse
1111

12+
if TYPE_CHECKING:
13+
from slack_bolt.oauth.async_oauth_settings import AsyncOAuthSettings
14+
1215

1316
class AsyncSuccessArgs:
1417
def __init__(
1518
self,
1619
*,
1720
request: AsyncBoltRequest,
1821
installation: Installation,
19-
settings: "AsyncOAuthSettings", # type: ignore[name-defined]
22+
settings: "AsyncOAuthSettings",
2023
default: "AsyncCallbackOptions",
2124
):
2225
"""The arguments for a success function.
@@ -41,7 +44,7 @@ def __init__(
4144
reason: str,
4245
error: Optional[Exception] = None,
4346
suggested_status_code: int,
44-
settings: "AsyncOAuthSettings", # type: ignore[name-defined]
47+
settings: "AsyncOAuthSettings",
4548
default: "AsyncCallbackOptions",
4649
):
4750
"""The arguments for a failure function.

slack_bolt/oauth/callback_options.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22
from logging import Logger
3-
from typing import Optional, Callable
3+
from typing import Optional, Callable, TYPE_CHECKING
44

55
from slack_sdk.oauth import RedirectUriPageRenderer, OAuthStateUtils
66
from slack_sdk.oauth.installation_store import Installation
@@ -9,14 +9,17 @@
99
from slack_bolt.request import BoltRequest
1010
from slack_bolt.response import BoltResponse
1111

12+
if TYPE_CHECKING:
13+
from slack_bolt.oauth.oauth_settings import OAuthSettings
14+
1215

1316
class SuccessArgs:
1417
def __init__(
1518
self,
1619
*,
1720
request: BoltRequest,
1821
installation: Installation,
19-
settings: "OAuthSettings", # type: ignore[name-defined]
22+
settings: "OAuthSettings",
2023
default: "CallbackOptions",
2124
):
2225
"""The arguments for a success function.
@@ -41,7 +44,7 @@ def __init__(
4144
reason: str,
4245
error: Optional[Exception] = None,
4346
suggested_status_code: int,
44-
settings: "OAuthSettings", # type: ignore[name-defined]
47+
settings: "OAuthSettings",
4548
default: "CallbackOptions",
4649
):
4750
"""The arguments for a failure function.

0 commit comments

Comments
 (0)