Skip to content

Dict constructor: Generator has incompatible item type "object"; expected "Tuple[Any, Any]" #5287

Closed
@beamerblvd

Description

@beamerblvd

I filed an issue about improving the dict constructor signature on typeshed, but I think there's also a related bug in the way MyPy is generating this error.

I have the following code:

parameters = dict(
    map(str.strip, p.split('=', 1)) if '=' in p else (p.strip(), None)
    for p in encoded_parameters.split(';')
) if encoded_parameters else {}  # type: Dict

On the second line of this, I get the following error:

error: Generator has incompatible item type "object"; expected "Tuple[Any, Any]"

I get that the error is due to my above-reported problem with the dict constructor signature; however, I also don't believe the error is right. First, the object is too generic. But second, if I improve it slightly (improved for type-checking purposes; the code isn't actually any better/worse):

parameters = dict(
    tuple(map(str.strip, p.split('=', 1))) if '=' in p else (p.strip(), None)
    for p in encoded_parameters.split(';')
) if encoded_parameters else {}  # type: Dict

This yields the exact same error, and it shouldn't. I now have a generator of tuples, and it should recognize that. There should be no error with this second approach.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions