Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 类型注释:输入和输出类型之间的依赖关系_Python_Mypy - Fatal编程技术网

Python 类型注释:输入和输出类型之间的依赖关系

Python 类型注释:输入和输出类型之间的依赖关系,python,mypy,Python,Mypy,现在我有一个函数: def foo(a: List) -> Any: return a[1] 我需要避免任何,并采取以下措施: def foo(a: List[T]) -> T: return a[1] 可能吗?是的,它叫: import typing T = typing.TypeVar("T") def foo(a: typing.Sequence[T]) -> T: return a[1] x = "a" x = foo([1]) # e

现在我有一个函数:

def foo(a: List) -> Any:
    return a[1]
我需要避免
任何
,并采取以下措施:

def foo(a: List[T]) -> T:
    return a[1]
可能吗?

是的,它叫:

import typing

T = typing.TypeVar("T")

def foo(a: typing.Sequence[T]) -> T:
    return a[1]

x = "a"
x = foo([1])
# error: Incompatible types in assignment (expression has type "int", variable has type "str")