From 0df725463a51810966845d64be954e92b8b22cb2 Mon Sep 17 00:00:00 2001 From: Titus Date: Sat, 28 Nov 2026 04:11:01 +0100 Subject: [PATCH] 0.158.4: a date written with its weekday parses back parse_date reads a weekday name after every other name, longest first, as a whole word, from a calendar that prints one, and refuses a weekday that is not the date's own, naming both, as the npm SDK 4.220.1 does. The separators are unchanged. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 4 +++ src/onlyworlds/_version.py | 2 +- src/onlyworlds/calendar.py | 46 ++++++++++++++++++++++++------ tests/test_calendar.py | 58 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 101 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ab7a7d..990855b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ The package version runs on its own semver; the schema it is generated from is ` ## Unreleased +## 0.158.4 (2026-11-28) + +- **Fixed: a date written with its weekday parses back.** `parse_date` read era, month and intercalary names and never a weekday's, so a world whose `format` prints `{weekday}` could not read its own dates: `"Moonday, 3 Frost 454"` answered `unrecognized: "moonday"`. A weekday name is now read after every other name, longest first, as a whole word: a word that is both a month's and a weekday's still reads as the month, a name that reads as a number is never tried, and a calendar whose names do not match its week's length (it prints no weekday) reads none. A weekday that is not the date's own is refused, naming both (`that date falls on Sunday, not Moonday`; `that date has no weekday, not Moonday` for a day outside the week), and a weekday alone names no date. The separators stay whitespace, the comma, the period and the word `year`. Nothing that parsed before parses differently. The npm SDK 4.220.1 reads the same. + ## 0.158.3 (2026-11-28) - **Fixed: a request body is refused for its depth only past 256 levels, where onlyworlds.com stops reading a body**, its top counted as 1: `ValueError: a request body is nested more than 256 deep (an object or list at the top is depth 1), past what onlyworlds.com reads. Nothing was sent.` 0.158.2 drew the line at 200 counted from the body's top, which refused a `bulk` item whose element held a value 199 or 200 levels deep. The server stores that value, since it counts a stored value's 200 from the element it sits in, and the folder readers read it back. A value nested past 200 in an element is still refused, by the server, with a 422 that names its key. As the npm SDK (4.219.3). diff --git a/src/onlyworlds/_version.py b/src/onlyworlds/_version.py index 2a391a4..92f7daf 100644 --- a/src/onlyworlds/_version.py +++ b/src/onlyworlds/_version.py @@ -1 +1 @@ -__version__ = "0.158.3" +__version__ = "0.158.4" diff --git a/src/onlyworlds/calendar.py b/src/onlyworlds/calendar.py index 9bfd324..cd9dfe6 100644 --- a/src/onlyworlds/calendar.py +++ b/src/onlyworlds/calendar.py @@ -1387,7 +1387,7 @@ def _week_skipped(cfg: object) -> tuple[int, list[_LeapRule]]: def weekday_for_step(cfg: Mapping[str, Any], step: int) -> str | None: """The weekday of a step, or None: no week, a nested week outside the months, a day out of the week, - or fewer names than the week's length.""" + or a count of names other than the week's length.""" s = _in(step) return cast("str | None", _weekday(cfg, s, _position(cfg, s))) @@ -1724,9 +1724,10 @@ def parse_date(cfg: Mapping[str, Any], text: str, *, now: int | None = None) -> A bare number (``"-454"``) is always the raw step. Otherwise calendar words are matched without regard to case: ``"454 AE"``, ``"Year 12 FT"``, ``"Frostwane 454"``, ``"12 Frostwane 454"``, - ``"Shieldmeet 1372"``; a partial date opens its period. A month and a day with no year read in the - year holding ``now``, and with no ``now`` are refused, never guessed. ``format_step`` of the result is - ``format_step`` of the step that was written; the step itself is not promised back. + ``"Shieldmeet 1372"``, ``"Moonday, 12 Frostwane 454"``; a partial date opens its period. A weekday beside + a whole date is refused when it is not that date's own, and names no date alone. A month and a day with no + year read in the year holding ``now``, and with no ``now`` are refused, never guessed. ``format_step`` of + the result is ``format_step`` of the step that was written; the step itself is not promised back. """ if not isinstance(text, str): raise TypeError(f"parse_date: the text is a string, got {text!r}") @@ -1776,6 +1777,35 @@ def eat(name: object) -> bool: break m += 1 + # A weekday name last, longest first, and only from a calendar that prints one (as many names as the + # week's length): a word that is a month's and a weekday's reads as the month, and a name that reads as + # a number is never tried. It names no date alone; the date must be whole without it, and a weekday + # that is not that date's own is refused (``checked`` below), never guessed past. + weekday: str | None = None + week = _bundle(cfg, "week") + if _truthy(week): + week_length, week_names = _get(week, "length"), _get(week, "names") + if _js_le(1, week_length) and isinstance(week_names, list) and _strict_eq(len(week_names), week_length): + day_names = [ + n + for n in week_names + if isinstance(n, str) and _js_trim(_normalize(n)) != "" and not _INT.fullmatch(_js_trim(_normalize(n))) + ] + for n in _js_sorted(day_names, lambda a, b: _length_of(b) - _length_of(a)): + if eat(n): + weekday = n + break + + def checked(r: ParseResult | ParseError) -> ParseResult | ParseError: + if weekday is None or "error" in r: + return r + own = weekday_for_step(cfg, r["step"]) + if own == weekday: + return r + if own is None: + return {"error": f"that date has no weekday, not {weekday}"} + return {"error": f"that date falls on {_js_string(own)}, not {weekday}"} + # What remains must be numbers only. numbers: list[float] = [] for part in [p for p in _RUN.split(_js_trim(rest)) if p]: @@ -1835,11 +1865,11 @@ def resolve_in_year(abs_year: float) -> ParseResult | ParseError: if year is None: if now_n is None: return {"error": "add a year (the world clock has no now set)"} - return resolve_in_year(_abs_year(cfg, now_n)) + return checked(resolve_in_year(_abs_year(cfg, now_n))) target = era if era is not None else _default_era(cfg, now_n) if target is None: - return resolve_in_year(year) + return checked(resolve_in_year(year)) # The era's reading first; a step outside the era's own range (format shows absolute years outside # every era) falls back to the absolute reading, so format(parse(format(n))) holds. @@ -1847,13 +1877,13 @@ def resolve_in_year(abs_year: float) -> ParseResult | ParseError: if "error" in via_era: return via_era if _era_for(cfg, _num(via_era["step"])) is target: - return via_era + return checked(via_era) via_abs = resolve_in_year(year) # A typed era names the date's era: an absolute reading that lands in an era is a date the input # never named, and is refused; the fallback stays for a step in no era. if era is not None and "error" not in via_abs and _era_for(cfg, _num(via_abs["step"])) is not None: return {"error": f"not a date in {_js_string(_get(era, 'name'))}"} - return via_abs + return checked(via_abs) def is_parse_error(result: ParseResult | ParseError) -> bool: diff --git a/tests/test_calendar.py b/tests/test_calendar.py index 0596cc1..c3ae221 100644 --- a/tests/test_calendar.py +++ b/tests/test_calendar.py @@ -355,6 +355,64 @@ def test_the_parser() -> None: parse_date(KIRROWMOSS, "12", Now=5) # type: ignore[call-arg] # an unknown option, as the SDK refuses one +def test_a_weekday_beside_a_whole_date_is_read_and_a_wrong_one_refused_naming_both() -> None: + own = weekday_for_step(KIRROWMOSS, 7603) + assert own == "Tresk" + for s in ( + f"{own}, 14 Thistlewane, Year 22 FT", + f"{own} 14 Thistlewane Year 22 FT", + f"14 Thistlewane, {own.upper()}. Year 22 FT", + ): + assert parse_date(KIRROWMOSS, s) == {"step": 7603} + assert parse_date(KIRROWMOSS, "Ondel, 14 Thistlewane, Year 22 FT") == { + "error": "that date falls on Tresk, not Ondel" + } + # With no year the date reads in the now's year, and is checked there. + assert parse_date(KIRROWMOSS, f"{own}, Thistlewane 14", now=7900) == {"step": 7603} + assert parse_date(KIRROWMOSS, "Volk, Thistlewane 14", now=7900) == {"error": "that date falls on Tresk, not Volk"} + # A refusal the date already had comes first. + assert parse_date(KIRROWMOSS, f"{own}, Thistlewane 14") == {"error": "add a year (the world clock has no now set)"} + + +def test_a_weekday_names_no_date_alone_and_a_day_outside_the_week_has_none() -> None: + assert is_parse_error(parse_date(KIRROWMOSS, "Tresk")) + assert is_parse_error(parse_date(KIRROWMOSS, "Tresk FT")) + # Harptos' week is nested in the months: Shieldmeet, between them, has no weekday. + assert not is_parse_error(parse_date(HARPTOS, "Shieldmeet 1372")) + assert parse_date(HARPTOS, "1st, Shieldmeet 1372") == {"error": "that date has no weekday, not 1st"} + assert step_of(parse_date(HARPTOS, "3rd, 3 Hammer 1372")) == step_of(parse_date(HARPTOS, "3 Hammer 1372")) + assert parse_date(HARPTOS, "3rd Hammer 1372") == {"error": "that date falls on 1st, not 3rd"} + + +def test_weekday_names_are_tried_last_never_as_numbers_and_only_when_printed() -> None: + months = {"names": ["Frost", "Thaw"], "lengths": [10, 10]} + # A word that is a month's and a weekday's reads as the month. + shared = { + "version": 1, + "grain": "day", + "bundles": {"months": months, "week": {"length": 2, "names": ["Frost", "Sunday"]}}, + } + assert parse_date(shared, "3 Frost 4") == {"step": 4 * 20 + 2} + # A name that reads as a number is never tried: "1 Frost 4" stays day 1. + numeric = {"version": 1, "grain": "day", "bundles": {"months": months, "week": {"length": 2, "names": ["1", "2"]}}} + assert parse_date(numeric, "1 Frost 4") == {"step": 4 * 20} + # Fewer or more names than the week's length print no weekday, and read none. + for names in (["Moonday"], ["Moonday", "Sunday", "Starday"]): + ragged = {"version": 1, "grain": "day", "bundles": {"months": months, "week": {"length": 2, "names": names}}} + assert parse_date(ragged, "Moonday, 1 Frost 4") == {"error": 'unrecognized: "moonday"'} + + +def test_a_template_with_the_weekday_parses_back_every_step_of_three_years() -> None: + for fmt in ( + "{weekday}, {day} {month}, Year {year} {era}", + "{weekday} {day} {month} {year} {era}", + "{day} {month} {year} {era}, {weekday}", + ): + cfg = {**KIRROWMOSS, "format": fmt} + misses = [n for n in range(7200, 7200 + 3 * 360) if parse_date(cfg, format_step(cfg, n)) != {"step": n}] + assert misses == [], (fmt, misses[:5]) + + def test_format_parse_format_is_stable() -> None: for cfg in (KIRROWMOSS, HARPTOS, SHIRE, DUNE, VESSACH): for n in (-3601, -108, -1, 0, 30, 213, 7603, 500596):