python / python/mypy

Odd error message when kwargs passed as **dict

Open
#5,382 17 comments 34 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature priority-0-high topic-calls topic-usability
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Checking this with mypy 0.620:

from concurrent.futures import ThreadPoolExecutor
import sys

opts = {'max_workers': None}
if sys.version_info >= (3, 6):
    opts['thread_name_prefix'] = 'SyncWorker'
executor = ThreadPoolExecutor(**opts)

...yields:

mypy_bug.py:6: error: Incompatible types in assignment (expression has type "str", target has type "None")
mypy_bug.py:7: error: Argument 1 to "ThreadPoolExecutor" has incompatible type "**Dict[str, None]"; expected "str"

Let's ignore the first error for now. But the second seems to have an incorrect message, specifically I think the **Dict[str, None] in it is weird. Just guessing, is it trying to assign something to the ThreadPoolExecutor's thread_name_prefix arg which is a str? Maybe the None from max_workers because passing kwargs as a dict like this might result in random/varying order of the dict items? But if that's the case, I'd expect it to say something else than **Dict[str, None] as the culprit, maybe None or int, but not the whole **dict.

Another thought is that when kwargs is passed as **dicts like this, their order is not really defined, so "Argument 1" isn't very helpful. Would be better to say the argument's name.

But maybe I'm just all off track here -- will leave the rest to someone who actually has an informed opinion :)

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 with the ThreadPoolExecutor reproducer in the issue and run it against mypy 0.620 to inspect the reported diagnostics for **opts. Trace the handling of dictionary-based keyword arguments and determine what the error should identify; done means the diagnostic is accurate and names the relevant argument or value.

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
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.