Skip to content
Advertisement

Mypy: incompatible type error during set update

Mypy returns an error if the set is updated with new tuple using 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 body

 error: Argument 1 to "add" of "set" has incompatible type "Tuple[str, str]"; expected "str"

As far as I know, it is common practice to add new tuplets to the set.

The add() method can add a tuple object as an element in the set

Why does mypy think it’s not allowed?

Advertisement

Answer

adgroups_by_campaign_id is marked as Dict[CampaignId, Set[str]] meaning that mypy will expect all the values to be sets that contain strings, not tuples of strings.

Set[str] should be changed to Set[Tuple[str, str]].

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement