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

Aberta
#16,499 0 comentários 0 reações 0 responsáveis Ver no GitHub

Mantenedores costumam responder em até 1 dia

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
4/5
Tempo estimado
3-5 dias
Facilidade para iniciantes
48/100
Tipo de issue
Bug
Clareza
Razoavelmente clara
Status de atividade
Ativa
Stack de tecnologia
python

Direção de pesquisa

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.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

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

Linguagem predominante
Python
Estrelas
5.1k
Forks
2.1k
Merge médio
2d 1h
PRs com merge (30d)
115

Preparar o ambiente

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de python/typeshed

Todas as issues de python/typeshed

Issues semelhantes

Mais issues de Python

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.