Skip to main content

Pass nullable arguments that may not exist

Project description

nullability

Pass nullable arguments that may not exist.

PyPI version

Usage

>>> 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:

>>> 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)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nullability-0.1.1.tar.gz (4.4 kB view hashes)

Uploaded Source

Built Distribution

nullability-0.1.1-py3-none-any.whl (3.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page