Using possibly undefined loop variable 'tc'
377 organization=organization,
378 customer=customer,
379 event_name="generate_text",
380 time_created=tc,381 idempotency_id=uuid.uuid4(),
382 properties={
383 "language": language,
Using possibly undefined loop variable 'tc'
390 organization=organization,
391 customer=customer,
392 event_name="computation",
393 time_created=tc,394 idempotency_id=uuid.uuid4(),
395 properties={
396 "cost": abs(compute_time * random.gauss(0.05, 0.01)),
Description
Using a loop variable (i.e. defined by a for loop or a list comprehension or a generator expression) outside the loop can create subtle bugs, like the variable not being defined if the list was empty. Generally, you should avoid using loop variables outside the loop, or define them early.
Bad practice
for i in items:
if item_name(i) == "coffee":
price = calculate_cost(i)
print(price) # raises `NameError` if `items` is empty
Preferred:
price = None # Define variable outside loop
for i in items:
if item_name(i) == "coffee":
price = calculate_cost(i)
print(price)