`functools.partialmethod` loses return type and callable signature information

Ouverte
#16,499 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 1 jour

Personne n'a encore pris cette issue.

Évaluation

Difficulté
4/5
Temps estimé
3-5 jours
Accessibilité débutants
48/100
Type d'issue
Bug
Clarté
Plutôt claire
Activité
Active
Stack technique
python

Piste de recherche

Start by reading the existing functools.partialmethod stub in typeshed and compare its descriptor behavior with the examples in the issue. Check the relevant stub tests for callable and descriptor typing; the issue does not name a test file. Done means preserving return-type and descriptor-binding information as far as current typing support allows, while acknowledging that a fully precise residual signature needs typing-spec support.

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

Description

For code of this shape:

from functools import partialmethod

def pprint(arg: object, /, *, options: dict) -> str:
    return "dummy"

class Foo: ...
class Bar: ...

Bar.__repr__ = partialmethod(pprint, options={})

it is difficult for type checkers to verify that partialmethod is compatible with the target method since it is currently typed with only a return-type parameter:

class partialmethod(Generic[_T]):
      ...
      def __get__(self, obj: Any, cls: type[Any] | None = None) -> Callable[..., _T]: ...

The current stub does not preserve the wrapped callable’s parameters or receiver type, so type checkers cannot model these class and instance access forms even though this ebhaviour is valid at runtime, and partialmethod is a descriptor:

Bar.__repr__      # T: (self: Bar) -> str
Bar().__repr__    # T: () -> str

This forces Pyrefly and Pyright to reject this valid code. Mypy on the other hand, looks at stub’s descriptor fallback (Any) and infers partialmethod[Any] even when the wrapped callable returns str.

We should preserve descriptor binding and as much callable information as possible, while accepting that a fully precise residual signature needs typing-spec support.

Born from Pyrefly issue, https://github.com/facebook/pyrefly/issues/5138

Langage dominant
Python
Étoiles
5.1k
Forks
2.1k
Merge moyen
2 j 1 h
PR mergées (30 j)
115

Préparer son environnement

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/typeshed

Toutes les issues de python/typeshed

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.