False-positive type errors when passing **kwargs to a function that has other keyword args
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 20.6k
- Fork
- 3.3k
- Merge trung bình
- 1 ngày 18 giờ
- Pull request đã merge (30 ngày)
- 54
Mô tả
Bug Report
If a function signature contains one or more explicit keyword parameters followed by a **kwargs parameter, and the function is called on kwargs only, then mypy generates false positive type errors.
The number of errors generated is unbounded and appears to be equal to the number of distinct types found among the explicit keyword parameters. In addition, the errors are mutually inconsistent with one another (see example below).
To Reproduce
from typing import Any, Optional
def example(
*,
a: Optional[int] = None,
b: Optional[float] = None,
c: Optional[bool] = None,
d: Optional[list] = None,
**kwargs
) -> None:
print(kwargs)
kwargs = {'hello': 'world'}
example(**kwargs)
Expected Behavior
I'd expect this to pass type-checking: it looks perfectly correct to me. All I'm doing is passing kwargs into kwargs.
Actual Behavior
example.py:14: error: Argument 1 to "example" has incompatible type "**dict[str, str]"; expected "Optional[int]" [arg-type]
example.py:14: error: Argument 1 to "example" has incompatible type "**dict[str, str]"; expected "Optional[float]" [arg-type]
example.py:14: error: Argument 1 to "example" has incompatible type "**dict[str, str]"; expected "Optional[bool]" [arg-type]
example.py:14: error: Argument 1 to "example" has incompatible type "**dict[str, str]"; expected "Optional[list[Any]]" [arg-type]
Found 4 errors in 1 file (checked 1 source file)
In addition to being false positives, these errors are mutually inconsistent: mypy is claiming that an Optional[int] is expected in Argument 1, and also that an Optional[float] is expected in Argument 1, and also that an Optional[bool] is expected in Argument 1, and so on!
If I change the last line to this:
example(a=None, b=None, c=None, d=None, **kwargs)
then the errors go away. Is this really what mypy wants me to do??
Your Environment
- Mypy version used: 1.14.1
- Mypy command-line flags: None
- Mypy configuration options from
pyproject.toml:
allow_redefinition = true
disable_error_code = "no-any-unimported, no-any-return"
disallow_any_unimported = true
check_untyped_defs = true
follow_imports = "silent"
plugins = "pydantic.mypy"
show_error_codes = true
strict_optional = false
warn_return_any = true
warn_unused_ignores = true
- Python version used: 3.9.20
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
Bắt đầu bằng cách tái hiện ví dụ từ issue với mypy 1.14.1 và lần theo đường đi của việc kiểm tra đối số từ khóa đối với các tham số từ khóa tường minh theo sau bởi **kwargs. Thêm một bài kiểm thử hồi quy cho lời gọi được示示 và xác minh rằng lời gọi đó không tạo ra lỗi dương tính giả, trong khi các lời gọi có từ khóa tường minh không tương thích vẫn được kiểm tra.
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
- compilers, tooling
- Loại issue
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức phù hợp với người mới
- 35/100