@@ -16,6 +16,7 @@ Our `pyscript.ffi` offers the following utilities:
16
16
counterpart.
17
17
* ` ffi.create_proxy(def_or_lambda) ` proxies a generic Python function into a
18
18
JavaScript one, without destroying its reference right away.
19
+ * ` ffi.is_none(reference) ` to check if a specific value is either ` None ` or ` JsNull `
19
20
20
21
Should you require access to Pyodide or MicroPython's specific version of the
21
22
FFI you'll find them under the ` pyodide.ffi ` and ` micropython.ffi ` namespaces.
@@ -209,3 +210,49 @@ only in Pyodide can we then destroy such proxy:
209
210
js .setTimeout (proxy, 1000 , " success" );
210
211
</script >
211
212
```
213
+
214
+ ## is_none
215
+
216
+ Starting from * Pyodide* version ` 0.28 ` , there is a new * nullish* value in town that is
217
+ meant to represent exactly the * JS* ` null ` value.
218
+
219
+ Previously, both ` null ` and ` undefined ` would have been converted into ` None `
220
+ but some * API* behaves differently if one value is absent or explicitly ` null ` .
221
+
222
+ In * JSON* , ` null ` would also survive serialization while ` undefined ` would
223
+ vanish. To preserve that distinction also in * Python* , the conversion
224
+ between * JS* and * Python* now has a new ` pyodide.ffi.jsnull ` as showed in
225
+ [ pyodide documentation] ( https://pyodide.org/en/stable/usage/type-conversions.html#javascript-to-python ) .
226
+
227
+ In general, there should be no surprise but specially when dealing with the * DOM*
228
+ world, most utilities and methods would return ` null ` .
229
+
230
+ To simplify and smooth-out this distinction, we decided to introduce ` is_null `
231
+ as [ demoed live in here] ( https://pyscript.com/@agiammarchi/pyscript-ffi-is-none/latest?files=main.py ) :
232
+
233
+ ``` html title="pyscript.ffi.is_none"
234
+ <!-- success in both Pyodide and MicroPython -->
235
+ <script type =" py" >
236
+ from pyscript .ffi import is_none
237
+ import js
238
+
239
+ js_undefined = js .undefined
240
+ js_null = js .document .body .getAttribute (" nope" )
241
+
242
+ print (js_undefined is None) # True
243
+ print (js_null) # jsnull
244
+ print (js_null is None) # False
245
+
246
+ # JsNull is still " falsy" as value
247
+ if (js_null):
248
+ print (" this will not be shown" )
249
+
250
+ # safely compared against both
251
+ print (is_none (js_undefined)) # True
252
+ print (is_none (js_none)) # True
253
+ </script >
254
+ ```
255
+
256
+ Please note that in * MicroPython* the method works the same but, as we try to
257
+ reach features-parity among runtimes, it is suggested to use ` is_none(ref) `
258
+ even if right now there is not such distinction between ` null ` and ` undefined ` .
0 commit comments