Feature Request: Let Pipe function take more than 1 positional argument.
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 35/100
- Loại issue
- Tính năng
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Đình trệ
- Công nghệ
- python
- Lĩnh vực
- developer-experience
Hướng nghiên cứu
Bắt đầu với returns/_internal/pipeline/pipe.py và pipe.pyi đi kèm, sau đó tái hiện các ví dụ bằng thiết lập kiểm tra kiểu của dự án. Xác định cách nhiều đối số vị trí truyền qua hàm đầu tiên, đồng thời vẫn giữ các lỗi kiểu đối với những pipeline không tương thích; hoàn tất khi ví dụ bazz chạy mà không có TypeError và các phép kết hợp không hợp lệ vẫn bị từ chối.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Feature Request/Enhancement
I stumbled upon this post a while ago:
https://github.com/python/typing/discussions/1245?sort=top#discussioncomment-3454192
It was pretty neat, and wanted to try it out for functions that take more than 1 positional argument. Sadly, it does not seem to work.
What's wrong
When trying to run the following code:
from returns._internal.pipeline.pipe import pipe
def one_arg_only(num1: float) -> int:
return int(num1)
def two_args(num1: float, num2: int) -> int:
return int(num1 + num2)
def to_string(f: float) -> str:
return str(f)
def to_float(s: str) -> float:
return float(s)
if __name__ == "__main__":
fizz = pipe(one_arg_only, to_string, to_float)
print(fizz(1)) # Trivial example
buzz = pipe(to_string, to_float)
print(buzz(two_args(1, 2))) # This works, but is not ideal
bazz = pipe(two_args, to_string, to_float)
try:
print(bazz(1, 2)) # Too many arguments for "__call__" of "_Pipe" [call-arg]
except TypeError as e:
print(
"Cannot pass two arguments even though first function requires 2 positional arguments"
)
raise e
you would get the following exception:
Cannot pass two arguments even though first function requires 2 positional arguments
Traceback (most recent call last):
File "/home/pratik/workplace/dry-python-returns/problem.py", line 21, in <module>
raise e
File "/home/pratik/workplace/dry-python-returns/problem.py", line 16, in <module>
print(fizz(1, 2))
^^^^^^^^^^
TypeError: pipe.<locals>.<lambda>() takes 1 positional argument but 2 were given
How is that should be
Invoking bazz should not throw a TypeError.
What I have tried
The following works if and only if the types are all correct.
from functools import reduce
from typing import overload, ParamSpec, TypeVar, Callable
_P = ParamSpec("_P")
_T1 = TypeVar("_T1")
_T2 = TypeVar("_T2")
_T3 = TypeVar("_T3")
@overload
def pipe(f1: Callable[_P, _T1]) -> Callable[_P, _T1]: ...
@overload
def pipe(
f1: Callable[_P, _T1],
f2: Callable[[_T1], _T2],
) -> Callable[_P, _T2]: ...
@overload
def pipe(
f1: Callable[_P, _T1],
f2: Callable[[_T1], _T2],
f3: Callable[[_T2], _T3],
) -> Callable[_P, _T3]: ...
def pipe(*functions):
def compose2(f, g):
return lambda *args, **kwargs: g(f(*args, **kwargs))
return reduce(compose2, functions)
When the types are not correct, mypy throws the following error message:
# Mypy error says: Cannot infer type argument 3 of "pipe"
fizz = pipe(one_arg_only, to_string, to_string)
I toyed around with typing.ParamSpec within returns/_internal/pipeline/pipe.pyi to see if that would do the trick, but I couldn't after a couple of hours of getting the above example to have mypy return no errors. Maybe I missed something, or I just have mind block.
Related issue: https://github.com/python/typing/discussions/1289
- 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#2295 · 4 bình luận · 1 reaction ·
-
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 ·
Tất cả issue của dry-python/returns
Issue tương tự
-
[Bug] reef-hermes tells me to resume with hermes --resume, which does not work from my shell Đang mởarea: harness bug status: needs-triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
Human-Agent-Society/reef#625 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 80/100
learningequality/kolibri#15351 · 2 bình luận ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
-
Name consistency Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
eellak/triplestore#65 · 1 bình luận ·