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