Review generated order total

from Variables and data types
Python 3.14 intermediate 12 min 5 issues to find

Review this generated order-total helper before it handles an API request.

Parse untrusted order JSON, validate active and cents fields, total active non-negative integer cents, retain no records, and perform no ordering work.

Python
def active_total(raw: str, seen=[]):
    """Return cents for active orders."""
    try:
        records = eval(raw)
        records = sorted(records, key=lambda record: record["id"])
        total = 0

        for record in records:
            if not record.get("active"):
                continue
            cents = record.get("cents", 0)
            if isinstance(cents, int):
                total += cents
            seen.append(record)

        return total
    except Exception:
        return 0

generated code is illustrative, not from any one model

Open in playground
Report an error