Open the context
Choose the project folder and name the files or behavior involved.
Append one URL query pair while preserving repeated keys, blank values and their original order. Distinguish a literal plus encoded as %2B from a space and reject malformed percent escapes or invalid UTF-8.
Choose the project folder and name the files or behavior involved.
Describe the input, expected output and boundaries that must stay intact.
Review the proposed edits and any commands before relying on their result.
Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.
import re
from urllib.parse import parse_qsl, urlencode
def append_query(query, key, value):
if any(type(x) is not str for x in (query, key, value)):
raise ValueError("query.type")
if re.search(r"%(?![0-9A-Fa-f]{2})", query):
raise ValueError("query.escape")
pairs = parse_qsl(query, keep_blank_values=True,
strict_parsing=True, errors="strict", max_num_fields=1000)
return urlencode([*pairs, (key, value)])The example is a starting point for a real project, not a claim that generated code is already tested. Adjust paths, dependencies and expected behavior to match your application.