python / python/mypy

Mypy does not correctly understand MemberDescriptors for objects with slots.

Aperta
#9,644 0 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-descriptors
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report

When a Python object uses slots, MemberDescriptors are created for each attribute defined as a slot.

Mypy incorrectly interprets these fields as the type of the slotted value, rather than as a member descriptor.

To Reproduce

class TestObject(object):
   __slots__ = ["a", "b"]
   a: int
   b: str

   def __init__(self, a: int, b: str) -> None:
      self.a = a
      self.b = b

def test() -> None:
   x = TestObject(5, "hi")
   reveal_type(TestObject.a)
   reveal_type(TestObject.b)
   reveal_type(x.a)
   reveal_type(x.b)

Expected Behavior

Here is what the types actually are based on the python repl.

>>> TestObject
<class '__main__.TestObject'>
>>> x = TestObject(10, "hi")
>>> type(TestObject.a)
<class 'member_descriptor'>
>>> type(TestObject.b)
<class 'member_descriptor'>
>>> type(x.a)
<class 'int'>
>>> type(x.b)
<class 'str'>

Actual Behavior

Mypy output:
12: note: Revealed type is 'builtins.int'
13: note: Revealed type is 'builtins.str'
14: note: Revealed type is 'builtins.int'
15: note: Revealed type is 'builtins.str'

Your Environment

Python 3.8.1
mypy 0.782

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 con la riproduzione fornita di slots e MemberDescriptor, quindi traccia il modo in cui mypy determina i tipi rivelati per gli attributi di classe rispetto agli attributi di istanza. Conferma la correzione verificando che TestObject.a e TestObject.b siano riportati come descrittori di membro, mentre x.a e x.b rimangano int e str.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.