Change design for improved type safety
See original GitHub issueI’m proposing that the Result
class should be split into two classes class Ok(Generic[T])
and class Err(Generic[E])
and that Result
be only a type alias for Union[Ok[T], Err[T]]
This has the advantage that you can use isinstance
as a replacement for matching and gain improved type checkability.
For example, take this code
r: Result[int, str] = Ok(2)
if r.is_ok():
reveal_type(r.value) # returns Union[int, str]
There is an unnecessary ambiguity in the type of value
. You can use unwrap
, but if you have a bug in your code, it’s now a runtime error and a type checker can’t help you find it.
On the other hand, with unions you can do this:
r: Result[int, str] = Ok(2) # == Union[Ok[int], Err[str]]
if isinstance(r, Ok):
reveal_type(r.value) # returns int
I can make a PR showing what this would look like.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:6 (6 by maintainers)
Top Results From Across the Web
C++ Core Guidelines: Type Safety by Design
Type safety by design just means, that you always initialise your variables, use std::variant instead of a union, or prefer variadic templates ...
Read more >Type safety
In computer science, type safety and type soundness are the extent to which a programming language discourages or prevents type errors. Type safety...
Read more >Chapter 2 – Software Processes – Part 2
Change is inevitable in all large software projects. ... Business changes lead to new and changed system requirements ... Improved design quality.
Read more >Design Approvals | Federal Aviation Administration
Obtaining Design Approvals · Original Design Approval Process · Amended Type Certificates · Supplemental Type Certificates ( STC ) · Search ...
Read more >Apply themes
Apply a theme to quickly format an entire document and give it a modern, professional look. Select Design > Themes. Options for themes...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Okay I figured out the problem. The typetest was still using
if res1.is_ok()
instead of the new way of usingif isinstance(res1, Ok)
. With the new way the error doesn’t happen so that’s great! I’ll probably make a migration guide soon and submit a PR.Alright, I updated everything here. The good news is that all tests (That are still relevant) pass. The bad news is as follows:
This apparently is a bug in MyPy. However seeing as the issue is from 2013, this won’t be fixed soon.
There are workarounds but those are all manual fixes. The question for you is if you are fine with this issue, because it’s quite a breaking (and unwelcome) change in the use of the different
map
methods, even if it’s not our fault.I’ll write a migration guide and submit the PR once we’ve discussed above.