Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport #5457 #5464

Merged
merged 2 commits into from
Mar 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions .mypy.ini
Original file line number Diff line number Diff line change
@@ -1,24 +1,49 @@
[mypy]
warn_unused_configs = True
strict = True
files = aiohttp, examples
check_untyped_defs = True
follow_imports_for_stubs = True
#disallow_any_decorated = True
disallow_any_generics = True
disallow_incomplete_defs = True
disallow_subclassing_any = True
disallow_untyped_calls = True
disallow_untyped_decorators = True
disallow_untyped_defs = True
implicit_reexport = False
no_implicit_optional = True
show_error_codes = True
strict_equality = True
warn_incomplete_stub = True
warn_redundant_casts = True
#warn_unreachable = True
warn_unused_ignores = True
disallow_any_unimported = True
warn_return_any = True

[mypy-examples.*]
disallow_untyped_calls = False
disallow_untyped_defs = False

[mypy-aiodns]
ignore_missing_imports = True

[mypy-brotli]
[mypy-aioredis]
ignore_missing_imports = True

[mypy-gunicorn.*]
[mypy-asynctest]
ignore_missing_imports = True

[mypy-uvloop]
[mypy-brotli]
ignore_missing_imports = True

[mypy-cchardet]
ignore_missing_imports = True

[mypy-gunicorn.*]
ignore_missing_imports = True

[mypy-tokio]
ignore_missing_imports = True

[mypy-asynctest]
[mypy-uvloop]
ignore_missing_imports = True
1 change: 1 addition & 0 deletions CHANGES/5457.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve Mypy coverage.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fmt format:

.PHONY: mypy
mypy:
mypy --show-error-codes aiohttp
mypy

.develop: .install-deps $(call to-hash,$(PYS) $(CYS) $(CS))
pip install -e .
Expand Down
2 changes: 1 addition & 1 deletion aiohttp/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ async def _resolve_host(
for trace in traces:
await trace.send_dns_resolvehost_end(host)

return res # type: ignore[no-any-return]
return res

key = (host, port)

Expand Down
5 changes: 3 additions & 2 deletions aiohttp/resolver.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import asyncio
import socket
from typing import Any, Dict, List, Optional
from typing import Any, Dict, List, Optional, Type, Union

from .abc import AbstractResolver
from .helpers import get_running_loop
Expand Down Expand Up @@ -146,4 +146,5 @@ async def close(self) -> None:
self._resolver.cancel()


DefaultResolver = AsyncResolver if aiodns_default else ThreadedResolver
_DefaultType = Type[Union[AsyncResolver, ThreadedResolver]]
DefaultResolver: _DefaultType = AsyncResolver if aiodns_default else ThreadedResolver
2 changes: 1 addition & 1 deletion aiohttp/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
__all__ = ("GunicornWebWorker", "GunicornUVLoopWebWorker", "GunicornTokioWebWorker")


class GunicornWebWorker(base.Worker): # type: ignore[misc]
class GunicornWebWorker(base.Worker): # type: ignore[misc,no-any-unimported]

DEFAULT_AIOHTTP_LOG_FORMAT = AccessLogger.LOG_FORMAT
DEFAULT_GUNICORN_LOG_FORMAT = GunicornAccessLogFormat.default
Expand Down
Empty file added examples/__init__.py
Empty file.
4 changes: 2 additions & 2 deletions examples/background_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ async def listen_to_redis(app):
print("Redis connection closed.")


async def start_background_tasks(app):
app["redis_listener"] = app.loop.create_task(listen_to_redis(app))
async def start_background_tasks(app: web.Application) -> None:
app["redis_listener"] = asyncio.create_task(listen_to_redis(app))


async def cleanup_background_tasks(app):
Expand Down
2 changes: 1 addition & 1 deletion examples/client_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import aiohttp


async def fetch(session):
async def fetch(session: aiohttp.ClientSession) -> None:
print("Query http://httpbin.org/get")
async with session.get("http://httpbin.org/get") as resp:
print(resp.status)
Expand Down
5 changes: 3 additions & 2 deletions examples/client_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ async def dispatch():
break

# send request
async with aiohttp.ws_connect(url, autoclose=False, autoping=False) as ws:
await dispatch()
async with aiohttp.ClientSession() as session:
async with session.ws_connect(url, autoclose=False, autoping=False) as ws:
await dispatch()


ARGS = argparse.ArgumentParser(
Expand Down
8 changes: 6 additions & 2 deletions examples/fake_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@

import aiohttp
from aiohttp import web
from aiohttp.abc import AbstractResolver
from aiohttp.resolver import DefaultResolver
from aiohttp.test_utils import unused_port


class FakeResolver:
class FakeResolver(AbstractResolver):
_LOCAL_HOST = {0: "127.0.0.1", socket.AF_INET: "127.0.0.1", socket.AF_INET6: "::1"}

def __init__(self, fakes, *, loop):
Expand All @@ -34,6 +35,9 @@ async def resolve(self, host, port=0, family=socket.AF_INET):
else:
return await self._resolver.resolve(host, port, family)

async def close(self) -> None:
self._resolver.close()


class FakeFacebook:
def __init__(self, *, loop):
Expand All @@ -45,7 +49,7 @@ def __init__(self, *, loop):
web.get("/v2.7/me/friends", self.on_my_friends),
]
)
self.runner = None
self.runner = web.AppRunner(self.app)
here = pathlib.Path(__file__)
ssl_cert = here.parent / "server.crt"
ssl_key = here.parent / "server.key"
Expand Down
4 changes: 2 additions & 2 deletions examples/web_classview.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ async def get(self):
return web.json_response(
{
"method": "get",
"args": dict(self.request.GET),
"args": dict(self.request.query),
"headers": dict(self.request.headers),
},
dumps=functools.partial(json.dumps, indent=4),
Expand All @@ -25,7 +25,7 @@ async def post(self):
return web.json_response(
{
"method": "post",
"args": dict(self.request.GET),
"args": dict(self.request.query),
"data": dict(data),
"headers": dict(self.request.headers),
},
Expand Down
21 changes: 11 additions & 10 deletions examples/web_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

from pprint import pformat
from typing import NoReturn

from aiohttp import web

Expand All @@ -22,20 +23,20 @@ async def root(request):
return resp


async def login(request):
resp = web.HTTPFound(location="/")
resp.set_cookie("AUTH", "secret")
return resp
async def login(request: web.Request) -> NoReturn:
exc = web.HTTPFound(location="/")
exc.set_cookie("AUTH", "secret")
raise exc


async def logout(request):
resp = web.HTTPFound(location="/")
resp.del_cookie("AUTH")
return resp
async def logout(request: web.Request) -> NoReturn:
exc = web.HTTPFound(location="/")
exc.del_cookie("AUTH")
raise exc


def init(loop):
app = web.Application(loop=loop)
def init():
app = web.Application()
app.router.add_get("/", root)
app.router.add_get("/login", login)
app.router.add_get("/logout", logout)
Expand Down