Support using Literal on __getattr__ and __getattribute__
Abierto
Nadie ha tomado este issue todavía.
feature
needs discussion
priority-1-normal
topic-literal-types
- Lenguaje dominante
- Python
- Estrellas
- 20.6k
- Forks
- 3.3k
- Métricas de merge de PR
- Métricas de PR pendientes
Descripción
Hi! Feature request for using Literal on __getattr__ and __getattribute__.
- Please insert below the code you are checking with mypy,
or a mock-up repro if the source is private. We would appreciate
if you try to simplify your case to a minimal repro.
from typing import Literal
LiteralStrHello = Literal["hello"]
LiteralStrWorld = Literal["world"]
class A:
def __getattr__(self, name: LiteralStrHello) -> LiteralStrWorld:
assert name == "hello"
return "world"
def __getattribute__(self, name: LiteralStrHello) -> LiteralStrWorld:
assert name == "hello"
return "world"
- What is the actual behavior/output?
main.py:8: error: Invalid signature "def (main.A, Literal['hello']) -> Literal['world']" for "__getattr__"
main.py:12: error: Invalid signature "def (main.A, Literal['hello']) -> Literal['world']" for "__getattribute__"
main.py:12: error: Argument 1 of "__getattribute__" is incompatible with supertype "object"; supertype defines the argument type as "str"
Found 3 errors in 1 file (checked 1 source file)
- What is the behavior/output you expect?
Success: no issues found in 1 source file
- What are the versions of mypy and Python you are using? Mypy:mypy 0.770+dev.5336f272c2ac625bd40a79ab3dbbe3d548f147b8, Python:3.8.0
Do you see the same issue after installing mypy from Git master? yes
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 proporcionada en main.py con mypy y compara los diagnósticos informados para getattr y getattribute. Rastrea los puntos de entrada de la comprobación de tipos para estos métodos especiales; se considera terminado cuando la reproducción produce “Success: no issues found in 1 source file” sin debilitar las comprobaciones ordinarias de object.getattribute.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- compilers, devtools
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 42/100