Skip to content

Commit c35f90d

Browse files
committed
New: Release 0.2.0 - Added nullsafe function / method calls
1 parent 5536f8a commit c35f90d

4 files changed

Lines changed: 51 additions & 4 deletions

File tree

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Proxy class for granting nullsafe abilities to an object.
5858

5959
No argument needed.
6060

61-
Nullish class with with nullsafe abilities. Instances will have a falsy boolean evaluation, equity comparison (`==`) to `None` and instance of `NullSafe` returns `True`, otherwise `False`. Identity comparison (`is`) to `None` will return `False`.
61+
Nullish class with with nullsafe abilities. Instances will have a falsy boolean evaluation, equity comparison (`==`) to `None` and instance of `NullSafe` returns `True`, otherwise `False`. Identity comparison (`is`) to `None` will return `False`. It also has a `__call__` method that always returns `undefined`.
6262

6363
### variable `undefined: NullSafe`
6464

@@ -114,6 +114,9 @@ assert _(o.existent).inexistent.nested is undefined
114114

115115
# o.maybe?.inexistent?.nested
116116
assert _(_(o).maybe).inexistent.nested is undefined
117+
118+
# o.inexistent?.inexistcall("anything").inexistent.nested().final
119+
assert _(o).inexistent.inexistcall("anything").inexistent.nested().final is undefined
117120
```
118121

119122
### Null safe item access
@@ -136,6 +139,9 @@ assert _(o["existent"])["inexistent"]["nested"] is undefined
136139

137140
# o.maybe?.inexistent?.nested
138141
assert _(_(o)["maybe"])["inexistent"]["nested"] is undefined
142+
143+
# o.inexistent?.inexistcall("anything").inexistent.nested().final
144+
assert _(o)["inexistent"]["inexistcall"]("anything")["inexistent"]["nested"]()["final"] is undefined
139145
```
140146

141147
### Null safe post evaluation
@@ -155,6 +161,9 @@ assert not _(o.nay).inexistent # bool(undefined) == False
155161

156162
# o.nay?.inexistent.nested
157163
assert _(o.nay).inexistent.nested is undefined
164+
165+
# o.nay?.inexistent().nested
166+
assert _(o.nay).inexistent().nested is undefined
158167
```
159168

160169
```python
@@ -163,11 +172,12 @@ o["nay"] = None
163172

164173
# o.nay?.inexistent
165174
assert _(o["nay"])["inexistent"] is undefined
166-
assert not _(o["nay"])["inexistent"]
167175

168176
# o.nay?.inexistent.nested
169177
assert _(o["nay"])["inexistent"]["nested"] is undefined
170-
assert not _(o["nay"])["inexistent"]["nested"]
178+
179+
# o.nay?.inexistent().nested
180+
assert _(o["nay"])["inexistent"]()["nested"] is undefined
171181
```
172182

173183
### Combined usage

nullsafe/core.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ def __eq__(self, o: object) -> bool:
2020
return True
2121
return False
2222

23+
def __call__(self, *args: Any, **kwds: Any) -> "NullSafe":
24+
return undefined
25+
2326
def __repr__(self) -> str:
2427
return "undefined"
2528

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
setup(
2323
name="nullsafe",
24-
version="0.1.2",
24+
version="0.2.0",
2525
author="Paaksing",
2626
author_email="paaksingtech@gmail.com",
2727
url="https://github.com/paaksing/nullsafe-python",

test/test_cases.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ class TestClassOne:
1010
subscriptable: dict
1111

1212

13+
class TestClassCallable:
14+
15+
existent: int = 123
16+
inexistent: str
17+
18+
def caller(self, some: str, arg: int):
19+
return f"{some}{arg}"
20+
21+
1322
class TestClassTwo:
1423

1524
existent: str
@@ -205,3 +214,28 @@ def test_null_combined():
205214
assert _(o.subscriptable)["inexistent"].existent is undefined
206215
assert _(o.subscriptable)["inexistent"].existent is undefined
207216
assert o.subscriptable["existent"].existent == 123
217+
218+
219+
def test_null_call():
220+
o = TestClassCallable()
221+
try:
222+
o.inexistent
223+
assert False
224+
except AttributeError as e:
225+
assert "inexistent" in str(e)
226+
assert _(o).inexistent() is undefined
227+
assert _(o).inexistent() == NullSafe()
228+
assert _(o).inexistent().inexistent is undefined
229+
assert _(o).inexistent.inexistent() is undefined
230+
assert _(o).inexistent.inexistent["dsrghsuiorgh"]() is undefined
231+
assert _(o).inexistent("douihgrf", 35).inexistent["dsrghsuiorgh"]() is undefined
232+
assert _(o).caller("some", 2) == "some2"
233+
234+
o = {"caller": TestClassCallable.caller}
235+
assert _(o)["inexistent"]() is undefined
236+
assert _(o)["inexistent"]() == NullSafe()
237+
assert _(o)["inexistent"]().inexistent is undefined
238+
assert _(o)["inexistent"]["inexistent"]() is undefined
239+
assert _(o)["inexistent"]["inexistent"]["dsrghsuiorgh"]() is undefined
240+
assert _(o)["inexistent"]("douihgrf", 35)["inexistent"]["dsrghsuiorgh"]() is undefined
241+
assert _(o)["caller"]({"fakeself"}, "some", 2) == "some2"

0 commit comments

Comments
 (0)