Pyright type checker won't let me pattern match just `Success` and `Failure` for a function returning `Result`
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 45/100
Hướng nghiên cứu
Tái hiện báo cáo bằng divtest.py với uv run pyright ., tập trung vào hàm div và các trường hợp match Success/Failure của nó. So sánh hành vi này với ví dụ pattern-matching được ghi trong tài liệu và xác định issue thuộc về returns hay Pyright; hoàn tất khi exhaustive match được chấp nhận hoặc một báo cáo Pyright đã được xác nhận được chuẩn bị.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Bug report
What's wrong
from returns.result import Failure, Success, safe
@safe
def div(first_number: int, second_number: int) -> int:
return first_number // second_number
def test_div() -> str:
match div(1, 0):
# Matches if the result stored inside `Success` is `10`
case Success(10):
return 'Result is "10"'
# Matches any `Success` instance and binds its value to the `value` variable
case Success(value):
return f'Result is "{value}"'
# Matches if the result stored inside `Failure` is `ZeroDivisionError`
case Failure(ZeroDivisionError()):
return '"ZeroDivisionError" was raised'
# Matches any `Failure` instance
case Failure(_):
return 'The division was a failure'
Given this code, taken directly from the documentation, but wrapped in a function with str return type instead of prints, I get the following error from pyright:
➜ divtest git:(main) ✗ uv run pyright .
/Users/Shared/Development/divtest/divtest.py
/Users/Shared/Development/divtest/divtest.py:9:19 - error: Function with declared return type "str" must return value on all code paths
"None" is not assignable to "str" (reportReturnType)
1 error, 0 warnings, 0 informations
I can fix this error by adding a catch-all branch _, but this kinda defeats the purpose of using a result and ruins type safety.
How is that should be
The type checker should know that Result and Success are the only possible return types for this function. The type checker shouldn't be asking for the _ branch.
System information
➜ divtest git:(main) ✗ uv tree
Resolved 5 packages in 1ms
divtest v0.1.0
├── pyright v1.1.407
│ ├── nodeenv v1.9.1
│ └── typing-extensions v4.15.0
└── returns v0.26.0
└── typing-extensions v4.15.0
➜ divtest git:(main) ✗ uv run python --version
Python 3.12.11
Comment
Please advise whether this is a returns issue or should I report it to pyright. I'm also open to creating a plugin for pyright if needed or otherwise investing some effort into resolving this. Also, I tried to join the telegram community and despite solving the numeric challenge I got kicked out a minute after entering.
Thanks
- Ngôn ngữ chính
- Python
- Star
- 4.4k
- Fork
- 154
- Merge trung bình
- 3 giờ 5 phút
- Pull request đã merge (30 ngày)
- 22
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.
Issue khác của dry-python/returns
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 48/100
dry-python/returns#2394 · 1 reaction ·
-
Composing 0-argument functions Đang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 52/100
dry-python/returns#2365 ·
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 25/100
dry-python/returns#2355 · 1 bình luận ·
-
bug
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 45/100
dry-python/returns#2253 · 2 bình luận ·
-
Incorrect result type for `returns.curry.partial` with keyword arguments for positional parameters Đang mởbug
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 45/100
dry-python/returns#2191 · 1 bình luận ·
Tất cả issue của dry-python/returns
Issue tương tự
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
stephrobert/dsoxlab#238 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
sublimehq/package_control#1780 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
nwg-piotr/nwg-displays#145 ·