代码之家  ›  专栏  ›  技术社区  ›  Sujay

泛型类型“StrictRedis”缺少类型参数

  •  0
  • Sujay  · 技术社区  · 10 月前
    import redis
    
    a = redis.StrictRedis(host="a", port=123, db=0, decode_response=true)
    

    Mypy出现错误 Missing type parameters for generic type "StrictRedis" [type-arg]

    Python版本: 3.9.20

    Mypy版本: 1.14.1

    2 回复  |  直到 10 月前
        1
  •  2
  •   Daraan    10 月前

    这是一个理论上可以忽略的类型警告。这个 types-redis 作为扩展的一部分 typeshed 您的类型检查器正在使用的定义 Redis 作为一个 Generic 守法庭行为 str | bytes .
    这样做是为了更好地了解某些方法的输出或输入,例如 pipeline -> str | bytes register_script(script: str | bytes) -> Script .

    您应该将其初始化为:

    a = redis.StrictRedis[your-type-here](host="a", port=123, db=0, decode_response=true)
    

    如果你不知道该用哪一个,就用 redis.StrictRedis[str | bytes](... 或者只是添加一个 # type: ignore[type-arg]

        2
  •  0
  •   digitalarbeiter    10 月前

    (不知道你的问题是什么。)

    如果Redis客户端库不提供类型提示,可以使用 # type: ignore 告诉我。

    a = redis.StrictRedis(host="a", port=123, db=0, decode_response=true)  # type: ignore
    

    请参阅mypy文档: https://mypy.readthedocs.io/en/latest/common_issues.html#spurious-errors-and-locally-silencing-the-checker

    推荐文章