python / python/mypy

mypy does not recognize tuple unpacking syntax for TypeVars.

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

还没有人认领这个 Issue。

feature priority-2-low topic-runtime-semantics topic-type-variables
主要语言
Python
星标
20.6k
派生
3.3k
PR 合并指标
PR 指标待抓取

描述

Bug Report

This is really a minor issue, but if you declare multiple TypeVar variables in one line, mypy does not seem to recognize them as type variables. For example,

from typing import TypeVar, Generic
T1, T2 = TypeVar('T1'), TypeVar('T2')

class Container(Generic[T1]):
    class Item(Generic[T2]):
        pass

To Reproduce

  1. Declare more than one TypeVars in one line. (See the example code above.)
  2. Run mypy check on this script.

Expected Behavior

The T1 and T2 in the example above should be recognized as type variables.
They should pass mypy check.

Actual Behavior

Errors:

$ mypy typevar.py 
typevar.py:5: error: Free type variable expected in Generic[...]
typevar.py:6: error: Free type variable expected in Generic[...]
Found 2 errors in 1 file (checked 1 source file)

Your Environment

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

贡献指南

打开贡献指南

从这里开始

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

调研方向

使用提供的 Python 代码片段和 mypy typevar.py 命令重现该问题,然后跟踪在检查 Generic[...] 之前,如何识别通过元组赋值的 TypeVars。完成标准是 T1T2 都被接受为类型变量,并且示例通过 mypy 且没有错误。

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

评估

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

把新 issue 发到你的邮箱

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