python / python/mypy

Add typed attribute to third party class.

Abierto
#9,823 1 comentario 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

feature
Lenguaje dominante
Python
Estrellas
20.6k
Forks
3.3k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

Feature

I would like to be able to add a (typed) attribute to a third-party class instance.

To give a specific example, Flask's current_app, and Flask-AppBuilder. Some example code:

    def my_view(self):
        reveal_type(current_app.appbuilder)
        if current_app.appbuilder.sm.has_access(...):
            ...

The reveal_type here shows Any: airflow/www/views.py:850: note: Revealed type is 'Any'

Things I tried:

  1. To add a typed attribute myself:

    if TYPE_CHECKING:
        current_app.appbuilder = AppBuilder(None)
        reveal_type(current_app.appbuilder)
    

    Didn't work: airflow/www/views.py:115: note: Revealed type is 'Any'

  2. A partial type stub file

    I could use stubgen etc to create a stub file for all of flask and then edit the type in flask.globals, but this presents a maintenance burden that I don't want (keeping it in sync).

    I searched for any way I could to have a "partial" stub file -- if I could have a stubs/flask/globals.pyi with out neededing all the rest of the type stubs for this module, but couldn't get it working - likely not supported.

Pitch

I don't know what the feature would look like, but for the flask ecosystem it is fairly common to add properties on to the global flask object, or the g global variable, and I would love to be able to type-check my use of them, without needing to cast it at each use.

This issue is mostly a request for help/ "is there any sensible way of doing this?"

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

Empieza reproduciendo los resultados de reveal_type en airflow/www/views.py:850 y el intento con TYPE_CHECKING alrededor de la línea 115. Revisa cómo los tipos de Flask's flask.globals y stubgen gestionan los atributos de terceros. El trabajo estará terminado cuando el issue tenga un enfoque documentado y mantenible, o una funcionalidad del type-checker claramente delimitada para tipar dichos atributos sin un stub de reemplazo completo.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
flask, python
Área
tooling
Tipo de issue
Nueva funcionalidad
Dificultad
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Estancado
Claridad
Necesita aclaración
Aptitud para principiantes
25/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.