python / python/cpython

`isinstance` on multiple-inheritance class erases type hints

未关闭
#124,840 9 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

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

描述

Bug report

Bug description:

Steps to reproduce:

  • Create a runtime checkable protocol
  • Create one base class with type hints
  • Create a second base class that satisfies the protocol
  • Create a derived class that inherits from both the base classes
  • get_type_hints on the derived class and see they are correct
  • check the derived class isinstance of the protocol
  • get_type_hints on the derived class and see that they are a blank dictionary
from abc import abstractmethod
from typing import Protocol, get_type_hints, runtime_checkable

@runtime_checkable
class Hello(Protocol):
    @abstractmethod
    def hello(self) -> str: ...

class Base:
    a: str

class Base2:
    def hello(self) -> str:
        return "hello"

class Thing(Base, Base2): ...

def test_hints():
    t = Thing()
    assert get_type_hints(t) == {"a": str}
    assert isinstance(t, Hello)
    assert get_type_hints(t) == {"a": str}
CPython versions tested on:

3.11, 3.13

Operating systems tested on:

Linux

贡献指南

打开贡献指南

从这里开始

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

调研方向

首先在 CPython 上运行提供的复现程序,并比较 isinstance 检查前后的 get_type_hints。跟踪该检查涉及的 get_type_hints 和 runtime-checkable Protocol 路径。完成的标准是:在 isinstance(t, Hello) 之后,派生的 Thing 实例仍报告 {"a": str}。

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

评估

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

把新 issue 发到你的邮箱

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