Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/MaT1g3R/option
Rust like Option and Result types in Python
https://github.com/MaT1g3R/option
mypy option-type optional pep484 result result-type typing
Last synced: 8 days ago
JSON representation
Rust like Option and Result types in Python
- Host: GitHub
- URL: https://github.com/MaT1g3R/option
- Owner: MaT1g3R
- License: mit
- Created: 2018-09-10T19:58:10.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-01-01T17:03:27.000Z (10 months ago)
- Last Synced: 2024-10-01T15:07:25.892Z (about 1 month ago)
- Topics: mypy, option-type, optional, pep484, result, result-type, typing
- Language: Python
- Homepage: https://mat1g3r.github.io/option/
- Size: 788 KB
- Stars: 83
- Watchers: 1
- Forks: 5
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-python-typing - option - Rust like Option and Result types. (Additional types)
README
# Option
[![CircleCI](https://circleci.com/gh/MaT1g3R/option/tree/master.svg?style=svg)](https://circleci.com/gh/MaT1g3R/option/tree/master)Rust-like [Option](https://doc.rust-lang.org/std/option/enum.Option.html) and [Result](https://doc.rust-lang.org/std/result/enum.Result.html) types in Python, slotted and fully typed.
An `Option` type represents an optional value, every `Option` is either `Some` and contains Some value, or `NONE`
A `Result` type represents a value that might be an error. Every `Result` is either `Ok` and contains a success value, or `Err` and contains an error value.
Using an `Option` type forces you to deal with `None` values in your code and increase type safety.
Using a `Result` type simplifies error handling and reduces `try` `except` blocks.
## Quick Start
```Python
from option import Result, Option, Ok, Err
from requests import getdef call_api(url, params) -> Result[dict, int]:
result = get(url, params)
code = result.status_code
if code == 200:
return Ok(result.json())
return Err(code)def calculate(url, params) -> Option[int]:
return call_api(url, params).ok().map(len)dict_len = calculate('https://example.com', {})
```## Install
Option can be installed from PyPi:
```bash
pip install option
```## Documentation
The documentation lives at https://mat1g3r.github.io/option/## License
MIT