question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Change design for improved type safety

See original GitHub issue

I’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:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
Jasonorocommented, Mar 6, 2020

Okay I figured out the problem. The typetest was still using if res1.is_ok() instead of the new way of using if 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.

1reaction
Jasonorocommented, Mar 5, 2020

Alright, I updated everything here. The good news is that all tests (That are still relevant) pass. The bad news is as follows:

$ mypy result/typetests.py
result\typetests.py:9:23: error: Cannot infer type argument 1 of "map_or" of "Err"
result\typetests.py:12:22: error: Cannot infer type argument 1 of "map_err" of "Ok"
result\typetests.py:12:45: error: Cannot infer type argument 1 of <list>
Found 3 errors in 1 file (checked 1 source file)

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found