python / python/mypy

Incompatible types in assignment for `dict[key] = tmp = value` and nested structures

Open
#5,289 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug false-positive priority-2-low
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

I have Python2 code building some nested structures like lists/tuples/sets/dicts in dicts. For populating the nested structure I keep a temporary reference, which mypy fails to parse if the (untyped) temporary reference is not the first variable being assigned:

#!/usr/bin/python2.7
from typing import Dict, Set  # noqa
x = {}  # type: Dict[str, Set[str]]
z = x[''] = set()  # okay
x[''] = y = set()  # fail: Incompatible types in assignment (expression has type "Set[<nothing>]", target has type "Set[str]")

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 minimal Python 2.7 reproduction shown in the issue and compare the two chained-assignment forms. Trace how mypy infers the untyped temporary in each case; done means the second form is accepted consistently with the first without weakening the declared Dict[str, Set[str]] checking.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.