python / python/cpython

Passing wrong number of arguments to generic produces runtime errors

未关闭
#157,810 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

stdlib topic-typing type-feature
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

In pypa/hatch#2437, we see a change to a generic type, adding an extra parameter, causing a run-time failure, and since this was used in a build plugin, it cause a build failure and my package to fail to install.

pdm.exceptions.BuildError: Build backend raised error: Showing the last 10 lines of the build output:
    class GitInfoBuildHook(BuildHookInterface[Any]):
                           ~~~~~~~~~~~~~~~~~~^^^^^
  File "/usr/lib/python3.12/typing.py", line 398, in inner
    return func(*args, **kwds)
           ^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/typing.py", line 1083, in _generic_class_getitem
    _check_generic(cls, params, len(cls.__parameters__))
  File "/tmp/pdm-build-env-4xoutxun-shared/lib/python3.12/site-packages/typing_extensions.py", line 3274, in _check_generic
    raise TypeError(f"Too {'many' if alen > elen else 'few'} arguments"
TypeError: Too few arguments for <class 'hatchling.builders.hooks.plugin.interface.BuildHookInterface'>; actual 1, expected at least 2

I find this behavior fairly surprising; I would only expect type errors in an annotation to cause errors when doing typechecking, not runtime errors like this.

贡献指南

打开贡献指南

从这里开始

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

调研方向

首先,通过 traceback 中显示的 typing.py 路径复现报告的泛型参数失败,然后比较 typing_extensions.py 中对应的验证逻辑。当针对错误泛型参数数量的预期行为得到确立,并通过回归测试覆盖,同时不破坏有效的运行时 typing 使用时,该问题即得到解决。

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

评估

技术栈
python
领域
backend
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
活跃
描述清晰度
基本清楚
新手友好度
45/100

把新 issue 发到你的邮箱

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