Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 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?

>Solution :

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]].

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading