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

Offen
#16,499 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Anfängerfreundlichkeit
48/100
Issue-Typ
Bug
Klarheit
Größtenteils klar
Aktivitätsstatus
Aktiv
Tech-Stack
python

Rechercherichtung

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.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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

Vorherrschende Sprache
Python
Sterne
5.1k
Forks
2.1k
Ø Merge
2 T. 7 Min.
Gemergte PRs (30 T.)
116

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus python/typeshed

Alle Issues in python/typeshed

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.