Giter Club home page Giter Club logo

Comments (9)

Dreamsorcerer avatar Dreamsorcerer commented on June 5, 2024 1

I've opened a discussion to see if there's anything we can do in the future:
python/typing#1543

from aiohttp.

Dreamsorcerer avatar Dreamsorcerer commented on June 5, 2024

The key is the instance, not the name/type.

So, you need to save the key somewhere globally and reuse it across the project.

from aiohttp.

Dreamsorcerer avatar Dreamsorcerer commented on June 5, 2024

We can probably make this clearer in the docs, but you'll see all the examples use a variable, rather than creating a new instance when indexing.

from aiohttp.

jmspereira avatar jmspereira commented on June 5, 2024

Thank you for the answer, but what is supposed to do when the handlers are defined in a different module than the server initialization?
Should the "standard" define all the app keys in a module and import them both in the module that initiates the server and in the handlers modules?

from aiohttp.

Dreamsorcerer avatar Dreamsorcerer commented on June 5, 2024

Yes, there needs to be a canonical location. For projects, I typically put them in typedefs.py or similar (along with typing aliases etc.). Between different projects, it needs to be imported, for example see aiohttp-jinja2:

The static function has similar usage, except it requires you to set app[aiohttp_jinja2.static_root_key].

app = web.Application()
app[aiohttp_jinja2.static_root_key] = "/static"

https://aiohttp-jinja2.readthedocs.io/en/latest/#default-globals

And we use that key, for example, in aiohttp-devtools:
https://github.com/aio-libs/aiohttp-devtools/blob/master/aiohttp_devtools/runserver/serve.py#L28

from aiohttp.

jmspereira avatar jmspereira commented on June 5, 2024

Ok, I will do it, thank you so much for the fast answer!

from aiohttp.

jmspereira avatar jmspereira commented on June 5, 2024

Hey @Dreamsorcerer, I was making this migration but there are some cases where the value that I want to store an AppKey is a callable (for instance a partial that is completed with the remaining args in the handler). However when I do:

some_key = AppKey("some_key", Callable[[type_arg1, type_arg2], return_type])

Mypy is not very happy saying:
error: Argument 2 to "AppKey" has incompatible type "object"; expected "type[Never] | None"

from aiohttp.

Dreamsorcerer avatar Dreamsorcerer commented on June 5, 2024

See the examples in the docs for the alternative syntax. i.e. It'll work with AppKey[Callable]("some_key"). The problem is that things like Callable are not types, but we don't want to annotate it as allowing anything and have mistakes like AppKey(..., 5). I'm not sure if there is a better annotation to make this work.

from aiohttp.

jmspereira avatar jmspereira commented on June 5, 2024

Thanks!

from aiohttp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.