python / python/mypy

type inferance from await asyncio.gather() with 6 elements no longer works

Ouverte
#17,030 2 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

bug
Langage dominant
Python
Étoiles
20.6k
Forks
3.3k
Métriques de merge des PR
Métriques de PR en attente

Description

Bug Report

mypy doesn't seem to properly infer types when unpacking the results of an asyncio.gather() that has at least 6 elements.

This leads to the unpacked variables getting assigned the object type, and then future uses of the variable cause typing issues.

To Reproduce

import asyncio
import datetime

async def get_str_value() -> str:
    await asyncio.sleep(1)

    return 'value'

async def get_datetime_value() -> datetime.datetime:
    await asyncio.sleep(2)

    return datetime.datetime.now()

async def run() -> None:

    (str1_value,
     str2_value,
     str3_value,
     datetime1_value,
     str4_value,
     str5_value,
     datetime2_value) = reveal_type(await asyncio.gather(get_str_value(),
                                                         get_str_value(),
                                                         get_str_value(),
                                                         get_datetime_value(),
                                                         get_str_value(),
                                                         get_str_value(),
                                                         get_datetime_value()))

    reveal_type(str1_value)
    reveal_type(datetime1_value)

    print(f"str={str1_value}, date={datetime1_value}")

asyncio.run(run())

Expected Behavior

mypy should be able to infer the appropriate type of the values from the gathered list. From searching, it looks like this might be a typeshed issue, or it might be something in mypy itself. It is unclear what can be done here.

Actual Behavior

mypy infers the return of the await as a list, and then assigns the object type to each of the variables. This results in cascading type errors as other code assumes those variables would be the appropriate type.

If you only have 5 values, then this correctly unpacks the tuple into the specific variables. But as soon as you gather 6 items it breaks.

asyncio-gather-test.py:21: note: Revealed type is "tuple[builtins.str, builtins.str, builtins.str, datetime.datetime, builtins.str, builtins.str]"
asyncio-gather-test.py:29: note: Revealed type is "builtins.str"
asyncio-gather-test.py:30: note: Revealed type is "datetime.datetime"

Your Environment

  • Mypy version used: 1.9, I also tried a development build
  • Mypy command-line flags: mypy
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.12.2

I'm really not sure what a good workaround in my own code would be here. For now I've taken to using asserts to check the types, which works ok, but is verbose.

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par reproduire l’exemple fourni de asyncio.gather, en comparant cinq arguments avec six ou plus et en vérifiant la sortie de reveal_type. Déterminez si la correction doit être apportée dans typeshed ou dans mypy, et considérez-la comme terminée lorsque les résultats hétérogènes conservent leurs types individuels pour l’appel signalé à sept éléments, sans erreurs en cascade.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
python
Domaine
devtools
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
45/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.