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.

Unexpected behavior of `Some(None)`

See original GitHub issue

I expect the Some “constructor” (actually just a function) to only return Somes.

However, given None:

>>> from returns.maybe import Some
>>> Some(None)
<returns.maybe._Nothing object at 0x7f345dcaa1c8>

For reference, another language that has null pointers, Java, raises an exception on Optional.of(null).

In general, I expect the explicitly requested creation of a Some to fail if no actual value to wrap in it is supplied.

If one explicitly wants to let Maybe decide whether to create a Some or None given a thing, there still is Maybe.new (to which Some just delegates).

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
sobolevncommented, Apr 21, 2020

My idea was that you might sometime need to explicitly represent that real None exists somewhere. Think of this example:

d = {'a': 1, 'b': None}
md = maybe(d.get)

a = md('a')  # Some(1)
b = md('b')  # Some(None)
c = md('c')  # Nothing

We can see that ‘b’ and ‘c’ are different. And previously it was impossible to tell this difference. Now it is.

0reactions
sobolevncommented, May 17, 2020

Nope, this won’t do.

from returns.maybe import Some, Nothing, Maybe, maybe

source = {'a': 1, 'b': None}
def md(key: str) -> Maybe[int]:
    try:
        return Some(source[key])
    except KeyError:
        return Nothing

reveal_type(md('a'))  # ok: Revealed type is 'returns.maybe.Maybe[builtins.int]'
reveal_type(md('b').unwrap())  # not ok: Revealed type is 'builtins.int*'

Because in real life md('b').unwrap() would be None. And I don’t want to have Maybe[Optional[int]] this is really ugly.

Proposed solution: use Result when you care about value / None/ error things. Like so:

from returns.result import safe

source = {'a': 1, 'b': None}

@safe
def md(key: str) -> int:
    return source[key])

reveal_type(md('a'))  # Revealed type is 'returns.result.Result[builtins.int, Exception]'
reveal_type(md('b').unwrap())  # ok: Revealed type is 'builtins.int*'

We would have:

  • Success(1) or Success(None) for 'a' and 'b'
  • Failure(KeyError) for 'c'

We can always use result_to_maybe or maybe_to_result converters.

Read more comments on GitHub >

github_iconTop Results From Across the Web

cypher - Unexpected Behavior Chaining Any() and None() in Neo4j ...
What I'm trying to get is nodes having certain property values for any property name (key) and not having some other values for...
Read more >
Unexpected behavior with Option · Issue #263 · pureconfig ...
I'm seeing some inconsistent and unexpected behavior with Option. When a field is optional in a config class, it defaults to None, ...
Read more >
What are Expected and Unexpected Behaviours? - Twinkl
That is, there are expected and unexpected behaviours. ... Some children, including children with autism, thrive when classroom rules are clear and easily ......
Read more >
Unexpected Behavior with Conditional Logic - WordPress.org
I had to use conditional logic to determine whether or not to show certain species traits depending on which species gets selected, and...
Read more >
Expected And Unexpected Behaviors Teaching Resources | TPT
Teaching expected and unexpected behaviors throughout the school day is an essential part of any social emotional learning program.
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