Class inheriting from property doesn't typecheck properly when used as a decorator
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
I have a custom class inheriting from property. When I go to use this as a decorator though mypy seems to miss that it's a property at all. This is with Python 3.6.3 and mypy 0.650.
Example:
class CustomProperty(property):
def __init__(self, fget=None, fset=None, fdel=None, doc=None):
super().__init__(fget, fset, fdel, doc)
class A:
def __init__(self) -> None:
self._val = 1
@CustomProperty
def val(self) -> int:
return self._val
@val.setter
def val(self, v: int) -> None:
self._val = v
def test_a() -> None:
print(A.val)
Running mypy on this gives the following output:
mypy minimal2.py
minimal.py:14: error: Name 'val' already defined on line 10
minimal.py:14: error: Name 'val' is not defined
minimal.py:20: error: "Type[A]" has no attribute "val"
Contrast this with the same code with the builtin property which typechecks fine:
class B:
def __init__(self) -> None:
self._val = 1
@property
def val(self) -> int:
return self._val
@val.setter
def val(self, v: int) -> None:
self._val = v
def test_b() -> None:
print(B.val)
It's possible that this is related to #1529 where @ilevkivskyi suggested that:
The original example now passes mypy, not sure however how useful the subclass can be (property is heavily special-cased in mypy), but at least the original bug is fixed, so closing this.
Is subclassing property frowned upon (or just not supported in mypy?).
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
Reproduce el informe con el ejemplo minimal.py usando Python 3.6.3 y mypy 0.650; después, compara el caso del decorador CustomProperty con el caso de la propiedad builtin. Rastrea cómo mypy gestiona las subclases de property y los setters de los decoradores; el trabajo estará terminado cuando la clase personalizada pase la comprobación de tipos igual que la propiedad builtin, sin los errores duplicate-name o missing-attribute indicados.
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
- 45/100