Review an account lifecycle snapshot
Find human accounts needing review without confusing missing history with proven inactivity.
Define the review population
Account exports often mix human users, service identities and disabled records. This rule reviews only enabled human accounts. Service identities need a different lifecycle policy; a long interval without interactive logon is not enough to decide that one is unused.
Use the supplied now value so the decision is reproducible. Calculate a cutoff with timedelta(days=inactive_days). A last logon strictly before the cutoff is inactive; equality remains inside the allowed interval. Parse offsets and compare actual UTC instants.
Missing history needs its own explanation
No recorded logon may indicate an unstarted account or an incomplete export. Return no history instead of inventing an old date. Malformed or naive timestamps and future timestamps need distinct review reasons. Preserve that uncertainty for the administrator making the next decision.
Normalise account keys for this fictional export and reject duplicate keys before relying on the data. Keep the rule's output small: normalized account and reason, sorted by account. The function proposes reviews; it does not disable users, remove access or contact an identity provider.
The checks include disabled and service accounts, exact date boundaries and unknown history. This is a good place to practise explaining why a row appears in a report: a short reason is more useful than a boolean named bad.
from datetime import datetime, timedelta
now = datetime.fromisoformat("2026-04-01T00:00:00+00:00")
print(now - timedelta(days=30))
Put it into practice.
Define review_accounts(accounts, now, inactive_days). Require aware now and a positive int days (not bool), otherwise ValueError. Normalize account keys with strip().casefold(); any duplicate raises ValueError. Review only kind == human and enabled is True. Emit account/reason for last_login before now-days (inactive), None/empty (no history), malformed/naive (invalid timestamp), or after now (future timestamp). Equality at the cutoff is allowed. Sort by account; never change account records.
See an explained solution
from datetime import datetime, timezone
def parse_utc(value):
moment = datetime.fromisoformat(value)
if moment.tzinfo is None or moment.utcoffset() is None:
raise ValueError("Timestamp needs an offset")
return moment.astimezone(timezone.utc)
from datetime import timedelta
def review_accounts(accounts, now, inactive_days):
current = parse_utc(now)
if type(inactive_days) is not int or inactive_days < 1:
raise ValueError("Days must be a positive integer")
cutoff = current - timedelta(days=inactive_days)
result, seen = [], set()
for row in accounts:
account = row["account"].strip().casefold()
if account in seen:
raise ValueError("Duplicate account")
seen.add(account)
if row["kind"] != "human" or row["enabled"] is not True:
continue
stamp = row.get("last_login")
if stamp is None or stamp == "":
reason = "no history"
else:
try:
last = parse_utc(stamp)
reason = "future timestamp" if last > current else "inactive" if last < cutoff else None
except (ValueError, TypeError):
reason = "invalid timestamp"
if reason:
result.append({"account":account, "reason":reason})
return sorted(result, key=lambda row:row["account"])
if __name__ == "__main__":
print(review_accounts([{"account":"alex", "kind":"human", "enabled":True, "last_login":None}], "2026-04-01T00:00:00Z", 30))
The function turns a clear policy into review reasons while preserving uncertainty. It performs no account mutation and does not apply human logon rules to service identities.