Skip to content

Commit

Permalink
Add aiohttp version validation (ref slackapi#912)
Browse files Browse the repository at this point in the history
  • Loading branch information
seratch committed Jan 12, 2021
1 parent e05af1f commit 0d06add
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,9 @@ def run(self):
"optional": [
# async modules depend on aiohttp
"aiodns>1.0",
"aiohttp>=3,<4",
# We recommend using 3.7.1+ for RTMClient
# https://github.com/slackapi/python-slack-sdk/issues/912
"aiohttp>=3.7.3,<4",
# used only under slack_sdk/*_store
"boto3<=2",
# InstallationStore/OAuthStateStore
Expand Down
23 changes: 23 additions & 0 deletions slack_sdk/aiohttp_version_checker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import logging
from typing import Callable


def _print_warning_log(message: str) -> None:
logging.getLogger(__name__).warning(message)


def validate_aiohttp_version(
aiohttp_version: str, print_warning: Callable[[str], None] = _print_warning_log,
):
if aiohttp_version is not None:
elements = aiohttp_version.split(".")
if len(elements) >= 3:
# patch version can be a non-numeric value
major, minor, patch = int(elements[0]), int(elements[1]), elements[2]
if major <= 2 or (
major == 3 and (minor == 6 or (minor == 7 and patch == "0"))
):
print_warning(
"We highly recommend upgrading aiohttp to 3.7.3 or higher versions."
"An older version of the library may not work with the Slack server-side in the future."
)
4 changes: 4 additions & 0 deletions slack_sdk/rtm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@
import aiohttp

import slack_sdk.errors as client_err
from slack_sdk.aiohttp_version_checker import validate_aiohttp_version
from slack_sdk.web.legacy_client import LegacyWebClient as WebClient


validate_aiohttp_version(aiohttp.__version__)


class RTMClient(object): # skipcq: PYL-R0205
"""An RTMClient allows apps to communicate with the Slack Platform's RTM API.
Expand Down
42 changes: 42 additions & 0 deletions tests/test_aiohttp_version_checker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import logging
import unittest

from slack_sdk.aiohttp_version_checker import validate_aiohttp_version


class TestAiohttpVersionChecker(unittest.TestCase):
def setUp(self):
self.logger = logging.getLogger(__name__)

def tearDown(self):
pass

def test_not_recommended_versions(self):
state = {"counter": 0}

def print(message: str):
state["counter"] = state["counter"] + 1

validate_aiohttp_version("2.1.3", print)
self.assertEqual(state["counter"], 1)
validate_aiohttp_version("3.6.3", print)
self.assertEqual(state["counter"], 2)
validate_aiohttp_version("3.7.0", print)
self.assertEqual(state["counter"], 3)

def test_recommended_versions(self):
state = {"counter": 0}

def print(message: str):
state["counter"] = state["counter"] + 1

validate_aiohttp_version("3.7.1", print)
self.assertEqual(state["counter"], 0)
validate_aiohttp_version("3.7.3", print)
self.assertEqual(state["counter"], 0)
validate_aiohttp_version("3.8.0", print)
self.assertEqual(state["counter"], 0)
validate_aiohttp_version("4.0.0", print)
self.assertEqual(state["counter"], 0)
validate_aiohttp_version("4.0.0rc1", print)
self.assertEqual(state["counter"], 0)

0 comments on commit 0d06add

Please sign in to comment.