Erronous 'Incompatible type' when using descriptor in metaclass
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 20.6k
- Forks
- 3.3k
- Métricas de merge de PR
- Métricas de PR pendientes
Descripción
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
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza con el reproductor proporcionado de descriptor y metaclase, ejecuta mypy con el ejemplo de Python mostrado y comprueba cómo gestiona la asignación al descriptor a través de una metaclase. Se considera terminado cuando se acepta DataModel.current_dut = dut, mientras que reveal_type(DataModel.current_dut) sigue siendo DUT.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- devtools
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100