Skip to content

Commit 8067096

Browse files
authored
fix: Improve type annotations in SignatureVerifier
Receive headers as abstract/immutable `collections.abc.Mapping` instead of `Dict` to more accurately declare compatibility with dict-like objects (common for holding headers in HTTP frameworks), and mark timestamp/signature args as `Optional` in `is_valid` to reflect reality and fix the typechecker errors in `is_valid_request`.
1 parent 0cb9728 commit 8067096

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

slack_sdk/signature/__init__.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import hashlib
44
import hmac
5+
from collections.abc import Mapping
56
from time import time
6-
from typing import Dict, Optional, Union
7+
from typing import Optional, Union
78

89

910
class Clock:
@@ -26,23 +27,23 @@ def __init__(self, signing_secret: str, clock: Clock = Clock()):
2627
def is_valid_request(
2728
self,
2829
body: Union[str, bytes],
29-
headers: Dict[str, str],
30+
headers: Mapping[str, str],
3031
) -> bool:
3132
"""Verifies if the given signature is valid"""
3233
if headers is None:
3334
return False
3435
normalized_headers = {k.lower(): v for k, v in headers.items()}
3536
return self.is_valid(
3637
body=body,
37-
timestamp=normalized_headers.get("x-slack-request-timestamp", None), # type: ignore[arg-type]
38-
signature=normalized_headers.get("x-slack-signature", None), # type: ignore[arg-type]
38+
timestamp=normalized_headers.get("x-slack-request-timestamp", None),
39+
signature=normalized_headers.get("x-slack-signature", None),
3940
)
4041

4142
def is_valid(
4243
self,
4344
body: Union[str, bytes],
44-
timestamp: str,
45-
signature: str,
45+
timestamp: Optional[str],
46+
signature: Optional[str],
4647
) -> bool:
4748
"""Verifies if the given signature is valid"""
4849
if timestamp is None or signature is None:

0 commit comments

Comments
 (0)