Fix the UTF-8 check for partner payload files

Review the AI's codeMedium18 min

Your task

Your service ingests files from partners and is moving from Python 2 to Python 3. Python 3 decodes text strictly, so bytes that are not valid UTF-8 raise an error in production. Before the cutover you want to classify each payload the way Python 3 will see it.

An assistant wrote classify_partner_payload(byte_values). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • byte_values: the payload as a list of integers.
  • Return "empty", "ascii", "utf8" or "invalid".

Rules the tests check:

  • An empty list is "empty".
  • If any value is below 0 or above 255, the payload is "invalid".
  • Otherwise read the bytes from the start as UTF-8 sequences. A sequence starts with a lead byte:
    • 0 to 127: the byte stands alone.
    • 194 to 223: 1 more byte follows, from 128 to 191.
    • 224: a byte from 160 to 191 follows, then 1 byte from 128 to 191.
    • 225 to 236, 238 or 239: 2 more bytes follow, each from 128 to 191.
    • 237: a byte from 128 to 159 follows, then 1 byte from 128 to 191.
    • 240: a byte from 144 to 191 follows, then 2 bytes from 128 to 191.
    • 241 to 243: 3 more bytes follow, each from 128 to 191.
    • 244: a byte from 128 to 143 follows, then 2 bytes from 128 to 191.
  • The payload is "invalid" if a lead byte is any other value (128 to 193, or 245 to 255), if a following byte is out of its range, or if the list ends in the middle of a sequence.
  • A valid payload is "ascii" when every byte is 127 or less, else "utf8".

Examples

classify_partner_payload(byte_values: list[int]) -> str
Example 1
Inputclassify_partner_payload([])
Output"empty"
Example 2
Inputclassify_partner_payload([72, 101, 108, 108, 111])
Output"ascii"
Example 3
Inputclassify_partner_payload([67, 97, 102, 195, 169])
Output"utf8"