gpt4 book ai didi

Python 静态类型提示/检查 Iterable[AnyStr] 与 Iterable[str] 之间的不匹配 |可迭代[字节]

转载 作者:行者123 更新时间:2023-12-04 11:34:47 25 4
gpt4 key购买 nike

我遇到了这个静态类型提示不匹配(使用 Pyright):

from __future__ import annotations
from typing import AnyStr, Iterable


def foo(i: Iterable[AnyStr]):
return i


def bar(i: Iterable[str] | Iterable[bytes]):
return i


def baz(i: Iterable[str | bytes]):
return i


def main():
s = ['a']

# makes sense to me
baz(foo(s)) # allowed
foo(baz(s)) # not allowed

# makes sense to me
baz(bar(s)) # allowed
bar(baz(s)) # not allowed

bar(foo(s)) # allowed
foo(bar(s)) # nope -- why?
Iterable[AnyStr]有什么区别和 Iterable[str] | Iterable[bytes] ?
它们不应该是“等效的”吗? (除了 AnyStr 指的是上下文中的单个一致类型)
更具体地说:键入提示以下内容的正确方法是什么?
import random
from typing import Iterable, AnyStr

def foo(i: Iterable[AnyStr]):
return i

def exclusive_bytes_or_str(): # type-inferred to be Iterator[bytes] | Iterator[str]
if random.randrange(2) == 0:
return iter([b'bytes'])
else:
return iter(['str'])

foo(iter([b'bytes'])) # fine
foo(iter(['str'])) # fine
foo(exclusive_bytes_or_str()) # same error

最佳答案

释义 answer来自 erictraut@github :

This isn't really the intended use for a constrained TypeVar. I recommend using an @overload instead:

@overload
def foo(i: Iterable[str]) -> Iterable[str]: ...
@overload
def foo(i: Iterable[bytes]) -> Iterable[bytes]: ...

def foo(i: Iterable[AnyStr]) -> Iterable[AnyStr]:
return i

因为:

The type Iterable[str] | Iterable[bytes] is not assignable to type Iterable[AnyStr]. A constrained type variable needs to be matched against one of its contraints, not multiple constraints. When a type variable is "solved", it needs to be replaced by another (typically concrete) type. If foo(bar(s)) were allowed, what type would the AnyType@foo type variable resolve to? If it were resolved to type str | bytes, then the concrete return type of foo would be Iterable[str | bytes]. That's clearly wrong.

关于Python 静态类型提示/检查 Iterable[AnyStr] 与 Iterable[str] 之间的不匹配 |可迭代[字节],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68869110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com