sansan0/TrendRadar · error · ValueError

week_map 缺少星期映射: {day}

Error message

week_map 缺少星期映射: {day}

What it means

Raised by _validate_timeline when week_map does not cover all ISO weekdays 1..7. Every day must be mapped to some day_plan, even if that plan is empty — the scheduler resolves 'today' unconditionally on each tick. This is the startup-time twin of the runtime error at scheduler.py:131.

Source

Thrown at trendradar/core/scheduler.py:328

    # 校验
    # ========================================

    def _validate_timeline(self, timeline: Dict[str, Any]) -> None:
        """
        启动时校验 timeline 配置

        Raises:
            ValueError: 配置不合法时抛出
        """
        required_top_keys = ["default", "periods", "day_plans", "week_map"]
        for key in required_top_keys:
            if key not in timeline:
                raise ValueError(f"timeline 缺少必须字段: {key}")

        # week_map 必须覆盖 1..7
        for day in range(1, 8):
            if day not in timeline["week_map"]:
                raise ValueError(f"week_map 缺少星期映射: {day}")

        # day_plan 引用完整性
        for day, plan_key in timeline["week_map"].items():
            if plan_key not in timeline["day_plans"]:
                raise ValueError(
                    f"week_map[{day}] 引用了不存在的 day_plan: {plan_key}"
                )

        # period 引用完整性
        for plan_key, plan in timeline["day_plans"].items():
            for period_key in plan.get("periods", []):
                if period_key not in timeline["periods"]:
                    raise ValueError(
                        f"day_plan[{plan_key}] 引用了不存在的 period: {period_key}"
                    )

        # 时间格式校验
        for period_key, period in timeline["periods"].items():

View on GitHub (pinned to 8ee26026ba)

Solutions

  1. Add entries for days 6 and 7 (and any other missing day) pointing at a rest/empty day_plan.
  2. Ensure weekday keys are integers 1-7, not strings, in YAML.
  3. Remember the contract: absence of a key is invalid; an empty periods list is the correct way to express 'nothing scheduled'.

Example fix

# before
week_map: {1: work, 2: work, 3: work, 4: work, 5: work}
# after
week_map: {1: work, 2: work, 3: work, 4: work, 5: work, 6: rest, 7: rest}
day_plans: {work: {periods: [...]}, rest: {periods: []}}
Defensive patterns

Strategy: validation

Validate before calling

week_map = timeline.get("week_map", {})
missing_days = [d for d in range(1, 8) if d not in week_map]
if missing_days:
    raise ValueError(f"week_map incomplete, missing days: {missing_days}")
# keys must be ints, not strings
assert all(isinstance(d, int) for d in week_map), "weekday keys must be ints 1-7"

Type guard

def week_map_covers_all_days(t: dict) -> bool:
    wm = t.get("week_map", {})
    return all(d in wm for d in range(1, 8))

Try / catch

try:
    scheduler = Scheduler(schedule_config, timeline_data)
except ValueError as e:
    if "week_map 缺少星期映射" in str(e):
        raise SystemExit(f"map days {range(1,8)} to day_plans (use a rest plan for off-days)")
    raise

Prevention

When it happens

Trigger: A week_map with only keys 1-5 (weekdays only); numeric keys quoted as strings ('6' instead of 6) so the integer loop misses them; a weekend day deleted intentionally.

Common situations: Users modeling 'no runs on weekends' by omitting the days; YAML quoting turning integer keys into strings; partial copy from an example that only showed weekdays.

Related errors


AI-assisted analysis of sansan0/TrendRadar@8ee26026ba (2026-08-15). Data as JSON: /api/errors/493c39dd2c72724e. Report an issue: GitHub.