https://github.com/sayanarijit/nullability
Pass nullable arguments that may not exist
https://github.com/sayanarijit/nullability
Last synced: 3 months ago
JSON representation
Pass nullable arguments that may not exist
- Host: GitHub
- URL: https://github.com/sayanarijit/nullability
- Owner: sayanarijit
- License: mit
- Created: 2023-12-28T13:45:17.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-12-28T13:48:43.000Z (almost 2 years ago)
- Last Synced: 2025-02-09T05:30:04.159Z (8 months ago)
- Language: Python
- Size: 6.84 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# nullability
Pass nullable arguments that may not exist.
[](https://pypi.org/project/nullability)
### Usage
```python
>>> Nullable(1).value
1>>> print(Nullable(None).value)
None>>> Nullable.if_not_none(1)
Nullable(value=1)>>> print(Nullable.if_not_none(None))
None
```### Usecase example:
```python
>>> from dataclasses import dataclass
>>> from typing import Optional
>>> from nullability import Nullable>>> @dataclass
... class Foo:
... bar: Optional[int]
... baz: Optional[int]
...
... def update(
... self,
... bar: Optional[Nullable[int]]=None,
... baz: Optional[Nullable[int]]=None,
... ):
... if bar:
... self.bar = bar.value
... if baz:
... self.baz = baz.value
... return self>>> foo = Foo(bar=1, baz=2)
>>> foo.update(bar=Nullable(3))
Foo(bar=3, baz=2)>>> foo.update(baz=Nullable(None))
Foo(bar=3, baz=None)>>> foo.update(bar=Nullable.if_not_none(None))
Foo(bar=3, baz=None)>>> foo.update(bar=Nullable.if_not_none(4))
Foo(bar=4, baz=None)
```