Skip to content

Commit

Permalink
Improve #1092 #1084 by removing mutable method default arguments (#1093)
Browse files Browse the repository at this point in the history
  • Loading branch information
seratch authored Aug 13, 2021
1 parent 2b30100 commit c07dc7b
Show file tree
Hide file tree
Showing 14 changed files with 39 additions and 25 deletions.
6 changes: 4 additions & 2 deletions slack_sdk/audit_logs/v1/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def __init__(
user_agent_prefix: Optional[str] = None,
user_agent_suffix: Optional[str] = None,
logger: Optional[logging.Logger] = None,
retry_handlers: List[AsyncRetryHandler] = async_default_handlers(),
retry_handlers: Optional[List[AsyncRetryHandler]] = None,
):
"""API client for Audit Logs API
See https://api.slack.com/admins/audit-logs for more details
Expand Down Expand Up @@ -88,7 +88,9 @@ def __init__(
user_agent_prefix, user_agent_suffix
)
self.logger = logger if logger is not None else logging.getLogger(__name__)
self.retry_handlers = retry_handlers
self.retry_handlers = (
retry_handlers if retry_handlers is not None else async_default_handlers()
)

if self.proxy is None or len(self.proxy.strip()) == 0:
env_variable = load_http_proxy_from_env(self.logger)
Expand Down
6 changes: 4 additions & 2 deletions slack_sdk/audit_logs/v1/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __init__(
user_agent_prefix: Optional[str] = None,
user_agent_suffix: Optional[str] = None,
logger: Optional[logging.Logger] = None,
retry_handlers: List[RetryHandler] = default_retry_handlers(),
retry_handlers: Optional[List[RetryHandler]] = None,
):
"""API client for Audit Logs API
See https://api.slack.com/admins/audit-logs for more details
Expand All @@ -77,7 +77,9 @@ def __init__(
user_agent_prefix, user_agent_suffix
)
self.logger = logger if logger is not None else logging.getLogger(__name__)
self.retry_handlers = retry_handlers
self.retry_handlers = (
retry_handlers if retry_handlers is not None else default_retry_handlers()
)

if self.proxy is None or len(self.proxy.strip()) == 0:
env_variable = load_http_proxy_from_env(self.logger)
Expand Down
2 changes: 1 addition & 1 deletion slack_sdk/http_retry/builtin_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def _can_retry(
response: Optional[HttpResponse] = None,
error: Optional[Exception] = None,
) -> bool:
return response.status_code == 429
return response is not None and response.status_code == 429

def prepare_for_next_attempt(
self,
Expand Down
6 changes: 4 additions & 2 deletions slack_sdk/scim/v1/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def __init__(
user_agent_prefix: Optional[str] = None,
user_agent_suffix: Optional[str] = None,
logger: Optional[logging.Logger] = None,
retry_handlers: List[AsyncRetryHandler] = async_default_handlers(),
retry_handlers: Optional[List[AsyncRetryHandler]] = None,
):
"""API client for SCIM API
See https://api.slack.com/scim for more details
Expand Down Expand Up @@ -103,7 +103,9 @@ def __init__(
user_agent_prefix, user_agent_suffix
)
self.logger = logger if logger is not None else logging.getLogger(__name__)
self.retry_handlers = retry_handlers
self.retry_handlers = (
retry_handlers if retry_handlers is not None else async_default_handlers()
)

if self.proxy is None or len(self.proxy.strip()) == 0:
env_variable = load_http_proxy_from_env(self.logger)
Expand Down
6 changes: 4 additions & 2 deletions slack_sdk/scim/v1/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def __init__(
user_agent_prefix: Optional[str] = None,
user_agent_suffix: Optional[str] = None,
logger: Optional[logging.Logger] = None,
retry_handlers: List[RetryHandler] = default_retry_handlers(),
retry_handlers: Optional[List[RetryHandler]] = None,
):
"""API client for SCIM API
See https://api.slack.com/scim for more details
Expand All @@ -99,7 +99,9 @@ def __init__(
user_agent_prefix, user_agent_suffix
)
self.logger = logger if logger is not None else logging.getLogger(__name__)
self.retry_handlers = retry_handlers
self.retry_handlers = (
retry_handlers if retry_handlers is not None else default_retry_handlers()
)

if self.proxy is None or len(self.proxy.strip()) == 0:
env_variable = load_http_proxy_from_env(self.logger)
Expand Down
6 changes: 4 additions & 2 deletions slack_sdk/web/async_base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __init__(
# for Org-Wide App installation
team_id: Optional[str] = None,
logger: Optional[logging.Logger] = None,
retry_handlers: List[RetryHandler] = async_default_handlers(),
retry_handlers: Optional[List[RetryHandler]] = None,
):
self.token = None if token is None else token.strip()
self.base_url = base_url
Expand All @@ -59,7 +59,9 @@ def __init__(
if team_id is not None:
self.default_params["team_id"] = team_id
self._logger = logger if logger is not None else logging.getLogger(__name__)
self.retry_handlers = retry_handlers
self.retry_handlers = (
retry_handlers if retry_handlers is not None else async_default_handlers()
)

if self.proxy is None or len(self.proxy.strip()) == 0:
env_variable = load_http_proxy_from_env(self._logger)
Expand Down
6 changes: 4 additions & 2 deletions slack_sdk/web/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def __init__(
# for Org-Wide App installation
team_id: Optional[str] = None,
logger: Optional[logging.Logger] = None,
retry_handlers: List[RetryHandler] = default_retry_handlers(),
retry_handlers: Optional[List[RetryHandler]] = None,
):
self.token = None if token is None else token.strip()
self.base_url = base_url
Expand All @@ -70,7 +70,9 @@ def __init__(
self.default_params["team_id"] = team_id
self._logger = logger if logger is not None else logging.getLogger(__name__)

self.retry_handlers = retry_handlers
self.retry_handlers = (
retry_handlers if retry_handlers is not None else default_retry_handlers()
)

if self.proxy is None or len(self.proxy.strip()) == 0:
env_variable = load_http_proxy_from_env(self._logger)
Expand Down
6 changes: 4 additions & 2 deletions slack_sdk/webhook/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def __init__(
user_agent_prefix: Optional[str] = None,
user_agent_suffix: Optional[str] = None,
logger: Optional[logging.Logger] = None,
retry_handlers: List[AsyncRetryHandler] = async_default_handlers(),
retry_handlers: Optional[List[AsyncRetryHandler]] = None,
):
"""API client for Incoming Webhooks and `response_url`
Expand Down Expand Up @@ -80,7 +80,9 @@ def __init__(
user_agent_prefix, user_agent_suffix
)
self.logger = logger if logger is not None else logging.getLogger(__name__)
self.retry_handlers = retry_handlers
self.retry_handlers = (
retry_handlers if retry_handlers is not None else async_default_handlers()
)

if self.proxy is None or len(self.proxy.strip()) == 0:
env_variable = load_http_proxy_from_env(self.logger)
Expand Down
6 changes: 4 additions & 2 deletions slack_sdk/webhook/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def __init__(
user_agent_prefix: Optional[str] = None,
user_agent_suffix: Optional[str] = None,
logger: Optional[logging.Logger] = None,
retry_handlers: List[RetryHandler] = default_retry_handlers(),
retry_handlers: Optional[List[RetryHandler]] = None,
):
"""API client for Incoming Webhooks and `response_url`
Expand All @@ -70,7 +70,9 @@ def __init__(
user_agent_prefix, user_agent_suffix
)
self.logger = logger if logger is not None else logging.getLogger(__name__)
self.retry_handlers = retry_handlers
self.retry_handlers = (
retry_handlers if retry_handlers is not None else default_retry_handlers()
)

if self.proxy is None or len(self.proxy.strip()) == 0:
env_variable = load_http_proxy_from_env(self.logger)
Expand Down
2 changes: 1 addition & 1 deletion tests/slack_sdk/audit_logs/test_client_http_retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def test_rate_limited(self):
client = AuditLogsClient(
token="xoxp-ratelimited",
base_url="http://localhost:8888/",
retry_handlers=[RateLimitErrorRetryHandler()],
)
client.retry_handlers.append(RateLimitErrorRetryHandler())

response = client.actions()
# Just running retries; no assertions for call count so far
Expand Down
2 changes: 1 addition & 1 deletion tests/slack_sdk/scim/test_client_http_retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ def test_ratelimited(self):
client = SCIMClient(
base_url="http://localhost:8888/",
token="xoxp-ratelimited",
retry_handlers=[RateLimitErrorRetryHandler()],
)
client.retry_handlers.append(RateLimitErrorRetryHandler())

response = client.search_users(start_index=0, count=1)
# Just running retries; no assertions for call count so far
Expand Down
2 changes: 1 addition & 1 deletion tests/slack_sdk/web/test_web_client_http_retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ def test_ratelimited(self):
base_url="http://localhost:8888",
token="xoxp-ratelimited",
team_id="T111",
retry_handlers=[RateLimitErrorRetryHandler()],
)
client.retry_handlers.append(RateLimitErrorRetryHandler())
try:
client.auth_test()
self.fail("An exception is expected")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ def test_rate_limited(self):
base_url="http://localhost:8888",
token="xoxb-rate_limited_only_once",
team_id="T111",
retry_handlers=[rate_limit_error_retry_handler],
)
client.retry_handlers.append(rate_limit_error_retry_handler)
client.auth_test()
6 changes: 2 additions & 4 deletions tests/slack_sdk/webhook/test_webhook_http_retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@ def test_send(self):
self.assertEqual(2, retry_handler.call_count)

def test_ratelimited(self):
client = WebhookClient(
"http://localhost:8888/ratelimited",
retry_handlers=[RateLimitErrorRetryHandler()],
)
client = WebhookClient("http://localhost:8888/ratelimited")
client.retry_handlers.append(RateLimitErrorRetryHandler())
response = client.send(text="hello!")
# Just running retries; no assertions for call count so far
self.assertEqual(429, response.status_code)

0 comments on commit c07dc7b

Please sign in to comment.