Skip to content

Commit

Permalink
Enable pytype (slackapi#39)
Browse files Browse the repository at this point in the history
* Enable pytype
  • Loading branch information
seratch authored Aug 21, 2020
1 parent df0a3c9 commit 976d840
Show file tree
Hide file tree
Showing 48 changed files with 140 additions and 111 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ logs/

.env*
*.db

.pytype/
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ python:
- "3.6"
- "3.7"
- "3.8"
- "3.8-dev" # 3.8 development branch
- "nightly" # nightly build
install:
- python setup.py install
- pip install -U pip
- pip install "pytest>=5,<6"
- pip install "pytype"
script:
# testing without aiohttp
- travis_retry pytest tests/scenario_tests/
Expand All @@ -18,3 +17,5 @@ script:
- travis_retry pytest tests/async_scenario_tests/
# run all tests just in case
- travis_retry python setup.py test
# Run pytype only for Python 3.8
- if [ ${TRAVIS_PYTHON_VERSION:0:3} == "3.8" ]; then pip install -e ".[adapter]"; pytype slack_bolt/; fi
2 changes: 1 addition & 1 deletion samples/fastapi/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
fastapi>=0.54,<0.55
fastapi<1
uvicorn<1
3 changes: 2 additions & 1 deletion scripts/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ cd ${script_dir}/..

pip install -e ".[testing]" && \
black slack_bolt/ tests/ && \
pytest $1
pytest $1 && \
pytype slack_bolt/
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"pytest-asyncio<1", # for async
"aiohttp>=3,<4", # for async
"black==19.10b0",
"pytype",
]

setuptools.setup(
Expand Down Expand Up @@ -58,7 +59,7 @@
"click>=7,<8", # for chalice
"Django>=3,<4",
"falcon>=2,<3",
"fastapi>=0.54,<0.55",
"fastapi<1",
"Flask>=1,<2",
"pyramid>=1,<2",
"sanic>=20,<21",
Expand Down
2 changes: 1 addition & 1 deletion slack_bolt/adapter/aws_lambda/chalice_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


class ChaliceSlackRequestHandler:
def __init__(self, app: App, chalice: Chalice):
def __init__(self, app: App, chalice: Chalice): # type: ignore
self.app = app
self.chalice = chalice
self.logger = get_bolt_app_logger(app.name, ChaliceSlackRequestHandler)
Expand Down
8 changes: 4 additions & 4 deletions slack_bolt/adapter/aws_lambda/handler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import base64
import logging
from typing import List, Dict
from typing import List, Dict, Any

from slack_bolt.adapter.aws_lambda.internals import _first_value
from slack_bolt.app import App
Expand All @@ -11,7 +11,7 @@


class SlackRequestHandler:
def __init__(self, app: App):
def __init__(self, app: App): # type: ignore
self.app = app
self.logger = get_bolt_app_logger(app.name, SlackRequestHandler)

Expand Down Expand Up @@ -68,15 +68,15 @@ def to_bolt_request(event) -> BoltRequest:
)


def to_aws_response(resp: BoltResponse) -> Dict[str, any]:
def to_aws_response(resp: BoltResponse) -> Dict[str, Any]:
return {
"statusCode": resp.status,
"body": resp.body,
"headers": resp.first_headers(),
}


def not_found() -> Dict[str, any]:
def not_found() -> Dict[str, Any]:
return {
"statusCode": 404,
"body": "Not Found",
Expand Down
2 changes: 1 addition & 1 deletion slack_bolt/adapter/bottle/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def set_response(bolt_resp: BoltResponse, resp: Response) -> None:


class SlackRequestHandler:
def __init__(self, app: App):
def __init__(self, app: App): # type: ignore
self.app = app

def handle(self, req: Request, resp: Response) -> str:
Expand Down
2 changes: 1 addition & 1 deletion slack_bolt/adapter/django/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def to_django_response(bolt_resp: BoltResponse) -> HttpResponse:


class SlackRequestHandler:
def __init__(self, app: App):
def __init__(self, app: App): # type: ignore
self.app = app

def handle(self, req: HttpRequest) -> HttpResponse:
Expand Down
2 changes: 1 addition & 1 deletion slack_bolt/adapter/falcon/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class SlackAppResource:
api.add_route("/slack/events", SlackAppResource(app))
"""

def __init__(self, app: App):
def __init__(self, app: App): # type: ignore
self.app = app

def on_get(self, req: Request, resp: Response):
Expand Down
8 changes: 4 additions & 4 deletions slack_bolt/adapter/flask/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@


def to_bolt_request(req: Request) -> BoltRequest:
return BoltRequest(
return BoltRequest( # type: ignore
body=req.get_data(as_text=True),
query=req.query_string.decode("utf-8"),
headers=req.headers,
)
headers=req.headers, # type: ignore
) # type: ignore


def to_flask_response(bolt_resp: BoltResponse) -> Response:
Expand All @@ -23,7 +23,7 @@ def to_flask_response(bolt_resp: BoltResponse) -> Response:


class SlackRequestHandler:
def __init__(self, app: App):
def __init__(self, app: App): # type: ignore
self.app = app

def handle(self, req: Request) -> Response:
Expand Down
2 changes: 1 addition & 1 deletion slack_bolt/adapter/pyramid/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def to_pyramid_response(bolt_resp: BoltResponse) -> Response:


class SlackRequestHandler:
def __init__(self, app: App):
def __init__(self, app: App): # type: ignore
self.app = app

def handle(self, request: Request) -> Response:
Expand Down
2 changes: 1 addition & 1 deletion slack_bolt/adapter/sanic/async_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def to_sanic_response(bolt_resp: BoltResponse) -> HTTPResponse:


class AsyncSlackRequestHandler:
def __init__(self, app: AsyncApp):
def __init__(self, app: AsyncApp): # type: ignore
self.app = app

async def handle(self, req: Request) -> HTTPResponse:
Expand Down
2 changes: 1 addition & 1 deletion slack_bolt/adapter/starlette/async_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def to_starlette_response(bolt_resp: BoltResponse) -> Response:


class AsyncSlackRequestHandler:
def __init__(self, app: AsyncApp):
def __init__(self, app: AsyncApp): # type: ignore
self.app = app

async def handle(self, req: Request) -> Response:
Expand Down
2 changes: 1 addition & 1 deletion slack_bolt/adapter/starlette/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def to_starlette_response(bolt_resp: BoltResponse) -> Response:


class SlackRequestHandler:
def __init__(self, app: App):
def __init__(self, app: App): # type: ignore
self.app = app

async def handle(self, req: Request) -> Response:
Expand Down
8 changes: 4 additions & 4 deletions slack_bolt/adapter/tornado/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


class SlackEventsHandler(RequestHandler):
def initialize(self, app: App):
def initialize(self, app: App): # type: ignore
self.app = app

def post(self):
Expand All @@ -20,12 +20,12 @@ def post(self):


class SlackOAuthHandler(RequestHandler):
def initialize(self, app: App):
def initialize(self, app: App): # type: ignore
self.app = app

def get(self):
if self.app.oauth_flow is not None:
oauth_flow: OAuthFlow = self.app.oauth_flow
if self.app.oauth_flow is not None: # type: ignore
oauth_flow: OAuthFlow = self.app.oauth_flow # type: ignore
if self.request.path == oauth_flow.install_path:
bolt_resp = oauth_flow.handle_installation(
to_bolt_request(self.request)
Expand Down
2 changes: 1 addition & 1 deletion slack_bolt/app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Don't add async module imports here
from .app import App
from .app import App # type: ignore
4 changes: 2 additions & 2 deletions slack_bolt/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ def start(self, port: int = 3000, path: str = "/slack/events") -> None:
def dispatch(self, req: BoltRequest) -> BoltResponse:
self._init_context(req)

resp: BoltResponse = BoltResponse(status=200, body=None)
resp: BoltResponse = BoltResponse(status=200, body="")
middleware_state = {"next_called": False}

def middleware_next():
Expand Down Expand Up @@ -396,7 +396,7 @@ def keyword_matcher(payload) -> bool:
text: Optional[str] = payload.get("event", {}).get("text", {})
if text:
if isinstance(keyword, Pattern):
return keyword.match(text)
return keyword.match(text) # type: ignore
elif isinstance(keyword, str):
return keyword in text
return False
Expand Down
6 changes: 3 additions & 3 deletions slack_bolt/app/async_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ def start(self, port: int = 3000, path: str = "/slack/events") -> None:
async def async_dispatch(self, req: AsyncBoltRequest) -> BoltResponse:
self._init_context(req)

resp: BoltResponse = BoltResponse(status=200, body=None)
resp: BoltResponse = BoltResponse(status=200, body="")
middleware_state = {"next_called": False}

async def async_middleware_next():
Expand Down Expand Up @@ -426,7 +426,7 @@ async def keyword_matcher(payload) -> bool:
text: Optional[str] = payload.get("event", {}).get("text", {})
if text:
if isinstance(keyword, Pattern):
return keyword.match(text)
return keyword.match(text) # type: ignore
elif isinstance(keyword, str):
return keyword in text
return False
Expand Down Expand Up @@ -648,7 +648,7 @@ def _register_listener(
matchers: Optional[List[Callable[..., Awaitable[bool]]]],
middleware: Optional[List[Union[Callable, AsyncMiddleware]]],
auto_acknowledgement: bool = False,
) -> Callable[..., None]:
) -> None:

if not inspect.iscoroutinefunction(func):
name = func.__name__
Expand Down
5 changes: 3 additions & 2 deletions slack_bolt/context/ack/internals.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional, List, Union
from typing import Optional, List, Union, Any

from slack_sdk.models.attachments import Attachment
from slack_sdk.models.blocks import Block, Option, OptionGroup
Expand All @@ -9,7 +9,7 @@


def _set_response(
self: any,
self: Any,
text_or_whole_response: Union[str, dict] = "",
blocks: Optional[List[Union[dict, Block]]] = None,
attachments: Optional[List[Union[dict, Attachment]]] = None,
Expand Down Expand Up @@ -50,6 +50,7 @@ def _set_response(
body["option_groups"] = convert_to_dict_list(body["option_groups"])

self.response = BoltResponse(status=200, body=body)
return self.response
else:
raise BoltError(
f"{text_or_whole_response} (type: {type(text_or_whole_response)}) is unsupported"
Expand Down
1 change: 1 addition & 0 deletions slack_bolt/context/context.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# pytype: skip-file
from typing import Optional

from slack_bolt.context.ack import Ack
Expand Down
4 changes: 2 additions & 2 deletions slack_bolt/context/respond/internals.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional, List, Dict, Union
from typing import Optional, List, Dict, Union, Any

from slack_sdk.models.attachments import Attachment
from slack_sdk.models.blocks import Block
Expand All @@ -13,7 +13,7 @@ def _build_message(
response_type: Optional[str] = None,
replace_original: Optional[bool] = None,
delete_original: Optional[bool] = None,
) -> Dict[str, any]:
) -> Dict[str, Any]:
message = {"text": text}
if blocks is not None and len(blocks) > 0:
message["blocks"] = convert_to_dict_list(blocks)
Expand Down
6 changes: 3 additions & 3 deletions slack_bolt/context/say/internals.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from typing import Optional
from typing import Optional, Any


def _can_say(self: any, channel: Optional[str]) -> bool:
def _can_say(self: Any, channel: Optional[str]) -> bool:
return (
hasattr(self, "client")
and self.client is not None
and (channel or self.channel)
and (channel or self.channel) is not None
)
11 changes: 6 additions & 5 deletions slack_bolt/kwargs_injection/args.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# pytype: skip-file
import logging
from logging import Logger
from typing import Callable, Dict
from typing import Callable, Dict, Any

from slack_bolt.context import BoltContext
from slack_bolt.context.ack import Ack
Expand All @@ -19,7 +20,7 @@ class Args:
request: BoltRequest
response: BoltResponse
context: BoltContext
payload: Dict[str, any]
payload: Dict[str, Any]
ack: Ack
say: Say
respond: Respond
Expand All @@ -33,7 +34,7 @@ def __init__(
req: BoltRequest,
resp: BoltResponse,
context: BoltContext,
payload: Dict[str, any],
payload: Dict[str, Any],
ack: Ack,
say: Say,
respond: Respond,
Expand All @@ -45,8 +46,8 @@ def __init__(
self.request = self.req = req
self.response = self.resp = resp
self.context: BoltContext = context
self.payload: Dict[str, any] = payload
self.body: Dict[str, any] = payload
self.payload: Dict[str, Any] = payload
self.body: Dict[str, Any] = payload
self.ack: Ack = ack
self.say: Say = say
self.respond: Respond = respond
Expand Down
11 changes: 6 additions & 5 deletions slack_bolt/kwargs_injection/async_args.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# pytype: skip-file
from logging import Logger
from typing import Callable, Awaitable, Dict
from typing import Callable, Awaitable, Dict, Any

from slack_bolt.context.ack.async_ack import AsyncAck
from slack_bolt.context.async_context import AsyncBoltContext
Expand All @@ -18,7 +19,7 @@ class AsyncArgs:
request: AsyncBoltRequest
response: BoltResponse
context: AsyncBoltContext
payload: Dict[str, any]
payload: Dict[str, Any]
ack: AsyncAck
say: AsyncSay
respond: AsyncRespond
Expand All @@ -32,7 +33,7 @@ def __init__(
req: AsyncBoltRequest,
resp: BoltResponse,
context: AsyncBoltContext,
payload: Dict[str, any],
payload: Dict[str, Any],
ack: AsyncAck,
say: AsyncSay,
respond: AsyncRespond,
Expand All @@ -44,8 +45,8 @@ def __init__(
self.request = self.req = req
self.response = self.resp = resp
self.context: AsyncBoltContext = context
self.payload: Dict[str, any] = payload
self.body: Dict[str, any] = payload
self.payload: Dict[str, Any] = payload
self.body: Dict[str, Any] = payload
self.ack: AsyncAck = ack
self.say: AsyncSay = say
self.respond: AsyncRespond = respond
Expand Down
Loading

0 comments on commit 976d840

Please sign in to comment.