-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Redis asyncio invalid type definition. #8242
Comments
Answered here: #7597 (comment) |
I think this issue must be reopened, because FastAPI depends on type system and the For all folks, who use redis with FastAPI consider downgrading Or you can downgrade |
The runtime TypeError can be worked around by quoting the annotation, either explicitly ( Is there any other remaining problem? |
Yes. The problem is that FastAPI framework uses types to inject dependencies. Quoting "Redis[Any]" doesn't work. Here is a minimum reproducible example: from __future__ import annotations
from typing import Any, AsyncGenerator
from fastapi import Depends, FastAPI, Request
from redis.asyncio import ConnectionPool, Redis
pool = ConnectionPool.from_url("redis://localhost:6379/0")
async def get_redis_connection(request: Request) -> AsyncGenerator[Redis[Any], None]:
redis_client: Redis[Any] = Redis(connection_pool=request.app.state.redis_pool)
try: # noqa: WPS501
yield redis_client
finally:
await redis_client.close()
app = FastAPI()
@app.get("/")
async def handler(redis: Redis[Any] = Depends(get_redis_connection)) -> None:
await redis.set("key", "val") Dependencies:
Mypy config from pyproject.toml: [tool.mypy]
strict = true
ignore_missing_imports = true
allow_subclassing_any = true
allow_untyped_calls = true
pretty = true
show_error_codes = true
implicit_reexport = true
allow_untyped_decorators = true
warn_unused_ignores = false
warn_return_any = false
namespace_packages = true If you run this file you'll get an error. |
In this particular case, fastapi doesn't seem to use the type stubs for runtime type checking, instead using the runtime classes. I'm not sure that is even easily possible. That said, this is a larger issue that needs to be discussed on typing-sig, but unfortunately it's out of scope to fix in typeshed. |
But maybe, if I provide a request with Redis type without Generic in definition, it will work as expected. Or it would be incorrect? |
That should help and is the correct long-term solution anyway, as redis is moving towards including types themselves. |
Ok. I'll keep this issue open to be able to link pull request to it. Can you assign this issue to me? |
There's another problem here - inheriting from Clearly we can't do Frankly, I think that having the |
You can work around this with something like: if TYPE_CHECKING:
_RedisBase = Redis[bytes]
else:
_RedisBase = Redis
class ArgRedis(_RedisBase): ... Admittedly it's not pretty. |
Relevant section of mypy docs: |
Thanks both, that's what I have python-arq/arq#331. |
There is another phase of this issue, the mypy worked when types-redis was installed but when the code is run, this
is raised and the line of code it points to is the type checking of the return value of a function as seen below: |
I found a problem using
types-redis
library with new redis.asyncio module.The problem is that mypy throws an error.
The code:
The error:
I tried to fix it by providing an Any generic type.
But found another issue. Mypy detects no error for the following code:
But I cant start my program because of this:
I guess it's a problem.
The text was updated successfully, but these errors were encountered: