0

我开始在 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"

好吧,我尝试了更晦涩的组合,但似乎没有任何效果。注释此代码的正确方法是什么?

4

1 回答 1

0

好吧,我找到了解决此问题的可行但丑陋的解决方案,遗憾的是它仅适用于同质列表。

BaseItemOrTuple = TypeVar(
    "BaseItemOrTuple",
    BaseItem,
    Tuple[BaseItem, BaseItem],
    Tuple[BaseItem, BaseItem, BaseItem],
    Tuple[BaseItem, BaseItem, BaseItem, BaseItem],
    # Et cetera for longer tuples
)

def exclude_filter(items: List[BaseItemOrTuple], matches: Sequence[str]) -> None:
    def item_matched(item: BaseItemOrTuple, matches: Sequence[str]) -> bool:
        if isinstance(item, tuple): #  cannot use one-liner here due to possible mypy bug
            name = item[0].name 
        else:
            name = 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)]

不变的最大问题List是:

Argument 1 to "exclude_filter" has incompatible type "List[Tuple[BaseItem, BaseItem]]"; expected "List[Tuple[BaseItem, ...]]"

这就是我不能使用的原因Tuple[BaseItem, ....]TypeVar我必须明确说明所有可能的元组长度。如果我正在使用就可以,Sequence但由于items[:]操作我不能。

此外,mypy 中可能存在带有条件表达式和使用的错误,isinstance()因此我需要使用正确的 if-else 块。

于 2020-07-23T14:34:25.503 回答