Skip to content

Commit 426bf33

Browse files
committed
Get the black and mypy fixes cleaned up. Re-organize fixtures to better mimic Element.
1 parent 0316ccf commit 426bf33

File tree

18 files changed

+151
-85
lines changed

18 files changed

+151
-85
lines changed

TODO.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
- Start documentation for example writers
1010
- Explain that we own the `src` in `py-config`
11+
- Remove the hack in noxfile to have tests only run the "fast" ones
12+
- Playright files get the example index.html directly
13+
- And thus, don't have the py-config src re-pointed to cdn
14+
- When run in nox, there are no local files and need to do CDN
1115

1216
## Eventually
1317

docs/developers/cdn_mode.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,4 @@ The `py_config.cdn.toml` file points at the CDN version.
9090

9191
- Remove the timeout
9292
- Test/implementation that calls a `is_local` function to detect the correct mode
93-
- Test/implementation which wires that into the HTML munger
93+
- Test/implementation which wires that into the HTML munger

noxfile.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,16 @@ def tests(session: Session) -> None:
176176
"python-frontmatter",
177177
)
178178
try:
179-
session.run("coverage", "run", "--parallel", "-m", "pytest", *session.posargs)
179+
session.run(
180+
"coverage",
181+
"run",
182+
"--parallel",
183+
"-m",
184+
"pytest",
185+
"-m",
186+
"not full",
187+
*session.posargs,
188+
)
180189
finally:
181190
if session.interactive:
182191
session.notify("coverage", posargs=[])

src/psc/app.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,10 @@ async def content_page(request: Request) -> _TemplateResponse:
107107
Route("/pages/{page_name}.html", content_page),
108108
Mount("/gallery", StaticFiles(directory=HERE / "gallery")),
109109
Mount("/static", StaticFiles(directory=HERE / "static")),
110+
]
111+
if PYODIDE.exists():
110112
Mount("/pyscript", StaticFiles(directory=PYSCRIPT)),
111113
Mount("/pyodide", StaticFiles(directory=PYODIDE)),
112-
]
113114

114115

115116
@contextlib.asynccontextmanager # type: ignore

src/psc/fixtures.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ class FakeDocument:
188188

189189
values: dict[str, str] = field(default_factory=dict)
190190
log: list[str] = field(default_factory=list)
191+
nodes: dict[str, FakeElement] = field(default_factory=dict)
191192

192193

193194
@pytest.fixture
@@ -197,39 +198,57 @@ def fake_document() -> Iterable[FakeDocument]:
197198

198199

199200
@dataclass
200-
class ElementNode:
201-
"""An element node."""
201+
class FakeElementNode:
202+
"""Fake for PyScript's ``element`` accessor that gets DOM node."""
203+
204+
log: list[str] = field(default_factory=list)
205+
206+
def removeAttribute(self, name: str) -> None: # noqa
207+
"""Pretend to remove an attribute from this node."""
208+
self.log.append(f"Removed {name}")
209+
210+
211+
@dataclass
212+
class FakeElement:
213+
"""A fake for PyScript's Element global."""
202214

203215
value: str
204216
document: FakeDocument
217+
element: FakeElementNode = FakeElementNode()
205218

206219
def write(self, value: str) -> None:
207220
"""Collect anything that is written to the node."""
208221
self.document.log.append(value)
209222

210-
def removeAttribute(self, name: str) -> None: # noqa
211-
"""Pretend to remove an attribute from this node."""
212-
pass
213-
214223

215224
@dataclass
216225
class ElementCallable:
217-
"""A callable that returns an ElementNode."""
226+
"""A callable that registers and returns an ElementNode."""
218227

219228
document: FakeDocument
220229

221-
def __call__(self, key: str) -> ElementNode:
230+
def __call__(self, key: str) -> FakeElement:
222231
"""Return an ElementNode."""
223232
value = self.document.values[key]
224-
node = ElementNode(value, self.document)
233+
node = FakeElement(value, self.document)
234+
self.document.nodes[key] = node
225235
return node
226236

237+
def removeAttribute(self, attr: str) -> None: # noqa: N802
238+
"""Fake the remove attribute call."""
239+
pass
240+
241+
def write(self, content: str) -> None:
242+
"""Fake the write call."""
243+
pass
244+
227245

228246
@pytest.fixture
229-
def fake_element(fake_document: FakeDocument) -> None: # type: ignore [misc]
247+
def fake_element(fake_document: FakeDocument) -> ElementCallable: # type: ignore [misc]
230248
"""Install the stateful Element into builtins."""
231249
try:
232-
builtins.Element = ElementCallable(fake_document) #type: ignore [attr-defined]
233-
yield
250+
this_element = ElementCallable(fake_document)
251+
builtins.Element = this_element # type: ignore [attr-defined]
252+
yield this_element
234253
finally:
235254
delattr(builtins, "Element")

src/psc/gallery/examples/altair/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<script defer src="../../../pyscript/pyscript.js"></script>
77
</head>
88
<body>
9-
<py-config src="../py_config.toml">
9+
<py-config src="../py_config.local.toml">
1010
packages=[
1111
"altair",
1212
"pandas",

src/psc/gallery/examples/antigravity/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<script defer src="../../../pyscript/pyscript.js"></script>
77
</head>
88
<body>
9-
<py-config src="../py_config.toml">
9+
<py-config src="../py_config.local.toml">
1010
paths = ["./antigravity.py"]
1111
</py-config>
1212
<strong>Based on xkcd: antigravity https://xkcd.com/353/.</strong>

src/psc/gallery/examples/hello_world/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<link rel="stylesheet" href="hello_world.css">
88
</head>
99
<body>
10-
<py-config src="../py_config.toml"></py-config>
10+
<py-config src="../py_config.local.toml"></py-config>
1111
<h1>Hello ...</h1>
1212
<div id="output"></div>
1313
<h6>More Info</h6>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""Say Hello."""
2-
output = Element("output") #type: ignore
2+
output = Element("output") # type: ignore
33
output.write("From Python...")

src/psc/gallery/examples/interest_calculator/calculator.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
"""Calculate the interest."""
2+
3+
14
def interest(*args, **kwargs):
25
"""Main interest calculation function."""
36
# Signal that PyScript is alive by setting the ``Calculate``

0 commit comments

Comments
 (0)