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

赋值中的类型不兼容(表达式的类型为“List[<nothing>]”,变量的类型为(…)

  •  0
  • d33tah  · 技术社区  · 5 年前

    from typing import List, Union
    
    T_BENCODED_LIST = Union[List[bytes], List[List[bytes]]]
    ret: T_BENCODED_LIST = []
    

    当我用mypy测试它时,我得到以下错误:

    example.py:4: error: Incompatible types in assignment (expression has type "List[<nothing>]", variable has type "Union[List[bytes], List[List[bytes]]]")
    

    0 回复  |  直到 5 年前
        1
  •  0
  •   JGC    4 年前

    这与以下mypy错误有关: https://github.com/python/mypy/issues/2164

    我能想出的唯一解决办法就是让mypy忽略任务:

    from typing import List, Union
    
    # Define the variable with type hint
    T_BENCODED_LIST: Union[List[bytes], List[List[bytes]]]
    
    # Set the value to empty list and tell mypy to look the other way.
    T_BENCODED_LIST = []  # type: ignore