A simple Maybe (Option) type for Python 3 inspired by Rust, fully type annotated.
Latest release:
pip install rustedpy-maybe
Latest GitHub master
branch version:
pip install git+https://github.com/rustedpy/maybe
There are no dependencies outside of the Python standard library. However, if
you wish to use the Result
conversion methods (see examples in the next
section), you will need to install the result
extra.
In this case, rather than installing via one of the commands above, you can
install the package with the result
extra either from the latest release:
pip install rustedpy-maybe[result]
or from the GitHub master
branch:
pip install git+https://github.com/rustedpy/maybe[result]
Experimental. API subject to change.
The idea is that a possible value can be either Some(value)
or Nothing()
,
with a way to differentiate between the two. Some
and Nothing
are both
classes encapsulating a possible value.
Example usage:
from maybe import Nothing, Some
o = Some('yay')
n = Nothing()
assert o.unwrap_or_else(str.upper) == 'yay'
assert n.unwrap_or_else(lambda: 'default') == 'default'
There are some methods that support conversion from a Maybe
to a Result
type
in the result library. If you wish to
leverage these methods, you must install the result
extra as described in the
installation section.
Example usage:
from maybe import Nothing, Some
from result import Ok, Err
o = Some('yay')
n = Nothing()
assert o.ok_or('error') == Ok('yay')
assert o.ok_or_else(lambda: 'error') == Ok('yay')
assert n.ok_or('error') == Err('error')
assert n.ok_or_else(lambda: 'error') == Err('error')
These steps should work on any Unix-based system (Linux, macOS, etc) with Python
and make
installed. On Windows, you will need to refer to the Python
documentation (linked below) and reference the Makefile
for commands to run
from the non-unix shell you're using on Windows.
- Setup and activate a virtual environment. See Python docs for more information about virtual environments and setup.
- Run
make install
to install dependencies - Switch to a new git branch and make your changes
- Test your changes:
make test
make lint
- You can also start a Python REPL and import
maybe
- Update documentation
- Edit any relevant docstrings, markdown files
- Run
make docs
- Add an entry to the changelog
- Git commit all your changes and create a new PR.
MIT License