Overloads with Generics "does not accept all possible arguments"

Ouverte
#19,053 1 commentaire 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Évaluation

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

Piste de recherche

Commencez par exécuter avec mypy l’exemple Python minimal de l’issue et comparez son résultat avec le comportement de Pyright indiqué par le lien. Suivez la vérification de compatibilité de l’implémentation de overload qui signale les signatures 1 et 2. Le travail est terminé lorsque l’exemple ne produit aucune erreur de compatibilité tout en conservant les résultats assert_type affichés.

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

Description

bug topic-overloads

I have a function whose return type is determined by the type of an input parameter (with a default value). I have attempted to type-hint it with generics. I get "error: Overloaded function implementation does not accept all possible arguments of signature".

To Reproduce

In the toy example below, the function has two return types (original-type or list) depending on two possible types of the force_list input (Force or NoForce). For completeness I have overloaded all 4 permutations of the input and output types.

Playground: https://mypy-play.net/?mypy=latest&python=3.12&gist=283290e33e2618dfcdf447513d0f4543

  from typing import TypeVar, assert_type, overload
  
  class Force: ...
  ForceInstance = Force()
  
  class NoForce: ...
  NoForceInstance = NoForce()
  
  T = TypeVar("T")
  
  @overload
  def list_or_orig(x: list[T], force_list: Force = ...) -> list[T]: ...
  
  @overload
  def list_or_orig(x: list[T], force_list: NoForce = ...) -> list[T]: ...
  
  @overload
  def list_or_orig(x: T, force_list: Force = ...) -> list[T]: ...
  
  @overload
  def list_or_orig(x: T, force_list: NoForce = ...) -> T: ...
  
  def list_or_orig(x: list[T] | T, force_list: Force | NoForce = ForceInstance) -> list[T] | T:
      """Return a list or scalar depending on force_list flag."""
      if isinstance(x, list):
          return x
      elif isinstance(force_list, Force):
          return [x]
      else:
          return x
  
  
  a_list = list_or_orig(1)  # Since default is Force, this will return list[int]
  assert_type(a_list, list[int])
  
  b_list = list_or_orig(1, ForceInstance)
  assert_type(b_list, list[int])
  
  a_int = list_or_orig(1, NoForceInstance)  # This will return int
  assert_type(a_int, int)

Expected Behavior

No errors when running through mypy.

Actual Behavior

main.py:29: error: Overloaded function implementation does not accept all possible arguments of signature 1  [misc]
main.py:29: error: Overloaded function implementation does not accept all possible arguments of signature 2  [misc]
Found 2 errors in 1 file (checked 1 source file)

Notes

If I replace the generic type T with a specific type, say, int then it's fine.

I appreciate there is an ambiguity between list[T] and T but have included the less-general list[T] first in the overloads.

The same code passes in the pyright playground.

Langage dominant
Python
Étoiles
20.6k
Forks
3.3k
Merge moyen
1 j 18 h
PR mergées (30 j)
54

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.

Autres issues de python/mypy

Toutes les issues de python/mypy

Issues similaires

Plus d'issues Python

Recevez les nouvelles issues par e-mail

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