Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

249
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!