MagicStack / MagicStack/asyncpg

Typing: Correctly set types when getting `Record` fields

Abierto
#1,210 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Lenguaje dominante
Python
Estrellas
8.1k
Forks
468
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

Consider the following:

```python
import asyncio

import asyncpg

async def main() -> int:
connection_string = "postgresql://postgres:postgres@127.0.0.1:5432/postgres"
conn = await asyncpg.connect(connection_string)
result = await conn.fetch("SELECT 1 + 1 AS num", record_class=Row)
row = result[0]
num = row["num"]
print(type(num))
return num # mypy error: Returning Any from function declared to return "int"

class Row(asyncpg.Record):
num: int

if __name__ == "__main__":
asyncio.run(main())
```

In essence, the type of `num` is `Any` instead of `int`. I tried to make `Row` inherit from `TypedDict` or use a `dataclass`, both failed.
I'll also be happy to help develop this feature with some general guidance.

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

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

Start with the conn.fetch(..., record_class=Row) example and the asyncpg.Record field access shown in the issue. Investigate how the declared Row.num annotation could affect the inferred type of row["num"]. Done means the example's num is inferred as int and no longer produces mypy's Any return error.

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

Evaluación

Stack tecnológico
postgresql, python
Área
databases
Tipo de issue
Nueva funcionalidad
Dificultad
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.