uvicorn icon indicating copy to clipboard operation
uvicorn copied to clipboard

Type Annotation and mypy support

Open Kludex opened this issue 3 years ago β€’ 16 comments

Checklist

  • [X] There are no similar issues or pull requests for this yet.
  • [x] I discussed this idea on the community chat and feedback is positive.

Summary

I've seen somewhere here that we want to have mypy. To be more organized, I've created the table below, so we can divide and conquer :sunglasses: :+1:

I'll be creating the PRs for those files in the following days.

All files

uvicorn
β”œβ”€β”€ config.py βœ…
β”œβ”€β”€ _handlers βœ…
β”œβ”€β”€ importer.py βœ…
β”œβ”€β”€ __init__.py βœ…
β”œβ”€β”€ lifespan βœ…
β”œβ”€β”€ logging.py βœ…
β”œβ”€β”€ loops βœ…
β”œβ”€β”€ __main__.py βœ…
β”œβ”€β”€ main.py βœ…
β”œβ”€β”€ middleware βœ…
β”œβ”€β”€ protocols βž–
β”‚Β Β  β”œβ”€β”€ http βœ… 
β”‚Β Β  β”œβ”€β”€ __init__.py βœ…
β”‚Β Β  β”œβ”€β”€ utils.py βœ…
β”‚Β Β  └── websockets βž–
β”‚Β Β      β”œβ”€β”€ auto.py βœ…
β”‚Β Β      β”œβ”€β”€ __init__.py βœ…
β”‚Β Β      β”œβ”€β”€ websockets_impl.py βœ… 
β”‚Β Β      └── wsproto_impl.py βž–
β”œβ”€β”€ server.py βœ…
β”œβ”€β”€ subprocess.py βœ…
β”œβ”€β”€ supervisors βœ…
└── workers.py βœ…

βœ…: Merged. βž–: PR available / WIP. ❌: Available to pick.

EDIT: I've simplified the report.

Kludex avatar Mar 10 '21 22:03 Kludex

@Kludex are the files without a PR associated "up for grabs"?

jkklapp avatar Apr 05 '21 15:04 jkklapp

Yes. But I think both h11 and httptools implementation files need others to be merged before (not sure).

But yeah, you can go for it. Ping me on that PR once you've finished so I can update the list above, please. Also, be aware that we're trying to match this: https://github.com/django/asgiref/blob/main/asgiref/typing.py, which is on the main branch, but it was not released yet.

EDIT: Looks like there will be a release on asgiref today. So we can start using it.

Kludex avatar Apr 05 '21 17:04 Kludex

Thanks for initiating this, @Kludex! πŸ‘

It would be great to have type annotations in this project, and I'm happy to help. I'll start contributing PRs and code reviews as time allows.

br3ndonland avatar Apr 18 '21 07:04 br3ndonland

This blog post by @tomchristie inspired me to be more diligent about using type annotations (and asyncio):

Python async frameworks - Beyond developer tribalism

Having to think about if a function does or doesn’t make I/O or could otherwise block is unarguably more complex than not having to think about it. It’s also more precise - you’re having to think about that for a reason, and done properly it’s presenting you with more information as a result. Being able to reason more clearly about which parts of your framework stack do or don’t perform I/O has a cost but also brings benefits.

There’s an analogy to make there in that writing Python using explicitly enforced typing is likely to be a harder than writing Python in an untyped style. It’s harder because you’re also being more precise. That’s not necessarily either good or bad, but it is definitively different.

I would include unit testing as a third entry in this category. After consistently working at unit testing, type annotations, and asyncio, a developer's understanding of their code will be in a completely different realm than before.

br3ndonland avatar Apr 18 '21 08:04 br3ndonland

ADDED tests:

  • [ ] tests
    • [x] conftest.py
    • [x] importer
      • [x] init.py
      • [x] raise_import_error.py
      • [x] test_importer.py
    • [x] init.py
    • [ ] middleware
      • [ ] test_debug.py
      • [ ] test_logging.py
      • [ ] test_message_logger.py
      • [ ] test_proxy_headers.py
      • [ ] test_wsgi.py
    • [ ] protocols
      • [ ] test_http.py
      • [ ] test_utils.py
      • [ ] test_websocket.py
    • [x] response.py
    • [ ] supervisors
      • [x] test_multiprocess.py
      • [ ] test_reload.py
    • [ ] test_auto_detection.py
    • [x] test_cli.py
    • [x] test_config.py
    • [x] test_default_headers.py
    • [x] test_lifespan.py
    • [x] test_main.py
    • [x] test_ssl.py
    • [x] utils.py

euri10 avatar Jun 26 '21 11:06 euri10

(would be great to have a py.typed file as well)

sebastian-philipp avatar Mar 08 '22 13:03 sebastian-philipp

Yes. py.typed, as per PEP 561, will be added when all files are covered.

Kludex avatar Mar 08 '22 13:03 Kludex

Only 3 missing... I'm excited! We are almost there!

Kludex avatar May 09 '22 19:05 Kludex

Only 2 missing 🀩

Kludex avatar May 13 '22 04:05 Kludex

One missing. One... πŸ™ 😭 πŸŽ‰ πŸƒ

Kludex avatar Jul 08 '22 05:07 Kludex

Are we there yet?

ananthb avatar Aug 21 '22 05:08 ananthb

You'll see a GIF with fireworks when we finish here... πŸ˜…

There's still one file missing. The thing is that while adding type annotations we usually find small bugs, and those need to fixed separately before the file gets fully type annotated.

We are getting there.

Kludex avatar Aug 21 '22 06:08 Kludex

This is awesome

liorsbg avatar Aug 22 '22 06:08 liorsbg

Maybe a dumb question, but will this allow mypyc compilation?

davidbrochart avatar Sep 03 '22 15:09 davidbrochart

It's on my plans to study the possibility, but I know nothing about mypyc.

@abersheeran knows stuff. :eyes:

Kludex avatar Sep 03 '22 15:09 Kludex

mypyc requires stricter type annotations and has little support for dynamic features. uvicorn may not be ready yet.

By the way, In baize, mypyc improves performance by about 30%. It may not improve uvicorn as expected.

abersheeran avatar Sep 03 '22 23:09 abersheeran

  • py.typed added on https://github.com/encode/uvicorn/pull/1687.

It will be available from 0.19.0.

Kludex avatar Oct 07 '22 12:10 Kludex

We finish the package code. Now let's finish the test files. :sweat_smile:

Kludex avatar Nov 21 '22 08:11 Kludex