【发布时间】:2021-12-21 12:01:38
【问题描述】:
如果 set 使用 add()
code.py
adgroups_by_campaign_id: Dict[CampaignId, Set[str]] = defaultdict(set)
for customer_id, campaign_ids in campaigns_per_customer_id.items():
adgroups = get_adgroups_in_campaings(ads_client, customer_id, campaign_ids, adgroup_names)
for adgroup in adgroups:
adgroups_by_campaign_id[CampaignId(adgroup['campaign_id'])].add(
(adgroup['adgroup_name'], adgroup['adgroup_resource_name']) -> RETURN ERROR
)
错误正文
error: Argument 1 to "add" of "set" has incompatible type "Tuple[str, str]"; expected "str"
据我所知,将新连音添加到集合中是常见的做法。
add() 方法可以将元组对象作为元素添加到集合中
为什么mypy认为不允许?
【问题讨论】:
-
因为你之前告诉 mypy 是
Set[str] -
错误信息很清楚,您将其注释为
Set[str],但您尝试将Tuple[str, str]添加到此集合中,但它需要str....跨度>