No type error detected when function with wrong type is used in the middle of a pipe
还没有人认领这个 Issue。
评估
调研方向
先从复现器开始并运行 mypy,然后检查 returns/_internal/pipeline/pipe.py 和 flow.py 中提到的 pipeline 入口,以及 pipe 使用的类型标注。将此案例与正常工作的第一个参数错误案例进行比较;当 mypy 拒绝不兼容的中间函数且不破坏有效 pipeline 时,即视为完成。
由索引模型根据 Issue 内容生成。
描述
Bug report
Thank you for this project! I'm new to Python, so I apologize if I've overlooked something.
What's wrong
from collections.abc import Callable
from returns.pipeline import flow, pipe
from returns.pointfree import map_
from returns.result import Success
def get_data(data: str) -> str:
return "200" + data
def do_more(data: int) -> int:
return data - 300
def transform(s: str) -> str:
return f"transformed: {s}"
my_pipe: Callable[[str], str] = pipe(get_data, do_more, transform)
print(flow(Success("500"), map_(my_pipe)))
Gives following runtime error while mypy doesn't complain:
(py-starter) python-boilerplate > python working.py
Traceback (most recent call last):
File "/Users/hoschi/repos/python-boilerplate/working.py", line 22, in <module>
print(flow(Success("500"), map_(my_pipe)))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Caskroom/miniconda/base/envs/py-starter/lib/python3.12/site-packages/returns/_internal/pipeline/flow.py", line 50, in flow
return reduce( # type: ignore
^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Caskroom/miniconda/base/envs/py-starter/lib/python3.12/site-packages/returns/_internal/pipeline/flow.py", line 51, in <lambda>
lambda composed, function: function(composed), # type: ignore
^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Caskroom/miniconda/base/envs/py-starter/lib/python3.12/site-packages/returns/pointfree/map.py", line 55, in factory
return container.map(function)
^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Caskroom/miniconda/base/envs/py-starter/lib/python3.12/site-packages/returns/result.py", line 426, in map
return Success(function(self._inner_value))
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Caskroom/miniconda/base/envs/py-starter/lib/python3.12/site-packages/returns/_internal/pipeline/pipe.py", line 34, in <lambda>
return lambda instance: flow(instance, *functions)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Caskroom/miniconda/base/envs/py-starter/lib/python3.12/site-packages/returns/_internal/pipeline/flow.py", line 50, in flow
return reduce( # type: ignore
^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Caskroom/miniconda/base/envs/py-starter/lib/python3.12/site-packages/returns/_internal/pipeline/flow.py", line 51, in <lambda>
lambda composed, function: function(composed), # type: ignore
^^^^^^^^^^^^^^^^^^
File "/Users/hoschi/repos/python-boilerplate/working.py", line 13, in do_more
return data - 300
~~~~~^~~~~
TypeError: unsupported operand type(s) for -: 'str' and 'int'
How is that should be
Mypy should detect the error. It can detect the error when the problem function is used at first arg: pipe(do_more, get_data, transform)
System information
-
pythonversion: 3.12 -
returnsversion:returns = {extras = ["compatible-mypy"], version = "^0.26.0"} -
mypyversion:mypy = "^1.17.1" -
hypothesisversion (if any): -
pytestversion (if any):
- 主要语言
- Python
- 星标
- 4.4k
- 派生
- 154
- 平均合并
- 3 小时 5 分钟
- 30 天内合并 PR
- 22
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
dry-python/returns 的其他 Issue
-
难度 4/5 3-5 天 新手友好度 48/100
dry-python/returns#2394 · 1 个 reaction ·
-
难度 4/5 3-5 天 新手友好度 52/100
dry-python/returns#2365 ·
-
难度 5/5 一周以上 新手友好度 25/100
dry-python/returns#2355 · 1 条评论 ·
-
bug
难度 4/5 3-5 天 新手友好度 45/100
dry-python/returns#2295 · 4 条评论 · 1 个 reaction ·
-
Incorrect result type for `returns.curry.partial` with keyword arguments for positional parameters 未关闭bug
难度 3/5 1-2 天 新手友好度 45/100
dry-python/returns#2191 · 1 条评论 ·
查看 dry-python/returns 的全部 Issue
相似的 Issue
-
bug confirmed issue
难度 2/5 1-3 小时 新手友好度 75/100
open-webui/open-webui#30750 · 1 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 75/100
-
enhancement
难度 2/5 1-3 小时 新手友好度 75/100
OpenwaterHealth/openmotion-bloodflow-app#604 · 1 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 70/100
-
good first issue
难度 1/5 1 小时以内 新手友好度 90/100