Wrong type inference for class with metaclass that acts as descriptor
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 20.6k
- Forks
- 3.3k
- Merge medio
- 1 d 18 h
- PR fusionados (30 d)
- 54
Descripción
Bug Report
Descriptor protocol does not work on metaclasses - defining __get__ method in a metaclass does not make classes using that metaclass work as descriptors.
To Reproduce
- Put this code in a py file:
from typing import Any, TYPE_CHECKING, overload
class _IntDescriptorMeta(type):
def __get__(self, instance: Any, owner: Any) -> int:
return 123
class IntDescriptorClass(metaclass=_IntDescriptorMeta):
...
class IntDescriptor:
def __get__(self, instance: Any, owner: Any) -> int:
return 123
class X:
number_cls = IntDescriptorClass
number = IntDescriptor()
print(X.number_cls)
print(X().number_cls)
print(X.number)
print(X().number)
if TYPE_CHECKING:
reveal_type(X.number_cls)
reveal_type(X().number_cls)
reveal_type(X.number)
reveal_type(X().number)
- Run it, you should see:
123
123
123
123
- Type check it with mypy and see the incorrect output.
Expected Behavior
I expected mypy to infer type of X.number_cls/X().number_cls correctly:
main.py:24: note: Revealed type is "builtins.int"
main.py:25: note: Revealed type is "builtins.int"
main.py:26: note: Revealed type is "builtins.int"
main.py:27: note: Revealed type is "builtins.int"
Note:
To simplify this example, __get__ returns an int no matter if instance is None or not but I assume a fix would also make it work properly with overloads as it does for instances of classes.
Actual Behavior
Mypy does not infer type of X.number_cls/X().number_cls correctly:
main.py:24: note: Revealed type is "def () -> __main__.IntDescriptorClass"
main.py:25: note: Revealed type is "def () -> __main__.IntDescriptorClass"
main.py:26: note: Revealed type is "builtins.int"
main.py:27: note: Revealed type is "builtins.int"
Your Environment
- Mypy version used: 0.910 as well as the latest (master branch)
- mypy-play.net URL: https://mypy-play.net/?mypy=latest&python=3.8&gist=de23d753f19cf0149ed9cd6919e84791
Additional notes
This works properly on pyright 1.1.161+, see the issue: https://github.com/microsoft/pyright/issues/2164
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 ejecutando la reproducción de Python proporcionada y comprobando con mypy los tipos revelados; usa la URL enlazada de mypy-play si resulta útil. Traza la inferencia de tipos de mypy para los descriptores definidos en metaclases y compárala con la de los descriptores de instancia ordinarios. Se considera terminado cuando las cuatro revelaciones del descriptor basado en clases informan builtins.int y el comportamiento existente de los descriptores sigue siendo correcto.
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
- Bien especificado
- Aptitud para principiantes
- 42/100