python / python/mypy

false error with simple nested structure

Open
#17,143 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

mypy reports error where it is not suppose to.

To Reproduce

from typing import *                                                                                       
                                                                                                           
# types                                                                                                    
MyDict = TypedDict('MyDict', {"a" : int, "b" : str}, total=False)                                          
MyUnion = Union[                                                                                           
    Tuple[Literal["arg_int"], int],                                                                        
    Tuple[Literal["arg_str"], str],                                                                        
    Tuple[Literal["arg_dict"], MyDict]                                                                     
]                                                                                                          
                                                                                                           
# variables                                                                                                
var1 : MyUnion = ('arg_int', 1)                                                                            
var2 : MyUnion = ('arg_str', 'test')                                                                       
var3 : MyUnion = ('arg_dict', {'a': 1})    

Expected Behavior

No error is expected in this case.

Actual Behavior

The following error is reported:

error: Incompatible types in assignment (expression has type "tuple[str, dict[str, int]]", variable has type "tuple[Literal['arg_int'], int] | tuple[Literal['arg_str'], str] | tuple[Literal['arg_dict'], MyDict]")  [assignment]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.9.0
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.11.8

Also tryed with mypy playground with all latest versions and got the same result.

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 provided Python reproducer with mypy 1.9.0 or a current version and confirm the assignment error for var3. Trace the type-checking path for tuple assignments involving TypedDict and a Union, then add a regression test showing that all three assignments are accepted; done means the test passes without the false error.

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
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.