Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

374
Visualizações
Is it possible to properly type hint the filterM function in Python?

I'm currently self-studying functional programming by writing a monad library in python. And I'm having trouble with type hinting. So for example, there is a function filterM in Haskell with signature

filterM :: (a -> m Bool) -> [a] -> m [a]

Ideally, if python can pattern match "subtypes" of a TypeVar by putting a bracket after it, then I should be able to do it with something like this:

T = TypeVar('T')
M = TypeVar('M', bound=Monad)
def filterM(filter_func: Callable[[T], M[bool]], iterable: list[T]) -> M[list[T]]

But it seems that the above syntax wouldn't work. In fact, it seems like there is no way to "extract" the type of monad I pass in at all. Say I pass in a Callable[[int], Maybe[bool]], the best I achieved was to take the whole Maybe[bool] as a single TypeVar. Then there is no way to convert it to the correct output type Maybe[list[int]].

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Currently, what you want cannot be done. You'll have to make a plan that doesn't require it.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda