String hinted arguments passed numerical values should not be coerced

未关闭 适合新手
#733 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
70/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
python
领域
cli

调研方向

issue 提供了一个位于 tp.py 的最小复现,但没有指出项目文件或测试。首先追踪 Fire 如何解析 CLI 值并应用 callable 参数的类型提示,然后找到与 string 类型提示参数相关的测试。完成标准是 1.10 和 '1.10' 都以字符串形式传入函数,并有一个回归测试覆盖这一行为。

由索引模型根据 Issue 内容生成。

描述

Observed behavior

If a callable command function defines an argument with a string hint, e.g. arg: str, and a value of 1.10 is passed to it, then it receives an argument of type float with value 1.1 which has different meaning than the passed value.

This is particularly troublesome when dealing with versions since 1.1 is much different than 1.10.

Expected behavior

Fire should not coerce the value to a type different than the type hint, regardless of its format.

Reproduction

Code
# tp.py
import fire


def main(arg: str):
    print(f"Hello! {type(arg)} {arg}")


if __name__ == "__main__":
    fire.Fire(main)
Execution
$ python tp.py 1.10
Hello! <class 'float'> 1.1

or

$ python tp.py '1.10'
Hello! <class 'float'> 1.1

Metadata

fire version v0.7.1

主要语言
Python
星标
28.2k
派生
1.5k
PR 合并指标
30 天内没有已合并 PR

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

google/python-fire 的其他 Issue

查看 google/python-fire 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。