Introduce DeprecatedGlobal and show proper warnings (#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
This commit is contained in:
Antonio Cuni
2022-12-06 14:31:57 +01:00
committed by GitHub
parent 94f2ac6204
commit e8318a98f0
12 changed files with 407 additions and 115 deletions

View File

@@ -214,8 +214,8 @@ class TestBasic(PyScriptTest):
"""
<py-script>
import js
js.console.log(PyScript.__version__)
js.console.log(str(PyScript.version_info))
js.console.log(pyscript.__version__)
js.console.log(str(pyscript.version_info))
</py-script>
"""
)
@@ -232,27 +232,47 @@ class TestBasic(PyScriptTest):
is not None
)
def test_python_modules_deprecated(self):
# GIVEN a py-script tag
def test_assert_no_banners(self):
"""
Test that the DOM doesn't contain error/warning banners
"""
self.pyscript_run(
"""
<py-script>
print('hello pyscript')
raise Exception('this is an error')
import pyscript
pyscript.showWarning("hello")
pyscript.showWarning("world")
</py-script>
"""
"""
)
# TODO: Adding a quick check that the deprecation warning is logged. Not spending
# to much time to make it perfect since we'll remove this right after the
# release. (Anyone wanting to improve it, please feel free to)
warning_msg = (
"[pyscript/main] DEPRECATION WARNING: 'micropip', 'Element', 'console', 'document' "
"and several other objects form the pyscript module (with the exception of 'display') "
"will be be removed from the Python global namespace in the following release. "
"To avoid errors in future releases use import from pyscript "
"instead. For instance: from pyscript import micropip, Element, "
"console, document"
with pytest.raises(AssertionError, match="Found 2 alert banners"):
self.assert_no_banners()
def test_deprecated_globals(self):
self.pyscript_run(
"""
<py-script>
# trigger various warnings
create("div", classes="a b c")
assert sys.__name__ == 'sys'
dedent("")
format_mime("")
assert MIME_RENDERERS['text/html'] is not None
console.log("hello")
PyScript.loop
</py-script>
<div id="mydiv"></div>
"""
)
# we EXPECTED to find a deprecation warning about what will be removed from the Python
# global namespace in the next releases
assert warning_msg in self.console.warning.lines
banner = self.page.locator(".py-warning")
messages = banner.all_inner_texts()
assert messages == [
"The PyScript object is deprecated. Please use pyscript instead.",
"Direct usage of console is deprecated. Please use js.console instead.",
"MIME_RENDERERS is deprecated. This is a private implementation detail of pyscript. You should not use it.", # noqa: E501
"format_mime is deprecated. This is a private implementation detail of pyscript. You should not use it.", # noqa: E501
"Direct usage of dedent is deprecated. Please use from textwrap import dedent instead.",
"Direct usage of sys is deprecated. Please use import sys instead.",
"Direct usage of create is deprecated. Please use pyscript.create instead.",
]