Skip to content

Commit 0d77f8d

Browse files
health: move to mypy (#1620)
1 parent 7b04d01 commit 0d77f8d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+866
-842
lines changed

.github/workflows/mypy.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: mypy validation
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
timeout-minutes: 5
12+
strategy:
13+
matrix:
14+
python-version: ["3.13"]
15+
steps:
16+
- uses: actions/checkout@v4
17+
- name: Set up Python ${{ matrix.python-version }}
18+
uses: actions/setup-python@v5
19+
with:
20+
python-version: ${{ matrix.python-version }}
21+
- name: Run mypy verification
22+
run: |
23+
./scripts/run_mypy.sh

.github/workflows/pytype.yml

Lines changed: 0 additions & 31 deletions
This file was deleted.

pyproject.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,11 @@ filterwarnings = [
6969
"ignore:slack.* package is deprecated. Please use slack_sdk.* package instead.*:UserWarning",
7070
]
7171
asyncio_mode = "auto"
72+
73+
74+
[tool.mypy]
75+
files = "slack_sdk/"
76+
exclude = ["slack_sdk/scim", "slack_sdk/rtm"]
77+
force_union_syntax = true
78+
warn_unused_ignores = true
79+
enable_error_code = "ignore-without-code"

requirements/testing.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ psutil>=6.0.0,<7
1616
boto3<=2
1717
# For AWS tests
1818
moto>=4.0.13,<6
19+
mypy<=1.13.0

scripts/run_mypy.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
# ./scripts/run_mypy.sh
3+
4+
set -e
5+
6+
script_dir=$(dirname $0)
7+
cd ${script_dir}/..
8+
9+
pip install -U pip setuptools wheel
10+
pip install -r requirements/testing.txt \
11+
-r requirements/optional.txt
12+
13+
mypy --config-file pyproject.toml

slack_sdk/audit_logs/v1/async_client.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
Refer to https://slack.dev/python-slack-sdk/audit-logs/ for details.
44
"""
5+
56
import json
67
import logging
78
from ssl import SSLContext
@@ -225,7 +226,7 @@ async def _perform_http_request(
225226
headers: Dict[str, str],
226227
) -> AuditLogsResponse:
227228
if body_params is not None:
228-
body_params = json.dumps(body_params)
229+
body_params = json.dumps(body_params) # type: ignore[assignment]
229230
headers["Content-Type"] = "application/json;charset=utf-8"
230231

231232
session: Optional[ClientSession] = None
@@ -252,7 +253,7 @@ async def _perform_http_request(
252253
retry_request = RetryHttpRequest(
253254
method=http_verb,
254255
url=url,
255-
headers=headers,
256+
headers=headers, # type: ignore[arg-type]
256257
body_params=body_params,
257258
)
258259

@@ -278,19 +279,19 @@ async def _perform_http_request(
278279
)
279280

280281
try:
281-
async with session.request(http_verb, url, **request_kwargs) as res:
282+
async with session.request(http_verb, url, **request_kwargs) as res: # type: ignore[arg-type, union-attr] # noqa: E501
282283
try:
283284
response_body = await res.text()
284285
retry_response = RetryHttpResponse(
285286
status_code=res.status,
286-
headers=res.headers,
287+
headers=res.headers, # type: ignore[arg-type]
287288
data=response_body.encode("utf-8") if response_body is not None else None,
288289
)
289290
except aiohttp.ContentTypeError:
290291
self.logger.debug(f"No response data returned from the following API call: {url}.")
291292
retry_response = RetryHttpResponse(
292293
status_code=res.status,
293-
headers=res.headers,
294+
headers=res.headers, # type: ignore[arg-type]
294295
)
295296
except json.decoder.JSONDecodeError as e:
296297
message = f"Failed to parse the response body: {str(e)}"
@@ -320,7 +321,7 @@ async def _perform_http_request(
320321
url=url,
321322
status_code=res.status,
322323
raw_body=response_body,
323-
headers=res.headers,
324+
headers=res.headers, # type: ignore[arg-type]
324325
)
325326
_debug_log_response(self.logger, resp)
326327
return resp
@@ -351,10 +352,10 @@ async def _perform_http_request(
351352

352353
if resp is not None:
353354
return resp
354-
raise last_error
355+
raise last_error # type: ignore[misc]
355356

356357
finally:
357358
if not use_running_session:
358-
await session.close()
359+
await session.close() # type: ignore[union-attr]
359360

360361
return resp

slack_sdk/audit_logs/v1/client.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
Refer to https://slack.dev/python-slack-sdk/audit-logs/ for details.
44
"""
5+
56
import json
67
import logging
78
import urllib
@@ -217,7 +218,7 @@ def _perform_http_request(
217218
headers: Dict[str, str],
218219
) -> AuditLogsResponse:
219220
if body is not None:
220-
body = json.dumps(body)
221+
body = json.dumps(body) # type: ignore[assignment]
221222
headers["Content-Type"] = "application/json;charset=utf-8"
222223

223224
if self.logger.level <= logging.DEBUG:
@@ -229,7 +230,7 @@ def _perform_http_request(
229230
req = Request(
230231
method=http_verb,
231232
url=url,
232-
data=body.encode("utf-8") if body is not None else None,
233+
data=body.encode("utf-8") if body is not None else None, # type: ignore[attr-defined]
233234
headers=headers,
234235
)
235236
resp = None
@@ -327,7 +328,7 @@ def _perform_http_request(
327328

328329
if resp is not None:
329330
return resp
330-
raise last_error
331+
raise last_error # type: ignore[misc]
331332

332333
def _perform_http_request_internal(self, url: str, req: Request) -> AuditLogsResponse:
333334
opener: Optional[OpenerDirector] = None
@@ -344,19 +345,18 @@ def _perform_http_request_internal(self, url: str, req: Request) -> AuditLogsRes
344345
else:
345346
raise SlackRequestError(f"Invalid URL detected: {url}")
346347

347-
# NOTE: BAN-B310 is already checked above
348-
http_resp: Optional[HTTPResponse] = None
348+
http_resp: HTTPResponse
349349
if opener:
350-
http_resp = opener.open(req, timeout=self.timeout) # skipcq: BAN-B310
350+
http_resp = opener.open(req, timeout=self.timeout)
351351
else:
352-
http_resp = urlopen(req, context=self.ssl, timeout=self.timeout) # skipcq: BAN-B310
352+
http_resp = urlopen(req, context=self.ssl, timeout=self.timeout)
353353
charset: str = http_resp.headers.get_content_charset() or "utf-8"
354354
response_body: str = http_resp.read().decode(charset)
355355
resp = AuditLogsResponse(
356356
url=url,
357357
status_code=http_resp.status,
358358
raw_body=response_body,
359-
headers=http_resp.headers,
359+
headers=http_resp.headers, # type: ignore[arg-type]
360360
)
361361
_debug_log_response(self.logger, resp)
362362
return resp

slack_sdk/audit_logs/v1/logs.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ def __init__(
804804
self.attributes = []
805805
for a in attributes:
806806
if isinstance(a, dict):
807-
self.attributes.append(Attribute(**a))
807+
self.attributes.append(Attribute(**a)) # type: ignore[arg-type]
808808
else:
809809
self.attributes.append(a)
810810
self.channel = channel
@@ -822,11 +822,11 @@ def __init__(
822822
self.rules_checked = None
823823
if rules_checked is not None:
824824
self.rules_checked = []
825-
for a in rules_checked:
825+
for a in rules_checked: # type: ignore[assignment]
826826
if isinstance(a, dict):
827-
self.rules_checked.append(AAARule(**a))
827+
self.rules_checked.append(AAARule(**a)) # type: ignore[arg-type]
828828
else:
829-
self.rules_checked.append(a)
829+
self.rules_checked.append(a) # type: ignore[arg-type]
830830
self.disconnecting_team = disconnecting_team
831831
self.is_channel_canvas = is_channel_canvas
832832
self.linked_channel_id = linked_channel_id
@@ -1021,7 +1021,7 @@ def __init__(
10211021
class WorkflowV2StepConfiguration:
10221022
name: Optional[str]
10231023
step_function_type: Optional[str]
1024-
step_function_app_id: Optional[int]
1024+
step_function_app_id: Optional[str]
10251025
unknown_fields: Dict[str, Any]
10261026

10271027
def __init__(
@@ -1235,7 +1235,7 @@ def __init__(
12351235
provided: Optional[str] = None,
12361236
**kwargs,
12371237
) -> None:
1238-
self.entries = [Entry(**e) if isinstance(e, dict) else e for e in entries]
1238+
self.entries = [Entry(**e) if isinstance(e, dict) else e for e in entries] # type: ignore[union-attr]
12391239
self.response_metadata = (
12401240
ResponseMetadata(**response_metadata) if isinstance(response_metadata, dict) else response_metadata
12411241
)

slack_sdk/audit_logs/v1/response.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ class AuditLogsResponse:
1313
body: Optional[Dict[str, Any]]
1414
typed_body: Optional[LogsResponse]
1515

16-
@property
17-
def typed_body(self) -> Optional[LogsResponse]: # type: ignore
16+
@property # type: ignore[no-redef]
17+
def typed_body(self) -> Optional[LogsResponse]:
1818
if self.body is None:
1919
return None
2020
return LogsResponse(**self.body)

slack_sdk/http_retry/builtin_async_handlers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ async def prepare_for_next_attempt_async(
6868
error: Optional[Exception] = None,
6969
) -> None:
7070
if response is None:
71-
raise error
71+
raise error # type: ignore[misc]
7272

7373
state.next_attempt_requested = True
7474
retry_after_header_name: Optional[str] = None
@@ -79,9 +79,9 @@ async def prepare_for_next_attempt_async(
7979
duration = 1
8080
if retry_after_header_name is None:
8181
# This situation usually does not arise. Just in case.
82-
duration += random.random()
82+
duration += random.random() # type: ignore[assignment]
8383
else:
84-
duration = int(response.headers.get(retry_after_header_name)[0]) + random.random()
84+
duration = int(response.headers.get(retry_after_header_name)[0]) + random.random() # type: ignore[assignment, index] # noqa: E501
8585
await asyncio.sleep(duration)
8686
state.increment_current_attempt()
8787

0 commit comments

Comments
 (0)