Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add const Nothing instance. #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/maybe/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .maybe import (
Nothing,
NOTHING,
Some,
SomeNothing,
Maybe,
Expand All @@ -10,6 +11,7 @@

__all__ = [
"Nothing",
"NOTHING",
"Some",
"SomeNothing",
"Maybe",
Expand Down
1 change: 1 addition & 0 deletions src/maybe/maybe.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ def ok_or_else(self, op: Callable[[], E]) -> result.Err[E]:
"""
return result.Err(op())

NOTHING = Nothing()

# Define Maybe as a generic type alias for use in type annotations
Maybe: TypeAlias = Union[Some[T], Nothing]
Expand Down
8 changes: 7 additions & 1 deletion tests/test_maybe.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from typing import Callable

from maybe.maybe import NOTHING
import pytest
import result

Expand Down Expand Up @@ -29,7 +30,12 @@ def test_eq() -> None:
assert Some(1) != "abc"
assert Some("0") != Some(0)


def test_const_nothing() -> None:
assert NOTHING == Nothing()
assert NOTHING.is_nothing() is True
assert Some(1) != NOTHING
assert len({Some(1), NOTHING}) == 2

def test_hash() -> None:
assert len({Some(1), Nothing(), Some(1), Nothing()}) == 2
assert len({Some(1), Some(2)}) == 2
Expand Down