python / python/mypy

Property return type not inferred when using `field_name = property(func)`

Abierto
#13,745 0 comentarios 2 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

bug
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 the field_name = property(func) syntax, the return type is not correctly inferred.
For descriptors however, that seems to work fine, so I assume this is a bug.
(Sorry if it has been reported before, I couldn't find anything related.)

To Reproduce

from __future__ import annotations
from typing import Any

def func1(self: Any) -> int:
    return 3

def func2(self: object) -> int:
    return 3

def func3(self: Test) -> int:
    return 3

class Test:
    def func4(self) -> int:
        return 3

    test1 = property(func1)
    test2 = property(func2)
    test3 = property(func3)
    test4 = property(func4)

reveal_type(Test().test1)
reveal_type(Test().test2)
reveal_type(Test().test3)
reveal_type(Test().test4)

# Output:
/tmp/test.py:27: note: Revealed type is "Any"
/tmp/test.py:28: note: Revealed type is "Any"
/tmp/test.py:29: note: Revealed type is "Any"
/tmp/test.py:30: note: Revealed type is "Any"
Success: no issues found in 1 source file

Expected Behavior

I'd expect mypy to understand the property return types.

It also seems that there is no way around this.
The following gives "Incompatible types in assignment":

class Test:
    test4: int = property(func4)

and we can't do property[int] as if it was a generic. (Shouldn't property be a generic in typeshed?)

Actual Behavior

We're getting Any instead.

Your Environment

  • Mypy version used: 0.981
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.9.13

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

El informe no menciona ningún archivo del repositorio ni ninguna prueba. Empieza ejecutando la reproducción de Python proporcionada con mypy 0.981 e inspecciona el manejo de property/descriptor y la definición de property en typeshed; se considera terminado cuando reveal_type informe del tipo de retorno int de cada property sin una asignación incompatible.

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
3/5
Tiempo estimado
1-2 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
45/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.