Skip to content

Subtype of mapping is not compatible type in .update() method #10970

@Dreamsorcerer

Description

@Dreamsorcerer
    async def psubscribe(self, *args: ChannelT, **kwargs: PubSubHandler):
        new_patterns: Dict[ChannelT, Optional[PubSubHandler]] = dict.fromkeys(args)
        new_patterns.update(kwargs)

This code (taken from aioredis) produces this error:
error: Argument 1 to "update" of "dict" has incompatible type "Dict[str, Callable[[Dict[str, str]], None]]"; expected "Mapping[Union[bytes, str, memoryview], Optional[Callable[[Dict[str, str]], None]]]" [arg-type]

But, the types are compatible as the keys and values are part of the expected union, I can't think of any operation that would make this unsafe. I understand it could be unsafe if the expectation was MutableMapping, as the function might assign any of the unioned types, but for Mapping this looks completely safe to me.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions