python / python/typing

spec/conformance: function with TypeVar default and default argument used in context.

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

还没有人认领这个 Issue。

topic: typing spec
主要语言
Python
星标
1.8k
派生
302
平均合并
23 小时
30 天内合并 PR
8

描述

I want to suggest adding the following test to the conformance test suite:

class Getter[T]:
    def get[S=None](self, default: S = None) -> T | S: ...  # OK

def test(arg: Getter[str]) -> None:
    result: str = arg.get()  # type: ignore
  • Since the default argument value is assignable to the default type var type, def get is well-defined
  • arg.get() implicitly uses None of type None as the argument. Therefore, we have an implicit constraint S <: None when .get is called without explicit default.

Therefore, result: str = arg.get() should raise an assignment type error (str | None not assignalbe to str).

Currently, only pyright succeeds on this test, and type checker results diverge.

  1. pyright: no errors (true negative on def get, true positive on result) (pyright playground)
  2. mypy: false positive on def get, false negative on result.
  3. ty: false positive on def get, true positive on result. (playground)
  4. pyrefly: false positive on def get, false negative on result (playground)
  5. zuban: false postive on def get, false negative on result.

Related Issues

贡献指南

这个仓库没有索引到贡献指南

从这里开始

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

调研方向

首先定位 typing 一致性测试套件,以及其中针对 TypeVar 默认值和默认参数的现有测试。添加所提议的 Getter 示例,并包含预期的定义和赋值诊断,然后比较所列各类型检查器的结果,以明确一致性预期。

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

评估

技术栈
python
领域
testing
Issue 类型
功能
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
42/100

把新 issue 发到你的邮箱

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