Review generated tenant pricing handlers

from Scope and namespaces
Python 3.14 advanced 8 min 4 issues to find

Review this generated handler factory for isolation, safety, and clear name ownership.

Create one pricing handler per tenant, with an independent attempt count and a tenant-specific discount.

Python
attempts = 0

def build_handlers(tenants):
    handlers = []
    for tenant in tenants:
        rate = tenant["discount"]
        formula = tenant["formula"]

        def handle(order):
            global attempts
            attempts += 1
            if order["total"] <= 0:
                return None
            locals()["rate"] = rate
            price = eval(formula)
            return {
                "tenant": tenant["name"],
                "attempt": attempts,
                "price": price,
            }

        handlers.append(handle)
    return handlers

generated code is illustrative, not from any one model

Open in playground
Report an error