Skip to content

Better message for incompatible type error #3382

@rcarrington

Description

@rcarrington

Consider the following snippet:

Foo = NewType('Foo', int)
Bar = NewType('Bar', str)
Baz = NewType('Baz', str)


def things(thing_1: Bar, thing_2: Bar) -> Baz:
    return Baz(thing_1 + thing_2)


stuff: Dict[Foo, Callable[[Bar, Bar], Bar]] = {
    Foo(1): things
}

it = stuff.get(Foo(1))
result = things(Bar('test'), Bar('strings'))

The result of running mypy is this:
foo.py:11: error: Dict entry 0 has incompatible type "Foo":
This message is a bit misleading (possibly truncated?); the issue here is with the mismatch in return type between lines 7 and 11 (Bar vs. Baz), not with the type of the key in the dictionary.

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