1.11 regression: "generic" mapping failing with confusing error message
未关闭
还没有人认领这个 Issue。
bug
- 主要语言
- Python
- 星标
- 20.6k
- 派生
- 3.3k
- PR 合并指标
- PR 指标待抓取
描述
Bug Report
I've boiled down a minimal example -- this pattern is used by pyupgrade (and reorder-python-imports) to register ast functions and has been working with mypy since version 0.710 -- but was broken with the last update sadly
To Reproduce
this is a simplified version of pyupgrade/_data.py -- FUNCS acts as a mapping from ast types to their callback functions
import ast
import collections
from typing import Callable, Protocol, TypeVar
AST_T = TypeVar('AST_T', bound=ast.AST)
ASTFunc = Callable[[AST_T], None]
FUNCS = collections.defaultdict(list)
def register(tp: type[AST_T]) -> Callable[[ASTFunc[AST_T]], ASTFunc[AST_T]]:
def register_decorator(func: ASTFunc[AST_T]) -> ASTFunc[AST_T]:
FUNCS[tp].append(func)
return func
return register_decorator
class ASTCallbackMapping(Protocol):
def __getitem__(self, tp: type[AST_T]) -> list[ASTFunc[AST_T]]: ...
def visit(funcs: ASTCallbackMapping) -> None: ...
def f() -> None:
visit(FUNCS)
Expected Behavior
the behaviour pre-mypy-1.11:
(no errors)
Actual Behavior
$ mypy t.py
t.py:24: error: Argument 1 to "visit" has incompatible type "defaultdict[type[AST_T], list[Callable[[AST_T], None]]]"; expected "ASTCallbackMapping" [arg-type]
t.py:24: note: Following member(s) of "defaultdict[type[AST_T], list[Callable[[AST_T], None]]]" have conflicts:
t.py:24: note: Expected:
t.py:24: note: def [AST_T: AST] __getitem__(self, type[AST_T], /) -> list[Callable[[AST_T], None]]
t.py:24: note: Got:
t.py:24: note: def __getitem__(self, type[AST_T], /) -> list[Callable[[AST_T], None]]
Found 1 error in 1 file (checked 1 source file)
the error message seems suspicious because I think those two are the same ?
Your Environment
- Mypy version used: 1.11.0
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.10.12 -- though really anything modern
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
使用 mypy 1.11,通过 t.py 中的最小示例重现该回归。以 pyupgrade/_data.py 作为动机示例,比较泛型协议和 defaultdict 的兼容性行为。当有效 mapping 能够在没有误导性的类型不兼容错误的情况下被接受,并且该回归由适当的测试覆盖时,即视为完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- compilers, devtools
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 45/100