python-tools.sh: Annotate generated conftest.py

create-conftest-py generates the conftest.py that pytest test directories
pick up at build time, and make clean removes it. The generated file lacks
type annotations, though: pytest_configure() and the nested patched() take
untyped parameters, and the replacement of .pytest_sessionstart() on the
class is rejected by strict mypy.

Emit a type-annotated version instead: add a future annotations import,
type both functions, move the types that are only used in annotations
behind a TYPE_CHECKING guard, and silence the method replacement with a
targeted type ignore.

Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.85.1
Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-09-11 17:41:30 +02:00
commit 83e77ca12e
Signed by: Jan Lindemann
GPG key ID: 3750640C9E25DD61

View file

@ -35,17 +35,25 @@ module_path()
cmd_create_conftest_py()
{
cat <<-'EOT'
from __future__ import annotations
import os
import sys
import shutil
import sys
def pytest_configure(config):
import _pytest.terminal
import _pytest.timing as timing
from typing import TYPE_CHECKING
# orig = _pytest.terminal.TerminalReporter.pytest_sessionstart
from _pytest.terminal import TerminalReporter
from _pytest.timing import Instant
def patched(self, session):
if TYPE_CHECKING:
from _pytest.config import Config
from _pytest.main import Session
def pytest_configure(config: Config) -> None:
"""Print a banner centered in the terminal when the session starts."""
def patched(self: TerminalReporter, session: Session) -> None:
width = shutil.get_terminal_size().columns
d = os.path.basename(os.getcwd())
text = f"Running pytest for {d}"
@ -59,9 +67,9 @@ cmd_create_conftest_py()
banner = text
print(banner, file = sys.stderr)
self._session = session
self._session_start = timing.Instant()
self._session_start = Instant()
_pytest.terminal.TerminalReporter.pytest_sessionstart = patched
TerminalReporter.pytest_sessionstart = patched # type: ignore[method-assign]
EOT
}