admin管理员组文章数量:1418338
import redis
a = redis.StrictRedis(host="a", port=123, db=0, decode_response=true)
Mypy is giving error
Missing type parameters for generic type "StrictRedis" [type-arg]
Python version: 3.9.20
Mypy version: 1.14.1
import redis
a = redis.StrictRedis(host="a", port=123, db=0, decode_response=true)
Mypy is giving error
Missing type parameters for generic type "StrictRedis" [type-arg]
Python version: 3.9.20
Mypy version: 1.14.1
- 1 Related: What's the difference between StrictRedis() and Redis()? – Daraan Commented Jan 29 at 17:27
- What's your redis-py version? You may not need the stubs, and redis-py itself does not declare Redis generic. Note: The redis package includes type annotations or type stubs since version 5.0.0. – STerliakov Commented Jan 31 at 15:28
2 Answers
Reset to default 2This is a type warning you can theoretically ignore. The types-redis
extension that is part of typeshed
that your type-checker is using, defines Redis
as a Generic
bound over str | bytes
.
This is done to better know the outputs or inputs of some method, e.g. of pipeline -> str | bytes
or register_script(script: str | bytes) -> Script
.
You should init it as:
a = redis.StrictRedis[your-type-here](host="a", port=123, db=0, decode_response=true)
If you do not know which to use, use redis.StrictRedis[str | bytes](...
or just add a # type: ignore[type-arg]
EDIT: For compatibility with older python versions use:
from typing import TYPE_CHECKING, Union
...
if TYPE_CHECKING:
a = redis.StrictRedis[Union[str, bytes]](host="a", port=123, db=0, decode_response=true)
else:
a = redis.StrictRedis(host="a", port=123, db=0, decode_response=true)
(Not sure what your question is.)
If the Redis client lib doesn't provide type hints, you can use # type: ignore
to tell mypy.
a = redis.StrictRedis(host="a", port=123, db=0, decode_response=true) # type: ignore
See the mypy docs: https://mypy.readthedocs.io/en/latest/common_issues.html#spurious-errors-and-locally-silencing-the-checker
本文标签: pythonMissing type parameters for generic type quotStrictRedisquotStack Overflow
版权声明:本文标题:python - Missing type parameters for generic type "StrictRedis" - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745287311a2651576.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论