Default Country Roulette — Python Bug Hunt

Inspired by the SMS-delivery mysteries every messaging integration debugs eventually: a UK number arrives as +44 20 7946 0958, the normaliser slaps the…

  • Language: Python
  • Layer: Backend
  • Difficulty: Medium
  • Concepts: Parsing, Phones
  • Modelled on: Twilio-class SMS
  • Visible tests: international numbers keep their code; national numbers get the default code; double-zero prefixes convert to plus
  • Reward: 50 XP for a complete fix

Briefing

Inspired by the SMS-delivery mysteries every messaging integration debugs eventually: a UK number arrives as +44 20 7946 0958, the normaliser slaps the default +1 in front anyway, and the OTP heads to a very confused landline in Kansas.

phone.py normalises to E.164.

Bug report

BUG-E164 · Priority: High · Reported by: OTP delivery

normalize(raw, default_cc="+1"):

  • strip spaces, dashes, dots and parentheses
  • already starts with "+" -> keep its country code
  • starts with "00" -> replace the 00 with "+"
  • bare national number -> prefix default_cc
  • anything with letters/other symbols -> raise ValueError

Observed: every already-international number gets ANOTHER country code.

Logs

[sms] normalize("+44 20 7946 0958") -> "+1+442079460958" -> undeliverable

The code as shipped

src/sms/phone.py (editable)

# Normalises phone numbers to E.164.

def normalize(raw, default_cc="+1"):
    cleaned = raw.replace(" ", "").replace("-", "").replace("(", "").replace(")", "").replace(".", "")
    return default_cc + cleaned

Open the hunt to edit the files, run the visible tests and submit against the hidden ones. More Python bug hunts.