我开始在 Python 3 中使用类型注释进行实验,并且遇到了函数问题exclude_filter
,特别是items
在下面的代码片段中进行了注释(我发布了这个未注释的)。简而言之,我正在尝试遍历列表并根据某些标准过滤掉一些项目。并且列表中项目的类型是类的实例或这些实例的元组,在这种情况下,我只在元组的第一个成员中寻找标准。
@dataclass
class BaseItem:
name: str
something: int
def exclude_filter(items, matches):
def item_matched(item, matches):
name = item[0].name if isinstance(item, tuple) else item.name
for match in matches:
if match in name:
return True
return False
items[:] = [i for i in items if not item_matched(i, matches)]
FOOS = [BaseItem("1st foo", 10), BaseItem("2nd foo", 11)]
BARS = [BaseItem("1st bar", 20), BaseItem("2nd bar", 22)]
FOOS_AND_BARS = list(zip(FOOS, BARS))
exclude_filter(FOOS, ["1st"])
exclude_filter(BARS, ["2nd"])
exclude_filter(FOOS_AND_BARS, ["1st"])
print(FOOS)
# [BaseItem(name='2nd foo', something=11)]
print(BARS)
# [BaseItem(name='1st bar', something=20)]
print(FOOS_AND_BARS)
# [(BaseItem(name='2nd foo', something=11), BaseItem(name='2nd bar', something=22))]
我已经尝试过明显错误items: List[BaseItem]
的结果:
Argument 1 to "exclude_filter" has incompatible type "List[Tuple[BaseItem, BaseItem]]"; expected "List[BaseItem]"
所以我试过item: List[Union[BaseItem, Tuple[BaseItem, BaseItem]]]
:
Argument 1 to "exclude_filter" has incompatible type "List[BaseItem]"; expected "List[Union[BaseItem, Tuple[BaseItem, BaseItem]]]"
然后我试过了T = TypeVar("T", BaseItem, Tuple[BaseItem, BaseItem])
,但我得到了:items: List[T]
item: T
"Tuple[BaseItem, BaseItem]" has no attribute "name"
好吧,我尝试了更晦涩的组合,但似乎没有任何效果。注释此代码的正确方法是什么?