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

Easy way to get common types for annotations #4686

Closed
2 of 3 tasks
Dreamsorcerer opened this issue Apr 12, 2020 · 4 comments
Closed
2 of 3 tasks

Easy way to get common types for annotations #4686

Dreamsorcerer opened this issue Apr 12, 2020 · 4 comments

Comments

@Dreamsorcerer
Copy link
Member

Dreamsorcerer commented Apr 12, 2020

🐣 Is your feature request related to a problem? Please describe.
I'm unclear what the best way currently is to make use of type annotations in my own aiohttp projects.

💡 Describe the solution you'd like
One of the aiohttp-demos to be updated with full type hinting to serve as an example.

Describe alternatives you've considered
The only way I can see at the moment, is with importing protected objects from various modules.

e.g. from aiohttp.web_app import _Handler
though this is also in aiohttp.web_urldispatcher._WebHandler

Types to add:

@hyzyla
Copy link
Contributor

hyzyla commented Apr 13, 2020

You can define all types in your code using public classes:

from aiohttp import web

Handler = Callable[[web.Request], Awaitable[web.StreamResponse]]

But, I think, that defining a common combination of types will improve the user experience of working with a library

@Dreamsorcerer
Copy link
Member Author

That is also possible, but seems even less maintainable than importing protected variables.

Hardcoded types in my code is more difficult to ensure they are correct and that they get updated if the signatures change in future.

@playpauseandstop
Copy link
Contributor

playpauseandstop commented Jun 22, 2020

Can add +1 here.

In my aiohttp libraries, such as aiohttp-middlewares I’m importing _Handler and _Middleware from aiohttp.web_middlewares and redefine it to Handler & Middleware for later reuse across the library.

While, in reality I would like to have ability to import aiohttp type annotations such as,

from aiohttp.web_typing import Handler, Middleware  # and so on

Or from aiohttp.annotations module, or from aiohttp.typing module, doesn’t matter. Main point to have single entry point with all reusable type annotations to use in other aiohttp libraries and projects.

ps. As implementation reference there is a pyrsistent.typing module, which provides type annotations for pyrsistent library in very clear way for 3rd party users
pps. I found out, that there is aiohttp.typedefs module already, but from quick overview it missed many usable type annotations, such as Handler, Middleware

@Dreamsorcerer
Copy link
Member Author

We've added the Handler type now.
Sounds like we should also add a Middleware type. Is there anything else that people think are missing?

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

No branches or pull requests

4 participants