un/registerJsModule fails various expectations
- Dominant language
- Python
- Stars
- 14.8k
- Forks
- 1k
- Avg merge
- 20h 29m
- Merged PRs (30d)
- 19
Description
## 🐛 Bug
It is not clear nor transparent to users how both `registerJsModule` and `unregisterJsModule` work behind the scene and while I've found a workaround, I'd like to understand if the current behavior is actually acceptable to you ... please let me expand on that in the next section.
### To Reproduce
```js
// this throws errors if not previously defined which is OK
// pyodide.unregisterJsModule('test')
// this is fine
pyodide.registerJsModule('test', {a: 1});
// this is also fine *only if done before any import*
pyodide.registerJsModule('test', {a: 2});
// prints 2: correct!
pyodide.runPython('from test import a; print(a)');
// ...*BUT* ....
pyodide.registerJsModule('test', {a: 3});
// this *also prints 2* !!!
pyodide.runPython('from test import a; print(a)');
// so one might expect that doing this ...
pyodide.unregisterJsModule('test');
// would also drop it from the import cache so that this should throw
pyodide.runPython('from test import a; print(a)');
// instead of printing 2
// and also I could re-register it and be sure the value is the expected one
pyodide.registerJsModule('test', {a: 4});
// *nope* ... this will still print 2
pyodide.runPython('from test import a; print(a)');
// so that to actually really unregister a module one needs to:
pyodide.unregisterJsModule('test');
pyodide.runPython('import sys;del sys.modules["test"]');
// so now we talk !!!
pyodide.registerJsModule('test', {a: 5});
pyodide.runPython('from test import a; print(a)');
// 5 :partying_face:
```
### Expected behavior
* it's unexpected that overwriting a module has expected results if never imported but unexpected results if previously imported
* it's unexpected that, assuming the previous point is *pythonic* or a module cache expected behavior, once *unregistered* the module is still reachable as import in further code ... where has been *unregistered* if code can still use it and re-registring it won't ever change anything?
* I hence would expect some error thrown when a module is unregistered, *and* imported after, or a proper cleanup of the module cache or I would expect an error thrown if I re-register a module that has already been imported, otherwise there are too many silent moving parts hard to debug or understand (imho)
### Environment
- Pyodide Version: 0.23.4
- Browser version: any
- Any other relevant information: @hoodmane seems to agree there might be room for improvements as this is a JS exposed API, not something *pythonic* that just behave like native Python would ... and I hope others agree something is weird with the current behavior.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.