Mypy:集合更新期间出现不兼容的类型错误
Mypy: incompatible type error during set update
Mypy returns 如果 set 使用 add()
[=14 更新为新的 tuple 则出错=]
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"
据我所知,向集合中添加新的连音符是常见的做法。
The add() method can add a tuple object as an element in the set
为什么mypy认为不允许?
adgroups_by_campaign_id
被标记为 Dict[CampaignId, Set[str]]
意味着 mypy
将期望所有值都是包含字符串的集合,而不是字符串元组。
Set[str]
应该改为 Set[Tuple[str, str]]
.
Mypy returns 如果 set 使用 add()
[=14 更新为新的 tuple 则出错=]
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"
据我所知,向集合中添加新的连音符是常见的做法。
The add() method can add a tuple object as an element in the set
为什么mypy认为不允许?
adgroups_by_campaign_id
被标记为 Dict[CampaignId, Set[str]]
意味着 mypy
将期望所有值都是包含字符串的集合,而不是字符串元组。
Set[str]
应该改为 Set[Tuple[str, str]]
.