5 ms·
Hi, I'm the author! Happy to answer your questions here!
by karlicoss 7y ago
Hi, I'm the author! Happy to answer your questions here!
- dbrgn 7y agoHi, thanks for featuring my result library! Regarding your criticism that `result.ok()` returns `None` if the value is not an `Ok` type: That's what `result.unwrap()` and `result.expect(msg)` are for :) (There's no unwrap_err and expect_err so far, but PRs are welcome.) The lib is strongly inspired by Rust (see https://doc.rust-lang.org/std/result/enum.Result.html https://doc.rust-lang.org/std/result/enum.Result.html), that's why `result.ok()` returns something option-ish. I like your `isinstance` approach btw! Will have to think about it a bit more. Reminds me a bit of Typescript as well. Does mypy have type guards (https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-differentiating-types https://www.typescriptlang.org/docs/handbook/advanced-types....)? Maybe that could be used to create helper functions that check for success/failure and which help mypy to derive the correct type.
- uryga 7y ago> Does mypy have type guards? not yet, maybe in the future. might be possible to emulate some use cases with Literal Types. tracking issue: https://github.com/python/mypy/issues/5206 https://github.com/python/mypy/issues/5206
- karlicoss 7y agoOh, not sure how I missed these methods, thanks! I'll update the post to reflect it.
- Agathos 7y agoThanks for writing it! I'm a Python programmer in my day job. I have sometimes remarked that the reason I play around with Haskell in my free time is because I don't want to see another run time type error unless I'm being paid to do so. But I needed to do some spreadsheet-scraping and data-munging for a personal project and I figured it would be easier in Python. So I searched and found your article and it was just what I needed. Now to sneak it in at work...
- karlicoss 7y agoI'm a fan of cool type systems and Haskell in particular too. Also doing lots of messing with data for personal stuff (e.g. https://beepb00p.xyz/mypkg.html#examples https://beepb00p.xyz/mypkg.html#examples). But yep, at least for me, the reality is if I need some quick yet useful code, Python happens to get me there very fast, just because of the sheer amount of code people already have written in Python.
- mplanchard 7y agoI’ve written a similar python library called `safetywrap` that implements both Result and Option types from rust, with almost all of the interfaces implemented, including unwrap, map, flat map, err_map, etc. It fully supports types and works well with mypy. Feel free to give it a shot! https://pypi.org/project/safetywrap/ https://pypi.org/project/safetywrap/
- dbrgn 7y agoAt first glance, this looks really solid! Maybe I can deprecate my result library now :D