python / python/mypy

Inconsistent behavior w.r.t missing builtins

Abierto
#14,547 3 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

bug topic-developer topic-tests
Lenguaje dominante
Python
Estrellas
20.6k
Forks
3.3k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

Issue

Some of the builtins fixtures are sparse to the point that they're missing even the declaration of dict, list, tuple etc. types. The austerity is justified by improved tests performance, and it's understood that specific tests can specify a broader fixture without slowing down other tests.

However, some core features rely on the presence of those types, e.g.

  • functions' **kwargs
  • modules' __annotations__
  • namedtuple's attributes like _field_types

I've observed 3 different strategies used to handle the types absence:

I think we should recommend and, ideally, consolidate on one strategy.

Motivation

Case in point: Recently while implementing #14526, I had to "play" a whack-a-mole with unrelated tests breaking. I've used **kw which required me to add dict to a number of builtins fixtures, which in turn caused __annotations__ to materialize where they didn't before, and failed some other tests.

I think ideally we shouldn't have this whack-a-mole, and tests should be less brittle.

Recommendation

Which strategy to adopt? While it's good practice to structure code to be more testable, I think it's normally discouraged to have code paths that are only due to testing. Since those builtins should universally exist in "production", I think crash should be our strategy.

Since code typically relies only on the presence of those classes (not their methods), we can put in place a rule (validated by tests) that any builtins fixture must define a baseline of types, even if empty. This shouldn't affect performance greatly, and provide a comfortable baseline.

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

Comienza comparando el manejo de los builtins faltantes en mypy/types.py, mypy/semanal.py y mypy/semanal_namedtuple.py, junto con los fixtures y tests de builtins afectados. Determina una estrategia coherente para las declaraciones de builtins requeridas y actualiza los tests relevantes para que el comportamiento de los fixtures sea estable con **kwargs, annotations y el manejo de NamedTuple.

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

Evaluación

Stack tecnológico
python
Área
compilers
Tipo de issue
Refactorización
Dificultad
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Estancado
Claridad
Necesita aclaración
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.