python / python/mypy

Errors on multi-variable assignments of types

未关闭
#10,101 11 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

documentation topic-type-alias
主要语言
Python
星标
20.6k
派生
3.3k
PR 合并指标
PR 指标待抓取

描述

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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先对 repro2.py 中的三个复现用例运行 mypy,并比较单一赋值、元组赋值和链式赋值是如何被记录为类型别名的。跟踪这些赋值和函数注解的类型检查路径;完成的标准是三个签名都不产生错误,同时现有的别名和变量检查保持不变。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
compilers
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
38/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。