最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Missing type parameters for generic type "StrictRedis" - Stack Overflow

programmeradmin1浏览0评论
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

Share Improve this question edited Jan 30 at 5:52 Daraan 4,2427 gold badges22 silver badges47 bronze badges asked Jan 29 at 17:22 SujaySujay 3752 silver badges9 bronze badges 2
  • 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
Add a comment  | 

2 Answers 2

Reset to default 2

This 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

发布评论

评论列表(0)

  1. 暂无评论