Skip to content

Commit

Permalink
Enforce typing with mypy (#341)
Browse files Browse the repository at this point in the history
* Enforce typing with mypy
  • Loading branch information
ovv authored and mattrasband committed Jun 23, 2019
1 parent fe3e699 commit acd6fe8
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 20 deletions.
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ jobs:
key: 'tox-{{ checksum "requirements/base.txt" }}-{{ checksum "requirements/testing.txt" }}-{{ checksum "requirements/development.txt" }}-{{ checksum "requirements/production.txt" }}'
paths:
- .tox
- .mypy_cache
- store_test_results:
path: .tox/py37/artifacts/
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.env
.tox/
.venv
.mypy_cache
artifacts/

# Editors
Expand Down
4 changes: 2 additions & 2 deletions pyslackersweb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
)


async def index(request: web.Request) -> None:
async def index(request: web.Request) -> web.Response:
location = request.app["website_app"].router["index"].url_for()
raise web.HTTPFound(location=location)
return web.HTTPFound(location=location)


async def app_factory() -> web.Application:
Expand Down
2 changes: 1 addition & 1 deletion pyslackersweb/website/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def app_factory() -> web.Application:
jinja2_setup(
website,
context_processors=[request_processor],
loader=FileSystemLoader([package_root / "templates"]),
loader=FileSystemLoader(str(package_root / "templates")),
filters={"formatted_number": formatted_number, **FILTERS},
)

Expand Down
9 changes: 5 additions & 4 deletions pyslackersweb/website/contexts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import datetime, timedelta
from typing import AsyncGenerator

from aiohttp import ClientSession, web
from apscheduler.schedulers.asyncio import AsyncIOScheduler
Expand All @@ -7,7 +8,7 @@
from . import tasks


async def background_jobs(app: web.Application) -> None:
async def background_jobs(app: web.Application) -> AsyncGenerator[None, None]:
scheduler = app["scheduler"]

scheduler.add_job(
Expand Down Expand Up @@ -37,18 +38,18 @@ async def background_jobs(app: web.Application) -> None:
yield


async def client_session(app: web.Application) -> None:
async def client_session(app: web.Application) -> AsyncGenerator[None, None]:
async with ClientSession(raise_for_status=True) as session:
app["client_session"] = session
yield


async def slack_client(app: web.Application) -> None:
async def slack_client(app: web.Application) -> AsyncGenerator[None, None]:
app["slack_client"] = SlackAPI(token=app["slack_token"], session=app["client_session"])
yield


async def apscheduler(app: web.Application):
async def apscheduler(app: web.Application) -> AsyncGenerator[None, None]:
app["scheduler"] = scheduler = AsyncIOScheduler()
scheduler.start()

Expand Down
9 changes: 7 additions & 2 deletions pyslackersweb/website/middleware.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import typing

from uuid import uuid4

from aiohttp import web
Expand All @@ -6,11 +8,14 @@


@web.middleware
async def request_context_middleware(request, handler):
async def request_context_middleware(
request: web.Request,
handler: typing.Callable[[web.Request], typing.Awaitable[web.StreamResponse]],
):
"""
Middleware to set a context variable for the request/response cycle.
"""
token = REQUEST_CONTEXT.set(str(uuid4()))
token = REQUEST_CONTEXT.set(uuid4())
try:
return await handler(request)
finally:
Expand Down
14 changes: 7 additions & 7 deletions pyslackersweb/website/tasks.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import dataclasses
import logging
from collections import Counter
from typing import List
from typing import List, Awaitable, Callable

import slack

Expand Down Expand Up @@ -31,7 +31,7 @@ class Channel:
members: int


def sync_github_repositories(app: web.Application):
def sync_github_repositories(app: web.Application) -> Callable[[], Awaitable[None]]:
session = app["client_session"]

async def _sync_github() -> None:
Expand Down Expand Up @@ -70,10 +70,10 @@ async def _sync_github() -> None:
return _sync_github


def sync_slack_users(app: web.Application):
def sync_slack_users(app: web.Application) -> Callable[[], Awaitable[None]]:
client = app["slack_client"]

async def _sync_slack_users():
async def _sync_slack_users() -> None:
logger.debug("Refreshing slack users cache.")
oauth_token = app["slack_token"]

Expand All @@ -82,7 +82,7 @@ async def _sync_slack_users():
return

try:
counter = Counter()
counter: Counter = Counter()
async for user in client.iter(slack.methods.USERS_LIST, minimum_time=3):
if user["deleted"] or user["is_bot"] or not user["tz"]:
continue
Expand All @@ -106,10 +106,10 @@ async def _sync_slack_users():
return _sync_slack_users


def sync_slack_channels(app: web.Application):
def sync_slack_channels(app: web.Application) -> Callable[[], Awaitable[None]]:
client = app["slack_client"]

async def _sync_slack_channel():
async def _sync_slack_channel() -> None:
logger.debug("Refreshing slack channels cache.")
oauth_token = app["slack_token"]

Expand Down
3 changes: 2 additions & 1 deletion pyslackersweb/website/util/context.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import contextvars

from uuid import uuid4

REQUEST_CONTEXT = contextvars.ContextVar("REQUEST_CONTEXT", default=None)
REQUEST_CONTEXT = contextvars.ContextVar("REQUEST_CONTEXT", default=uuid4())
1 change: 1 addition & 0 deletions requirements/testing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pytest
pytest-cov
pytest-aiohttp
tox==3.12.1
mypy==0.710
7 changes: 4 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ commands = python -m pytest --verbose --cov=pyslackersweb/ --cov-report=term-mis

[testenv:lint]
deps =
-r requirements/production.txt
pylint
commands = pylint pyslackersweb
-r requirements/testing.txt
commands =
pylint pyslackersweb
mypy . --ignore-missing-imports

[testenv:fmt]
deps = black
Expand Down

0 comments on commit acd6fe8

Please sign in to comment.