8000 Add Deprecation message when loading from latest (#1848) · shivashankarv/pyscript@e750fa7 · GitHub
[go: up one dir, main page]

Skip to content

Commit e750fa7

Browse files
fpligerpre-commit-ci[bot]antocuni
authored
Add Deprecation message when loading from latest (pyscript#1848)
* add tests to verify if we show an error banner when users load from latest * add deprecation manager to take care of showing a notification in case script src is being loaded from latest * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * make sure deprecation warning also register onWorker * restore tests for banner in worker * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add a wait selector when testing banner since worker seems to take too long to render in CI * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix test_deprecate_loading_scripts_from_latest: I think that the previous failure was because we actually TRIED to execute the js from latest/core.js and it conflicted with our local copy. But to trigger the warning is enough to have a script pointing to pyscript.net/latest, there is no need to execute it: modify it with type="ignore-me" and an URL which doesn't exist. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Antonio Cuni <anto.cuni@gmail.com>
1 parent 5a15199 commit e750fa7

File tree

6 files changed

+61
-9
lines changed

6 files changed

+61
-9
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// PyScript Derepcations Plugin
2+
import { hooks } from "../core.js";
3+
import { notify } from "./error.js";
4+
5+
// react lazily on PyScript bootstrap
6+
hooks.main.onReady.add(checkDeprecations);
7+
hooks.main.onWorker.add(checkDeprecations);
8+
9+
/**
10+
* Check that there are no scripts loading from pyscript.net/latest
11+
*/
12+
function checkDeprecations() {
13+
const scripts = document.querySelectorAll("script");
14+
for (const script of scripts) checkLoadingScriptsFromLatest(script.src);
15+
}
16+
17+
/**
18+
* Check if src being loaded from pyscript.net/latest and display a notification if true
19+
* * @param {string} src
20+
*/
21+
function checkLoadingScriptsFromLatest(src) {
22+
if (/\/pyscript\.net\/latest/.test(src)) {
23+
notify(
24+
"Loading scripts from latest is deprecated and will be removed soon. Please use a specific version instead.",
25+
);
26+
}
27+
}

pyscript.core/tests/integration/support.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,9 @@ def pyscript_run(
545545
- wait until pyscript has been fully loaded
546546
"""
547547
doc = self._pyscript_format(
548-
snippet, execution_thread=self.execution_thread, extra_head=extra_head
548+
snippet,
549+
execution_thread=self.execution_thread,
550+
extra_head=extra_head,
549551
)
550552
if not wait_for_pyscript and timeout is not None:
551553
raise ValueError("Cannot set a timeout if wait_for_pyscript=False")

pyscript.core/tests/integration/test_warnings_and_banners.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,35 @@
11
import pytest
22

3-
from .support import PyScriptTest
4-
5-
pytest.skip(reason="NEXT: Restore the banner", allow_module_level=True)
3+
from .support import PyScriptTest, skip_worker
64

75

86
class TestWarningsAndBanners(PyScriptTest):
97
# Test the behavior of generated warning banners
108

9+
def test_deprecate_loading_scripts_from_latest(self):
10+
# Use a script tag with an invalid output attribute to generate a warning, but only one
11+
self.pyscript_run(
12+
"""
13+
<script type="py">
14+
print("whatever..")
15+
</script>
16+
""",
17+
extra_head='<script type="ignore-me" src="https://pyscript.net/latest/any-path-triggers-the-warning-anyway.js"></script>',
18+
)
19+
20+
# wait for the banner to appear (we could have a page.locater call but for some reason
21+
# the worker takes to long to render on CI, since it's a test we can afford 2 calls)
22+
loc = self.page.wait_for_selector(".py-error")
23+
assert (
24+
loc.inner_text()
25+
== "Loading scripts from latest is deprecated and will be removed soon. Please use a specific version instead."
26+
)
27+
28+
# Only one banner should appear
29+
loc = self.page.locator(".py-error")
30+
assert loc.count() == 1
31+
32+
@pytest.mark.skip("NEXT: To check if behaviour is consistent with classic")
1133
def test_create_singular_warning(self):
1234
# Use a script tag with an invalid output attribute to generate a warning, but only one
1335
self.pyscript_run(

pyscript.core/types/plugins.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
declare const _default: {
2+
"deprecations-manager": () => Promise<typeof import("./plugins/deprecations-manager.js")>;
23
error: () => Promise<typeof import("./plugins/error.js")>;
34
"py-terminal": () => Promise<typeof import("./plugins/py-terminal.js")>;
45
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {};
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
declare const _default: {
2-
pyscript: {
1+
declare namespace _default {
2+
let pyscript: {
33
"__init__.py": string;
44
"display.py": string;
55
"event_handling.py": string;
66
"magic_js.py": string;
77
"util.py": string;
88
};
9-
"pyscript.py": string;
10-
pyweb: {
9+
let pyweb: {
1110
"pydom.py": string;
1211
};
13-
};
12+
}
1413
export default _default;

0 commit comments

Comments
 (0)
0