python / python/cpython

`isinstance` on multiple-inheritance class erases type hints

Aperta
#124,840 9 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

3.12 3.13 3.14 stdlib topic-typing type-bug
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia eseguendo il riproduttore fornito su CPython e confronta get_type_hints prima e dopo il controllo isinstance. Traccia i percorsi get_type_hints e runtime-checkable Protocol coinvolti in quel controllo. Il lavoro è completato quando l’istanza derivata di Thing continua a riportare {"a": str} dopo isinstance(t, Hello).

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
backend
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.