From 8e8efe4fd44074c9558343915604b73974188f9c Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Mon, 8 Mar 2021 22:55:05 +0100 Subject: [PATCH 1/3] fix: mypy issues --- setup.cfg | 11 ++++++++++- uvicorn/loops/asyncio.py | 3 ++- uvicorn/loops/auto.py | 2 +- uvicorn/loops/uvloop.py | 2 +- uvicorn/main.py | 17 +++++++---------- uvicorn/protocols/http/auto.py | 4 ++++ uvicorn/protocols/websockets/auto.py | 4 ++++ uvicorn/supervisors/__init__.py | 14 ++++++++++---- uvicorn/workers.py | 11 ++++++----- 9 files changed, 45 insertions(+), 23 deletions(-) diff --git a/setup.cfg b/setup.cfg index 04ee1d0b4..1c321fb1c 100644 --- a/setup.cfg +++ b/setup.cfg @@ -6,7 +6,16 @@ max-line-length = 88 disallow_untyped_defs = True ignore_missing_imports = True follow_imports = silent -files = uvicorn/lifespan,tests/test_lifespan.py +files = uvicorn +# files = +# uvicorn/lifespan, +# tests/test_lifespan.py, +# uvicorn/loops, +# uvicorn/main.py, +# uvicorn/workers.py, +# uvicorn/protocols/http/auto.py, +# uvicorn/protocols/websockets/auto.py, +# uvicorn/supervisors/__init__.py [mypy-tests.*] disallow_untyped_defs = False diff --git a/uvicorn/loops/asyncio.py b/uvicorn/loops/asyncio.py index f2ac3f9ae..a556511c6 100644 --- a/uvicorn/loops/asyncio.py +++ b/uvicorn/loops/asyncio.py @@ -4,7 +4,8 @@ import sys -def asyncio_setup(): +def asyncio_setup() -> None: + loop: asyncio.AbstractEventLoop if ( sys.version_info.major >= 3 and sys.version_info.minor >= 8 diff --git a/uvicorn/loops/auto.py b/uvicorn/loops/auto.py index b92007495..b41b015ed 100644 --- a/uvicorn/loops/auto.py +++ b/uvicorn/loops/auto.py @@ -1,4 +1,4 @@ -def auto_loop_setup(): +def auto_loop_setup() -> None: try: import uvloop # noqa except ImportError: # pragma: no cover diff --git a/uvicorn/loops/uvloop.py b/uvicorn/loops/uvloop.py index 8cd55c77d..cb6f32638 100644 --- a/uvicorn/loops/uvloop.py +++ b/uvicorn/loops/uvloop.py @@ -3,5 +3,5 @@ import uvloop -def uvloop_setup(): +def uvloop_setup() -> None: asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) diff --git a/uvicorn/main.py b/uvicorn/main.py index d7e866ec4..25d3b545b 100644 --- a/uvicorn/main.py +++ b/uvicorn/main.py @@ -31,7 +31,7 @@ logger = logging.getLogger("uvicorn.error") -def print_version(ctx, param, value): +def print_version(ctx: click.Context, param: click.Parameter, value: bool) -> None: if not value or ctx.resilient_parsing: return click.echo( @@ -281,7 +281,7 @@ def print_version(ctx, param, value): show_default=True, ) def main( - app, + app: str, host: str, port: int, uds: str, @@ -318,11 +318,10 @@ def main( use_colors: bool, app_dir: str, factory: bool, -): +) -> None: sys.path.insert(0, app_dir) kwargs = { - "app": app, "host": host, "port": port, "uds": uds, @@ -359,10 +358,10 @@ def main( "use_colors": use_colors, "factory": factory, } - run(**kwargs) + run(app, **kwargs) -def run(app, **kwargs): +def run(app: str, **kwargs: typing.Any) -> None: config = Config(app, **kwargs) server = Server(config=config) @@ -376,12 +375,10 @@ def run(app, **kwargs): if config.should_reload: sock = config.bind_socket() - supervisor = ChangeReload(config, target=server.run, sockets=[sock]) - supervisor.run() + ChangeReload(config, target=server.run, sockets=[sock]).run() elif config.workers > 1: sock = config.bind_socket() - supervisor = Multiprocess(config, target=server.run, sockets=[sock]) - supervisor.run() + Multiprocess(config, target=server.run, sockets=[sock]).run() else: server.run() diff --git a/uvicorn/protocols/http/auto.py b/uvicorn/protocols/http/auto.py index a76f5cbb5..471c46679 100644 --- a/uvicorn/protocols/http/auto.py +++ b/uvicorn/protocols/http/auto.py @@ -1,3 +1,7 @@ +import asyncio +from typing import Type + +AutoHTTPProtocol: Type[asyncio.Protocol] try: import httptools # noqa except ImportError: # pragma: no cover diff --git a/uvicorn/protocols/websockets/auto.py b/uvicorn/protocols/websockets/auto.py index e3364dc73..0dfba3bdb 100644 --- a/uvicorn/protocols/websockets/auto.py +++ b/uvicorn/protocols/websockets/auto.py @@ -1,3 +1,7 @@ +import asyncio +import typing + +AutoWebSocketsProtocol: typing.Optional[typing.Type[asyncio.Protocol]] try: import websockets # noqa except ImportError: # pragma: no cover diff --git a/uvicorn/supervisors/__init__.py b/uvicorn/supervisors/__init__.py index e2c87b819..10d834eb2 100644 --- a/uvicorn/supervisors/__init__.py +++ b/uvicorn/supervisors/__init__.py @@ -1,8 +1,14 @@ +import typing + +from uvicorn.supervisors.basereload import BaseReload from uvicorn.supervisors.multiprocess import Multiprocess -try: - from uvicorn.supervisors.watchgodreload import WatchGodReload as ChangeReload -except ImportError: - from uvicorn.supervisors.statreload import StatReload as ChangeReload +if typing.TYPE_CHECKING: + ChangeReload: typing.Type[BaseReload] +else: + try: + from uvicorn.supervisors.watchgodreload import WatchGodReload as ChangeReload + except ImportError: + from uvicorn.supervisors.statreload import StatReload as ChangeReload __all__ = ["Multiprocess", "ChangeReload"] diff --git a/uvicorn/workers.py b/uvicorn/workers.py index 7c160dff6..2f0a43111 100644 --- a/uvicorn/workers.py +++ b/uvicorn/workers.py @@ -1,6 +1,7 @@ import asyncio import logging import signal +from typing import Any from gunicorn.workers.base import Worker @@ -16,7 +17,7 @@ class UvicornWorker(Worker): CONFIG_KWARGS = {"loop": "uvloop", "http": "httptools"} - def __init__(self, *args, **kwargs): + def __init__(self, *args: Any, **kwargs: Any) -> None: super(UvicornWorker, self).__init__(*args, **kwargs) logger = logging.getLogger("uvicorn.error") @@ -58,24 +59,24 @@ def __init__(self, *args, **kwargs): self.config = Config(**config_kwargs) - def init_process(self): + def init_process(self) -> None: self.config.setup_event_loop() super(UvicornWorker, self).init_process() - def init_signals(self): + def init_signals(self) -> None: # Reset signals so Gunicorn doesn't swallow subprocess return codes # other signals are set up by Server.install_signal_handlers() # See: https://github.com/encode/uvicorn/issues/894 for s in self.SIGNALS: signal.signal(s, signal.SIG_DFL) - def run(self): + def run(self) -> None: self.config.app = self.wsgi server = Server(config=self.config) loop = asyncio.get_event_loop() loop.run_until_complete(server.serve(sockets=self.sockets)) - async def callback_notify(self): + async def callback_notify(self) -> None: self.notify() From 9ebeb0d2d59a1ee3ce15130fcf72d62541c3d281 Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Mon, 8 Mar 2021 22:56:23 +0100 Subject: [PATCH 2/3] fix setup.cfg --- setup.cfg | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/setup.cfg b/setup.cfg index 1c321fb1c..b9a8e9a34 100644 --- a/setup.cfg +++ b/setup.cfg @@ -6,16 +6,15 @@ max-line-length = 88 disallow_untyped_defs = True ignore_missing_imports = True follow_imports = silent -files = uvicorn -# files = -# uvicorn/lifespan, -# tests/test_lifespan.py, -# uvicorn/loops, -# uvicorn/main.py, -# uvicorn/workers.py, -# uvicorn/protocols/http/auto.py, -# uvicorn/protocols/websockets/auto.py, -# uvicorn/supervisors/__init__.py +files = + uvicorn/lifespan, + tests/test_lifespan.py, + uvicorn/loops, + uvicorn/main.py, + uvicorn/workers.py, + uvicorn/protocols/http/auto.py, + uvicorn/protocols/websockets/auto.py, + uvicorn/supervisors/__init__.py [mypy-tests.*] disallow_untyped_defs = False From 48cf4a15a2abb5243ed5f6aeec34372db9817cc9 Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Sat, 29 May 2021 07:40:19 +0200 Subject: [PATCH 3/3] fix run() type hint on app parameter --- uvicorn/main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/uvicorn/main.py b/uvicorn/main.py index 862821126..424b388d8 100644 --- a/uvicorn/main.py +++ b/uvicorn/main.py @@ -5,6 +5,7 @@ import typing import click +from asgiref.typing import ASGIApplication import uvicorn from uvicorn.config import ( @@ -370,7 +371,7 @@ def main( run(app, **kwargs) -def run(app: str, **kwargs: typing.Any) -> None: +def run(app: typing.Union[ASGIApplication, str], **kwargs: typing.Any) -> None: config = Config(app, **kwargs) server = Server(config=config)