python / python/mypy

Errors on multi-variable assignments of types

Open
#10,101 11 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

documentation topic-type-alias
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

MyPy seems to have issues when multi-variable assignments of types to their aliases are involved.

To Reproduce
Run mypy on the following two sources:

from typing import Any

typ_a = Any
typ_b = Any
def test1(x: typ_a, y: typ_b): pass
from typing import Any

typ_a, typ_b = Any, Any
def test2(x: typ_a, y: typ_b): pass

typ_a = typ_b = Any
def test3(x: typ_a, y: typ_b): pass

Expected Behavior
MyPy does not show any errors for any of the function's signatures, since they are all essentially the same.

Actual Behavior
MyPy shows the following errors for test2 and test3:

repro2.py:4: error: Variable "repro2.typ_a" is not valid as a type
repro2.py:4: note: See https://mypy.readthedocs.io/en/latest/common_issues.html#variables-vs-type-aliases
repro2.py:4: error: Variable "repro2.typ_b" is not valid as a type
repro2.py:7: error: Variable "repro2.typ_a" is not valid as a type
repro2.py:7: note: See https://mypy.readthedocs.io/en/latest/common_issues.html#variables-vs-type-aliases
repro2.py:7: error: Variable "repro2.typ_b" is not valid as a type
Found 4 errors in 1 file (checked 1 source file)

Confusingly, if one merges the two source files, no errors are shown.

Your Environment

  • Mypy version used: 0.800
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.9.1
  • Operating system and version: Ubuntu 20.04

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 mypy on the three reproductions in repro2.py and compare how single, tuple, and chained assignments are recorded as type aliases. Trace the type-checking path for those assignments and function annotations; done means all three signatures produce no errors while existing alias and variable checks remain unchanged.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.