Erronous 'Incompatible type' when using descriptor in metaclass
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Bug Report
When using descriptors in a metaclass, mypy will errounously throw 'Incompatible types in assignment' when accessing the described attribute in the derived class.
To Reproduce
` from typing import Generic, TypeVar, Any
from dataclasses import dataclass
@dataclass
class DUT:
Description: str = ""
ProductNumber: int = 0
T = TypeVar('T')
class DataDescriptor(Generic[T]):
def __init__(self, value: T) -> None:
self.value = value
def __set_name__(self, owner: Any, name: str) -> None:
self.name = name
def __get__(self, instance: Any, cls: Any) -> T:
return self.value
def __set__(self, obj: Any, value: T) -> None:
self.value = value
class DataModelMeta(type):
current_dut = DataDescriptor(DUT())
class DataModel(object, metaclass=DataModelMeta):
pass
dut: DUT = DUT("hello", 1234)
reveal_type(DataModel.current_dut)
reveal_type(dut)
DataModel.current_dut = dut`
# main.py:32: note: Revealed type is 'main.DUT*'
# main.py:33: note: Revealed type is 'main.DUT'
# main.py:34: error: Incompatible types in assignment (expression has type "DUT", variable has type "DataDescriptor[DUT]")
# Found 1 error in 1 file (checked 1 source file)
Expected Behavior
Assignment to DataModel.current_dut should expect the DUT type. mypy was able to infer this, as shown by reveal_type(DataModel.current_dut).
Actual Behavior
mypy throws an 'Incompatible type' error at the assignment
Your Environment
- Mypy version used: 0.790
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini(and other config files): None - Python version used: 3.8.6:db45529
- Operating system and version: Windows 7
This can also be reproduced on the latest Python / mypy at https://mypy-play.net
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con il riproduttore fornito per descriptor e metaclasse, esegui mypy con l'esempio Python mostrato e verifica come gestisce l'assegnazione al descriptor tramite una metaclasse. Il lavoro è completato quando DataModel.current_dut = dut viene accettato, mentre reveal_type(DataModel.current_dut) rimane DUT.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100