Skip to content

Commit ad622e4

Browse files
authored
Fix #312 Typehint errors with pytype 2021.4.26 (#313)
1 parent 39194ae commit ad622e4

File tree

6 files changed

+48
-19
lines changed

6 files changed

+48
-19
lines changed

.github/workflows/ci-build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ jobs:
7474
if [ ${python_version:7:3} == "3.8" ]; then
7575
pip install -e ".[async]"
7676
pip install -e ".[adapter]"
77-
pip install "pytype==2021.4.15" && pytype slack_bolt/
77+
pip install "pytype" && pytype slack_bolt/
7878
fi
7979
- name: Run all tests for codecov (3.9 only)
8080
run: |

slack_bolt/oauth/async_callback_options.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,10 @@ def __init__(
9191
state_utils=state_utils,
9292
redirect_uri_page_renderer=redirect_uri_page_renderer,
9393
)
94-
self.success = self._success_handler
95-
self.failure = self._failure_handler
94+
# Note that pytype 2021.4.26 misunderstands these assignments.
95+
# Thus, we put "type: ignore" for the following two lines
96+
self.success = self._success_handler # type: ignore
97+
self.failure = self._failure_handler # type: ignore
9698

9799
# --------------------------
98100
# Internal methods

slack_bolt/oauth/async_oauth_settings.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,14 @@ def __init__(
103103
logger: The logger that will be used internally
104104
"""
105105
# OAuth flow parameters/credentials
106-
self.client_id = client_id or os.environ.get("SLACK_CLIENT_ID")
107-
self.client_secret = client_secret or os.environ.get(
108-
"SLACK_CLIENT_SECRET", None
106+
client_id: Optional[str] = client_id or os.environ.get("SLACK_CLIENT_ID")
107+
client_secret: Optional[str] = client_secret or os.environ.get(
108+
"SLACK_CLIENT_SECRET"
109109
)
110-
if self.client_id is None or self.client_secret is None:
110+
if client_id is None or client_secret is None:
111111
raise BoltError("Both client_id and client_secret are required")
112+
self.client_id = client_id
113+
self.client_secret = client_secret
112114

113115
self.scopes = scopes or os.environ.get("SLACK_SCOPES", "").split(",")
114116
if isinstance(self.scopes, str):

slack_bolt/oauth/oauth_settings.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,14 @@ def __init__(
9797
state_expiration_seconds: The seconds that the state value is alive (Default: 600 seconds)
9898
logger: The logger that will be used internally
9999
"""
100-
self.client_id = client_id or os.environ.get("SLACK_CLIENT_ID")
101-
self.client_secret = client_secret or os.environ.get(
102-
"SLACK_CLIENT_SECRET", None
100+
client_id: Optional[str] = client_id or os.environ.get("SLACK_CLIENT_ID")
101+
client_secret: Optional[str] = client_secret or os.environ.get(
102+
"SLACK_CLIENT_SECRET"
103103
)
104-
if self.client_id is None or self.client_secret is None:
104+
if client_id is None or client_secret is None:
105105
raise BoltError("Both client_id and client_secret are required")
106+
self.client_id = client_id
107+
self.client_secret = client_secret
106108

107109
self.scopes = scopes or os.environ.get("SLACK_SCOPES", "").split(",")
108110
if isinstance(self.scopes, str):

slack_bolt/request/async_request.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,21 @@ def __init__(
4242
context: The context in this request.
4343
mode: The mode used for this request. (either "http" or "socket_mode")
4444
"""
45-
if mode == "http" and not isinstance(body, str):
46-
raise BoltError(error_message_raw_body_required_in_http_mode())
47-
self.raw_body = body if mode == "http" else ""
45+
46+
if mode == "http":
47+
# HTTP Mode
48+
if not isinstance(body, str):
49+
raise BoltError(error_message_raw_body_required_in_http_mode())
50+
self.raw_body = body if body is not None else ""
51+
else:
52+
# Socket Mode
53+
if body is not None and isinstance(body, str):
54+
self.raw_body = body
55+
else:
56+
# We don't convert the dict value to str
57+
# as doing so does not guarantee to keep the original structure/format.
58+
self.raw_body = ""
59+
4860
self.query = parse_query(query)
4961
self.headers = build_normalized_headers(headers)
5062
self.content_type = extract_content_type(self.headers)
@@ -57,7 +69,7 @@ def __init__(
5769
self.context = build_async_context(
5870
AsyncBoltContext(context if context else {}), self.body
5971
)
60-
self.lazy_only = self.headers.get("x-slack-bolt-lazy-only", [False])[0]
72+
self.lazy_only = bool(self.headers.get("x-slack-bolt-lazy-only", [False])[0])
6173
self.lazy_function_name = self.headers.get(
6274
"x-slack-bolt-lazy-function-name", [None]
6375
)[0]

slack_bolt/request/request.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,20 @@ def __init__(
4242
context: The context in this request.
4343
mode: The mode used for this request. (either "http" or "socket_mode")
4444
"""
45-
if mode == "http" and not isinstance(body, str):
46-
raise BoltError(error_message_raw_body_required_in_http_mode())
47-
self.raw_body = body if mode == "http" else ""
45+
if mode == "http":
46+
# HTTP Mode
47+
if not isinstance(body, str):
48+
raise BoltError(error_message_raw_body_required_in_http_mode())
49+
self.raw_body = body if body is not None else ""
50+
else:
51+
# Socket Mode
52+
if body is not None and isinstance(body, str):
53+
self.raw_body = body
54+
else:
55+
# We don't convert the dict value to str
56+
# as doing so does not guarantee to keep the original structure/format.
57+
self.raw_body = ""
58+
4859
self.query = parse_query(query)
4960
self.headers = build_normalized_headers(headers)
5061
self.content_type = extract_content_type(self.headers)
@@ -56,7 +67,7 @@ def __init__(
5667
raise BoltError(error_message_unknown_request_body_type())
5768

5869
self.context = build_context(BoltContext(context if context else {}), self.body)
59-
self.lazy_only = self.headers.get("x-slack-bolt-lazy-only", [False])[0]
70+
self.lazy_only = bool(self.headers.get("x-slack-bolt-lazy-only", [False])[0])
6071
self.lazy_function_name = self.headers.get(
6172
"x-slack-bolt-lazy-function-name", [None]
6273
)[0]

0 commit comments

Comments
 (0)