python / python/cpython

`isinstance` on multiple-inheritance class erases type hints

Open
#124,840 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

3.12 3.13 3.14 stdlib topic-typing type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied reproducer on CPython and compare get_type_hints before and after the isinstance check. Trace the get_type_hints and runtime-checkable Protocol paths involved in that check. Done means the derived Thing instance still reports {"a": str} after isinstance(t, Hello).

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.