Repository navigation
Description
This feature is crutial before 1.0 release.
Currently, our interfaces are not universal. Many functions and methods do require to explicitly annotate all containers that they can work with. As a result users cannot create their own containers.
And we struggle to build useful abstractions (like MonadIO on top of IO and Future, see https://github.com/gcanti/fp-ts/blob/master/src/MonadIO.ts). Let's see this example: https://returns.readthedocs.io/en/latest/pages/context.html#requirescontextfutureresult-container
It can possibly work with both IO and Future containers. In the first case, it would be sync. But, in the second one it would be async. And the source code won't be changed at all! We would only need to substitute our monad stack.
def _fetch_post(
post_id: int,
client_get: Callable[[str], MonadIO[Response]],
) -> MonadIO[_Post]:
return client_get(_URL.format(post_id)).bind_result(
safe(tap(httpx.Response.raise_for_status)),
).map(
lambda response: response.json(),
)Passing IOResult will run this code in sync, while FutureResult will run like real async code.
To make this happen, we need HKT!
I have spent a lot of time working on the initial implementation, but still no luck.
Related:
- Lightweight higher-kinded polymorphism: https://www.cl.cam.ac.uk/~jdy22/papers/lightweight-higher-kinded-polymorphism.pdf
- Swift: https://bow-swift.io/docs/fp-concepts/higher-kinded-types/
- TypeScript: https://github.com/gcanti/fp-ts/blob/master/docs/guides/HKT.md
Native blockers:
Activity
sobolevn commentedon Jun 18, 2020
That's exactly the same example I had in
How Async Should Have Been: https://dev.to/gcanti/functional-design-tagless-final-332ksobolevn commentedon Jun 18, 2020
Blocked by python/mypy#9001
Because I need a
TypeQueryinstance to findKind[]types in the function signature.antonagestam commentedon Aug 17, 2020
Just lurking on this PR which seems really promising 🙌 Has there been any interest from mypy to internalize support for this further on?
sobolevn commentedon Aug 17, 2020
@antonagestam thanks! 👍
When this will be released, I will work with mypy team! I am pretty sure that we can incorporate some parts of our API into the standard API.
sobolevn commentedon Aug 17, 2020
BTW, your https://github.com/antonagestam/phantom-types project looks amazing!
antonagestam commentedon Aug 17, 2020
@sobolevn Thanks! I'm finding it hard to find the time to get it off the ground but I'm currently making some changes to make it much more based around predicate functions which I think will make it a lot more useful and adaptable to different contexts :)
Exciting times in the world of typed Python!