python / python/mypy

Per type instance metadata

Abierto
#8,843 1 comentario 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

question topic-plugins
Lenguaje dominante
Python
Estrellas
20.6k
Forks
3.3k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

I've been working on pynamodb-mypy (started a while ago and coming back to it now...). The first task I took up is to implement attribute optionality, i.e.

from pynamodb.models import Model
from pynamodb.attributes import NumberAttribute

class MyModel(Model):
  my_number = NumberAttribute()
  my_optional_number = NumberAttribute(null=True)

reveal_type(MyModel().my_number)  # note: Revealed type is 'int'
reveal_type(MyModel().my_optional_number)  # note: Revealed type is 'Optional[int]'

I've been implementing:

  • get_function_hook to handle NumberAttribute initialization and parsing the 'null' parameter
  • get_attribute_hook to wrap the return type in optionality as needed

This required storing state between the function hook (parsing the arguments) and the attribute hook. One approach I've successfully tried is to append a "sentinel" type arg to the instance's args, i.e. from NumberAttribute it turns into NumberAttribute[None], and generally from SomeAttribute[...] to SomeAttribute[..., None] (I'm using a NoneType as the sentinel). Then in get_attribute_hook I'm looking for the sentinel.

You can see my current implementation here.

I was wondering what would be your thoughts on storing extra metadata on a type instance. Type args appear to be the only sanctioned "extras" as they're effectively part of the type. The new Annotated comes to mind, e.g. have my function hook return something like Annotated[AttrT, 'nullable_attr'], but I got the impression that it doesn't quite work this way -- that Annotateds should be converted to something else during semantic analysis rather than keeping them around for later stages to use.

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 leyendo pynamodb_mypy/plugin.py, especialmente get_function_hook y get_attribute_hook, y después revisa cómo mypy gestiona los argumentos de tipo y Annotated durante el análisis semántico. El issue solo estará completo cuando los maintainers decidan y documenten una forma compatible de transmitir metadatos por instancia para la opcionalidad; no se han proporcionado criterios de aceptación concretos.

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
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Estancado
Claridad
Necesita aclaración
Aptitud para principiantes
18/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.