Cast for input function monkey-patch doesn't fix things as expected type changes
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 20.6k
- 派生
- 3.3k
- PR 合并指标
- PR 指标待抓取
描述
Bug Report
I'm writing code that monkey-patches the built-in input function, like so:
import builtins
def f(x: str) -> str:
print(x)
return 'hi'
builtins.input = f
print(input('what')) # prints 'what' and then 'hi'
MyPy reasonably gives me a type error on the assignment to input, saying:
t.py:7: error: Incompatible types in assignment (expression has type "Callable[[str], str]", variable has type "Callable[[object], str]") [assignment]
Found 1 error in 1 file (checked 1 source file)
Okay, fine so I change my code like this, exactly matching the reported variable type:
import builtins
from typing import cast, Callable
def f(x: str) -> str:
print(x)
return 'hi'
builtins.input = cast(Callable[[object], str], f)
print(input('what')) # prints 'what' and then 'hi'
I'm now casting to exactly what the error message said was expected. However, I still get a mypy error on the same line, and the expected type has changed:
t.py:9: error: Incompatible types in assignment (expression has type "Callable[[object], str]", variable has type "Callable[[DefaultArg(object)], str]") [assignment]
Found 1 error in 1 file (checked 1 source file)
Looking it up, it seems like DefaultArg is from mypy_extensions? That wasn't obvious, but I can get this to work by doing:
import builtins
from typing import cast, Callable
from mypy_extensions import DefaultArg
def f(x: str) -> str:
print(x)
return 'hi'
builtins.input = cast(Callable[[DefaultArg(object)], str], f)
print(input('what')) # prints 'what' and then 'hi'
To Reproduce
Run the code snippets provided above through mypy by saving them to a file and running mypy filename.
Expected Behavior
If I cast to the type that the error message says it needs, then the type error should go away.
If an "optional extensions" type is required to correctly type code dealing with a core built-in function, it should be mentioned somewhere in the python.org core documentation maybe? I first turned to searching docs.python.org to figure out what DefaultArg was so I could figure out where to import it from, but there are zero hits. Web searches without careful quoting are near useless, since modern search engines find all sorts of conversations about default arguments that have nothing to do with mypy. Thankfully I was clued in enough to search my mypy documentation pages and finally found it. I realize this may not be fixable, but maybe some kind of reminder could be included in the error message when DefaultArg is required?
The minimal correct fix here should be for the original error message to include DefaultArg in the expected type, since defining a function which has no default for its first argument and setting that as input will cause crashes for people who expect the usual behavior.
Actual Behavior
First type error message is just wrong about the expected/required type.
Your Environment
- Mypy version used: 1.8.0, so I upgraded to 1.10.1 (current latest) and have the exact same issue.
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini(and other config files): none specified - Python version used: 3.11.8
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
通过保存提供的代码片段并在不使用任何命令行标志或配置的情况下运行 mypy,复现该问题。首先跟踪内置 input 签名和 DefaultArg 在赋值错误中是如何呈现的;完成标准是初始诊断报告所需的 DefaultArg 形式,并且不再建议使用不充分的 cast。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- tooling
- Issue 类型
- 缺陷
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 45/100