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

Type Annotation and mypy support #998

Closed
2 tasks done
Kludex opened this issue Mar 10, 2021 · 18 comments · Fixed by #1754 or #1793
Closed
2 tasks done

Type Annotation and mypy support #998

Kludex opened this issue Mar 10, 2021 · 18 comments · Fixed by #1754 or #1793

Comments

@Kludex
Copy link
Member

Kludex commented Mar 10, 2021

Checklist

  • There are no similar issues or pull requests for this yet.
  • 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 😎 👍

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.

@jkklapp
Copy link
Contributor

jkklapp commented Apr 5, 2021

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

@Kludex
Copy link
Member Author

Kludex commented Apr 5, 2021

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.

@br3ndonland
Copy link
Contributor

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
Copy link
Contributor

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.

@euri10
Copy link
Member

euri10 commented Jun 26, 2021

ADDED tests:

  • tests
    • conftest.py
    • importer
      • init.py
      • raise_import_error.py
      • test_importer.py
    • 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
    • response.py
    • supervisors
      • test_multiprocess.py
      • test_reload.py
    • test_auto_detection.py
    • test_cli.py
    • test_config.py
    • test_default_headers.py
    • test_lifespan.py
    • test_main.py
    • test_ssl.py
    • utils.py

jhesketh added a commit to jhesketh/aquarium that referenced this issue Jan 18, 2022
Uvicorn is very close to implementing typing at which point we can
upgrade and remove the ignore.
encode/uvicorn#998

Signed-off-by: Joshua Hesketh <[email protected]>
@sebastian-philipp
Copy link

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

@Kludex
Copy link
Member Author

Kludex commented Mar 8, 2022

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

@Kludex
Copy link
Member Author

Kludex commented May 9, 2022

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

@Kludex
Copy link
Member Author

Kludex commented May 13, 2022

Only 2 missing 🤩

@Kludex
Copy link
Member Author

Kludex commented Jul 8, 2022

One missing. One... 🙏 😭 🎉 🏃

@ananthb
Copy link

ananthb commented Aug 21, 2022

Are we there yet?

@Kludex
Copy link
Member Author

Kludex commented Aug 21, 2022

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.

@liorsbg
Copy link

liorsbg commented Aug 22, 2022

This is awesome

@davidbrochart
Copy link

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

@Kludex
Copy link
Member Author

Kludex commented Sep 3, 2022

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

@abersheeran knows stuff. 👀

@abersheeran
Copy link
Member

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.

@Kludex
Copy link
Member Author

Kludex commented Oct 7, 2022

It will be available from 0.19.0.

@Kludex
Copy link
Member Author

Kludex commented Nov 21, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
9 participants