8000 Introduce DeprecatedGlobal and show proper warnings (#1014) · DindyalM/pyscriptErrors@e8318a9 · GitHub
[go: up one dir, main page]

Skip to content

Commit e8318a9

Browse files
authored
Introduce DeprecatedGlobal and show proper warnings (pyscript#1014)
* kill the PyScript class and the weird pyscript instance; from the user point of view its functionalities are still available as pyscript.*, but pyscript is not the module, not the instance of PyScript * simplify the code in _set_version_info, while I'm at it * start to implement DeprecatedGlobal * DeprecatedGlobal.__getattr__ * don't show the same warning twice * DeprecatedGlobal.__call__ * make it possible to specify a different warning message for every global * WIP: carefully use DeprecatedGlobal to show reasonable warning messages depending on which name you are accessing to. More names to follow * deprecate more names * deprecate private names * depreacte direct usage of console and document * deprecate the PyScript class * use a better error message * fix test_pyscript.py * introduce a __repr__ for DeprecatedGlobal * add an helper to ensure that we don't show any error or warning on the page * WIP: ensure that examples don't use depreacted features. Many tests are failing * don't deprecate Element * don't use the global micropip to install packages, else we trigger a warning * use a better error message for micropip * fix test_todo_pylist to avoid using deprecated globals * fix test_webgl_raycaster * fix tests * make HTML globally available * add MIME_RENDERERS and MIME_METHODS * fix the typing of Micropip, thanks to @FabioRosado
1 parent 94f2ac6 commit e8318a9

File tree

12 files changed

+407
-115
lines changed

12 files changed

+407
-115
lines changed

examples/pylist.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
from datetime import datetime as dt
22

3+
import pyscript
34

4-
class PyItem(PyItemTemplate):
5+
6+
class PyItem(pyscript.PyItemTemplate):
57
def on_click(self, evt=None):
68
self.data["done"] = not self.data["done"]
79
self.strike(self.data["done"])
810

911
self.select("input").element.checked = self.data["done"]
1012

1113

12-
class PyList(PyListTemplate):
14+
class PyList(pyscript.PyListTemplate):
1315
item_class = PyItem
1416

1517
def add(self, item):

examples/todo-pylist.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,17 @@ <h1>To Do List</h1>
124124
<p>pylist.py</p>
125125
<pre class="prism-code language-python">
126126
<code class="language-python">
127+
import pyscript
127128
from datetime import datetime as dt
128129

129-
class PyItem(PyItemTemplate):
130+
class PyItem(pyscript.PyItemTemplate):
130131
def on_click(self, evt=None):
131132
self.data["done"] = not self.data["done"]
132133
self.strike(self.data["done"])
133134

134135
self.select("input").element.checked = self.data["done"]
135136

136-
class PyList(PyListTemplate):
137+
class PyList(pyscript.PyListTemplate):
137138
item_class = PyItem
138139

139140
def add(self, item):

examples/webgl/raycaster/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from js import THREE
3232
from js import performance
3333
from js import Object
34+
from js import document
3435
import asyncio
3536

3637

pyscriptjs/src/main.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -210,22 +210,21 @@ export class PyScriptApp {
210210
//Refresh the module cache so Python consistently finds pyscript module
211211
runtime.invalidate_module_path_cache()
212212

213-
// inject `define_custom_element` it into the PyScript module scope
213+
// inject `define_custom_element` and showWarning it into the PyScript
214+
// module scope
214215
const pyscript_module = runtime.interpreter.pyimport('pyscript');
215216
pyscript_module.define_custom_element = define_custom_element;
216-
pyscript_module.PyScript.set_version_info(version);
217+
pyscript_module.showWarning = showWarning;
218+
pyscript_module._set_version_info(version);
217219
pyscript_module.destroy();
218220

219-
// TODO: Currently adding the imports for backwards compatibility, we should
220-
// remove it
221+
// import some carefully selected names into the global namespace
221222
await runtime.run(`
222-
from pyscript import *
223-
`);
224-
logger.warn(`DEPRECATION WARNING: 'micropip', 'Element', 'console', 'document' and several other \
225-
objects form the pyscript module (with the exception of 'display') will be \
226-
be removed from the Python global namespace in the following release. \
227-
To avoid errors in future releases use import from pyscript instead. For instance: \
228-
from pyscript import micropip, Element, console, document`);
223+
import js
224+
import pyscript
225+
from pyscript import Element, display, HTML
226+
pyscript._install_deprecated_globals_2022_12_1(globals())
227+
`)
229228

230229
if (this.config.packages) {
231230
logger.info('Packages to install: ', this.config.packages);

pyscriptjs/src/pyexec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ import type { Runtime } from './runtime';
66
const logger = getLogger('pyexec');
77

88
export function pyExec(runtime: Runtime, pysrc: string, outElem: HTMLElement) {
9-
// this is the python function defined in pyscript.py
10-
const set_current_display_target = runtime.globals.get('set_current_display_target');
9+
//This is pyscript.py
10+
const pyscript_py = runtime.interpreter.pyimport('pyscript');
11+
1112
ensureUniqueId(outElem);
12-
set_current_display_target(outElem.id);
13-
//This is the python function defined in pyscript.py
14-
const usesTopLevelAwait = runtime.globals.get('uses_top_level_await');
13+
pyscript_py.set_current_display_target(outElem.id);
1514
try {
1615
try {
17-
if (usesTopLevelAwait(pysrc)) {
16+
if (pyscript_py.uses_top_level_await(pysrc)) {
1817
throw new UserError(
1918
ErrorCode.TOP_LEVEL_AWAIT,
2019
'The use of top-level "await", "async for", and ' +
@@ -33,7 +32,8 @@ export function pyExec(runtime: Runtime, pysrc: string, outElem: HTMLElement) {
3332
displayPyException(err, outElem);
3433
}
3534
} finally {
36-
set_current_display_target(undefined);
35+
pyscript_py.set_current_display_target(undefined);
36+
pyscript_py.destroy();
3737
}
3838
}
3939

pyscriptjs/src/pyodide.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ declare const loadPyodide: typeof loadPyodideDeclaration;
1010

1111
const logger = getLogger('pyscript/pyodide');
1212

13-
interface Micropip {
13+
interface Micropip extends PyProxy {
1414
install: (packageName: string | string[]) => Promise<void>;
1515
destroy: () => void;
1616
}
@@ -91,7 +91,8 @@ export class PyodideRuntime extends Runtime {
9191
async installPackage(package_name: string | string[]): Promise<void> {
9292
if (package_name.length > 0) {
9393
logger.info(`micropip install ${package_name.toString()}`);
94-
const micropip = this.globals.get('micropip') as Micropip;
94+
95+
const micropip = this.interpreter.pyimport('micropip') as Micropip;
9596
try {
9697
await micropip.install(package_name);
9798
micropip.destroy();

0 commit comments

Comments
 (0)
0