From d0ef52a7a245b5015a7e79043a3230e5484f93b3 Mon Sep 17 00:00:00 2001 From: konsti Date: Tue, 6 Aug 2024 11:06:24 +0200 Subject: [PATCH] Don't textwrap toml scenarios (#205) Unlike json scenarios, toml and yaml scenarios have multi-line description fields, in which we can write proper markdown. Textwrap doesn't understand markdown and mangles it. To fix https://github.com/astral-sh/uv/pull/5441#discussion_r1691470051, we stop textwrapping toml and yaml files. I've tested this locally and the snapshots look better, due to https://github.com/astral-sh/uv/issues/5475 is somewhat incomplete though: https://github.com/astral-sh/uv/pull/5441. --- src/packse/scenario.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/packse/scenario.py b/src/packse/scenario.py index 782aebf6..49aa328d 100644 --- a/src/packse/scenario.py +++ b/src/packse/scenario.py @@ -273,6 +273,11 @@ class Scenario(msgspec.Struct, forbid_unknown_fields=True): The description of the scenario. """ + _textwrap: bool = False + """ + Whether to wrap long lines in the scenario description (`.json` scenarios) or not (`.toml` or `.yaml` scenarios). + """ + def hash(self) -> str: """ Return a hash of the scenario contents @@ -298,9 +303,17 @@ def dict(self) -> dict: return json.loads(enc.encode(self)) -def _load(target: Path, type: Type): +def _load[T: Scenario | list[Scenario]](target: Path, type: Type[T]) -> T: if target.suffix == ".json": - return msgspec.json.decode(target.read_text(), type=type, dec_hook=dec_hook) + loaded = msgspec.json.decode(target.read_text(), type=type, dec_hook=dec_hook) + # json scenarios have unformatted single line descriptions that we need to wrap for rust docstrings, while + # toml and yaml descriptions are already formatted and should be converted verbatim (with the default: False). + if isinstance(loaded, Scenario): + loaded._textwrap = True + else: + for scenario in loaded: + scenario._textwrap = True + return loaded elif target.suffix == ".toml": return msgspec.toml.decode(target.read_text(), type=type, dec_hook=dec_hook) elif target.suffix == ".yaml":