Cast for input function monkey-patch doesn't fix things as expected type changes
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 20.6k
- Fork
- 3.3k
- Chỉ số merge pull request
- Chỉ số pull request đang chờ
Mô tả
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
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Tái hiện vấn đề bằng cách lưu các snippet được cung cấp và chạy mypy mà không có cờ dòng lệnh hoặc cấu hình. Bắt đầu bằng việc theo dõi cách chữ ký tích hợp của input và DefaultArg được hiển thị trong lỗi phép gán; hoàn tất khi chẩn đoán ban đầu báo cáo dạng DefaultArg bắt buộc và không còn đề xuất một cast không đủ.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- python
- Lĩnh vực
- tooling
- Loại issue
- Lỗi
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 45/100