Fix the event tag builder for a tracking endpoint

Review the AI's codeEasy10 min

Your task

Your FastAPI service tags each tracked event before it saves it. An AI wrote the tag helper with a mutable default argument, tags: list = []. Python creates that list once and shares it across calls, so tags from one user's request showed up on later requests. The batch version builds the tags for a series of events, one per request.

An assistant wrote build_event_tags(base_tags, names). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • base_tags: the tags every event starts with, as a list of strings. It can be empty and can hold repeats and empty strings.
  • names: the event names, one per request, in order. A name can be any string, including the empty string. The list can be empty.
  • Return one list of tags per name, in the same order as names.

Rules the tests check:

  • An event's list is base_tags, in order and exactly as given, followed by one tag: event: plus that event's name.
  • Use the name exactly as given. Do not trim it, and do not check for an existing prefix, so the name event:y gives the tag event:event:y.
  • Each event's list holds the base tags and its own event tag, nothing else. Every list has one more item than base_tags.
  • With no names, return an empty list.

Examples

build_event_tags(base_tags: list[str], names: list[str]) -> list[list[str]]
Example 1
Inputbuild_event_tags(["env:prod"], ["signup", "login"])
Output[["env:prod", "event:signup"], ["env:prod", "event:login"]]
Example 2
Inputbuild_event_tags([], ["a", "a", "a"])
Output[["event:a"], ["event:a"], ["event:a"]]
Example 3
Inputbuild_event_tags(["t1", "t2"], ["x"])
Output[["t1", "t2", "event:x"]]