https://github.com/miyamo2/py_nullable
Python's None Wrapper, inspired by java.util.Optional
https://github.com/miyamo2/py_nullable
null-safety nullable python python3
Last synced: about 1 year ago
JSON representation
Python's None Wrapper, inspired by java.util.Optional
- Host: GitHub
- URL: https://github.com/miyamo2/py_nullable
- Owner: miyamo2
- License: mit
- Created: 2023-02-01T15:26:22.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-07-10T06:52:55.000Z (over 1 year ago)
- Last Synced: 2025-03-14T23:37:46.395Z (about 1 year ago)
- Topics: null-safety, nullable, python, python3
- Language: Python
- Homepage: https://pypi.org/project/py-nullable/
- Size: 2.74 MB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# py_nullable
[](https://img.shields.io/pypi/pyversions/py_nullable?logo=python&logoColor=yellow)
[](https://pypi.org/project/py-nullable?latest)
[](https://img.shields.io/github/v/release/miyamo2theppl/py_nullable)
[](https://img.shields.io/pypi/wheel/py-nullable)
[](https://github.com/miyamo2theppl/py_nullable/actions?query=workflow%3Arelease)
[](https://github.com/miyamo2theppl/py_nullable/actions?query=workflow%3Atest)
[](https://img.shields.io/github/license/miyamo2theppl/py_nullable)
[](https://pepy.tech/project/py-nullable)
## Introduction
Python's None Wrapper, inspired by java.util.Optional
[Document(latest version only)](https://miyamo2theppl.github.io/py_nullable/)
## Getting Started
### Installing
install from PyPI with:
```sh
python -m pip install py_nullable
```
install from source with:
```sh
git clone https://github.com/miyamo2theppl/py_nullable.git
cd py_nullable
python -m pip install .
```
### Simple Usage
if you want to get value.
```python
from py_nullable import Nullable
nullable: Nullable[str] = Nullable[str]("some string")
if nullable.isPresent():
print(nullable.get()) # Prints some string
```
if you want to generate a new Nullable from an existing Nullable with the mapping function.
```python
from typing import Callable
from py_nullable import Nullable
nullable: Nullable[str] = Nullable[str]("1234")
callback: Callable[[str], int] = lambda x: int(x) * 2
result: Nullable[int] = nullable.map(callback)
print(result.get()) # Prints 2468
```
if you want to refactor the return value from Optional[T] to Nullable[T].
```python
from yourpackage import YourClass
from py_nullable import Nullable, nullable_wrap
in_memory_db: dict[str, YourClass] = {"A001": YourClass("foo")}
@nullable_wrap
def find_by_id(id: str) -> Optional[YourClass]:
return in_memory_db.get(id)
nullable: Nullable[YourClass] = find_by_id("B001")
print(nullable.isEmpty()) # Prints True
```
## Contributing
### Create a feature branch
```sh
git checkout -b feature_{example}
```
### Set up your environment
```sh
python -m venv .venv
.venv/bin/activate
pip install -r dev_requirements.txt
pip install -e .
```
### Running Tests
```sh
pytest --cov py_nullable --cov-branch --cov-report=html
```