python / python/mypy

`Any` subclass in `Dict` not being compatible with `Any` generates confusing message

Open
#9,013 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

priority-1-normal topic-usability
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Found by @nipunn1313 in python/typeshed#4237. Please consider:

from typing import Any, Dict

class Foo(Any):
    pass

d: Dict[Foo, Foo]

def bar(x: Dict[str, str]) -> None:
    pass

bar(d)  # error: Argument 1 to "bar" has incompatible type "Dict[Foo, Foo]"; expected "Dict[str, str]"

This fails with the error marked inline, while this passes:

from typing import Any, Dict

class Foo(Any):
    pass

foo: Foo

def bar(x: str) -> None:
    pass

bar(foo)

mypy version 0.780, Python 3.8.2.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied Python reproducer with mypy and compare its diagnostics for Dict[Foo, Foo] and Foo, using the reported mypy 0.780 behavior as the baseline. Trace the type-compatibility path for a Dict containing a subclass of Any. Done means the Dict case no longer produces the confusing incompatible-type message while the scalar Any-subclass case remains valid.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.