Visitar URL original
`functools.partialmethod` loses return type and callable signature information · Issue #16499 · python/typeshed · GitHub
Skip to content

functools.partialmethod loses return type and callable signature information #16499

Description

@dibrinsofor

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, facebook/pyrefly#5138

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions