You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+18-18Lines changed: 18 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,7 +17,7 @@ class Dummy:
17
17
pass
18
18
```
19
19
20
-
Normal Python code
20
+
Normal Python code:
21
21
22
22
```python
23
23
o = Dummy()
@@ -42,29 +42,29 @@ if value is not undefined:
42
42
print("accessed")
43
43
```
44
44
45
-
##Documentation
45
+
# Documentation
46
46
47
-
###Basics
47
+
## Basics
48
48
49
49
There are 5 values importable in nullsafe root:
50
50
51
-
####class `NullSafeProxy: (o: T)`
51
+
### class `NullSafeProxy: (o: T)`
52
52
53
53
Receives an object `o` on instantiation.
54
54
55
55
Proxy class for granting nullsafe abilities to an object.
56
56
57
-
####class `NullSafe: ()`
57
+
### class `NullSafe: ()`
58
58
59
59
No argument needed.
60
60
61
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`.
62
62
63
-
####variable `undefined: NullSafe`
63
+
### variable `undefined: NullSafe`
64
64
65
65
Instance of `Nullsafe`, this instance will be returned for all nullish access in a proxied object, enabling identity comparison `value is undefined` for code clarity.
66
66
67
-
####function `nullsafe: (o: T) -> T`
67
+
### function `nullsafe: (o: T) -> T`
68
68
69
69
Receives an object `o` as argument.
70
70
@@ -74,27 +74,27 @@ return `undefined` if `o` is `None` or `undefined`, otherwise return the proxied
74
74
75
75
This function is **generic typed** (`(o: T) -> T`), code autocompletions and linters functionalities will remain. Disclaimer: If the object was not typed before proxy, it obviously won't come out typed out of the blue.
76
76
77
-
####function `_: (o: T) -> T` (alias to `nullsafe`)
77
+
### function `_: (o: T) -> T` (alias to `nullsafe`)
78
78
79
-
Alias to nullsafe, used for better code clarity.
79
+
Alias to `nullsafe`, used for better code clarity.
80
80
81
81
The examples shown will be using `_` instead of `nullsafe` for code clarity. For better understanding, the Javascript equivalents will be shown as comments.
82
82
83
-
###Implementation
83
+
## Implementation
84
84
85
85
Nullsafe abilities are granted after proxying an object through `NullSafeProxy`. To proxy an object pass it through `_()` or `nullsafe()`. Due to language limitation, the implementation does not follow the "return the first nullish value in chain", instead it "extend the a custom nullish value until the end of chain". Inexistent values of a proxied object and its subsequent values in chain will return `undefined`.
86
86
87
-
###Import
87
+
## Import
88
88
89
89
```python
90
90
from nullsafe import undefined, _
91
91
```
92
92
93
-
###Usage
93
+
## Usage
94
94
95
95
There are various way to get a nullsafe proxied object.
96
96
97
-
####Null safe attribute access
97
+
### Null safe attribute access
98
98
99
99
Proxied object doing a possibly `AttributeError` access.
100
100
@@ -138,7 +138,7 @@ assert _(o["existent"])["inexistent"]["nested"] is undefined
138
138
assert _(_(o)["maybe"])["inexistent"]["nested"] is undefined
139
139
```
140
140
141
-
####Null safe post evaluation
141
+
### Null safe post evaluation
142
142
143
143
Possibly `None` or `undefined` object doing possibly `AttributeError` or `KeyError` access.
144
144
@@ -170,19 +170,19 @@ assert _(o["nay"])["inexistent"]["nested"] is undefined
170
170
assertnot _(o["nay"])["inexistent"]["nested"]
171
171
```
172
172
173
-
####Combined usage
173
+
### Combined usage
174
174
175
175
Of course you can combine different styles.
176
176
177
177
```python
178
178
assert _(o).inexistent["inexistent"].inexistent.inexistent["inexistent"]["inexistent"] is undefined
179
179
```
180
180
181
-
###Limitations
181
+
## Limitations
182
182
183
183
List of limitations that you may encounter.
184
184
185
-
####`undefined` behavior
185
+
### `undefined` behavior
186
186
187
187
`undefined` is actually an instance of `NullSafe`, the actual mechanism used for nullsafe chaining, it cannot self rip the nullsafe functionality when the chain ends (because it doesn't know), so this following actually possible and probably not the wanted behavior.
188
188
@@ -192,7 +192,7 @@ val = _(o).inexistent
192
192
assert val.another_inexistent is undefined
193
193
```
194
194
195
-
####Post evaluation
195
+
### Post evaluation
196
196
197
197
In other languages like Javascript, it checks for each item in the chain and return `undefined` on the first nullish value, which in fact is post-evaluated. This is not possible in python because it raises an `AttributeError` or `KeyError` on access attempt, unless it returns `None` (see [one of the available usage](#null-safe-post-evaluation)), so it must proxy the instance that may contain the attr or key before accessing.
0 commit comments