Python traps 2 · Chapter 9 of 16

re.sub's fourth argument is count

Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Libraries
  • 3 tasks
  • Python
  • In the all labs plan

The first 2 chapters of Python traps 2 are free. This one comes with the all labs plan.

Why you need this

The call is legal Python, so it runs. Since 3.13, Python warns about a count passed by position, but a DeprecationWarning is shown only when the call is in the script you ran. From an imported module, which is where production code lives, it is silent:

$ python3 -c "import kitchen; print(kitchen.swap_roti('Roti, roti, ROTI, roti'))"
Roti, naan, ROTI, naan

pytest does show it, in the warnings summary at the end, which is exactly where Kunal found it. Any of these is right:

print(re.sub("roti", "naan", note, flags=re.IGNORECASE))
print(re.sub("(?i)roti", "naan", note))
ROTI = re.compile("roti", re.IGNORECASE)
print(ROTI.sub("naan", note))
naan, naan, naan, naan
naan, naan, naan, naan
naan, naan, naan, naan

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    repro.py: the third UPI ID, shown on purpose

    Write reproduce() in repro.py. Make a short chat text with three lower-case UPI IDs in it (such as myra.parekh@okhdfc, kabir.p@oksbi) and no other @ sign. Hide them with mask_upi from redact.py and return how many @ signs are left in the text it gave, as an int.

    The check runs your reproduce() twice: beside the original redact.py it must return 1 (the third ID still showing), and beside a fixed redact.py it must return 0. So it must really call mask_upi with three IDs. No real waiting and no input(): reproduce() must finish within 3 seconds.

  2. Task 2

    redact.py: every UPI ID hidden

    Fix mask_upi in redact.py so it replaces every UPI ID in the text with [upi hidden], however many there are, in any case (Priya@OKAXIS and rohan.d@Ybl too), and leaves everything else in the text exactly as it was. It must raise no warning. export_chat hides the IDs in the whole chat the same way.

    Keep UPI, HIDDEN and export_chat as they are. The check imports a fresh copy of your redact.py, uses its own chats with one to six IDs in upper, lower and mixed case, and records warnings: a fix that hides every lower-case ID but shows Priya@OKAXIS is as wrong as the bug.

  3. Task 3

    test_redact.py: a test that would have caught it

    Write a pytest file, test_redact.py, that would have caught this bug before an export reached a partner. The check runs your file three times: beside the original redact.py, where at least one test must fail; beside the fixed redact.py, where every test must pass; and beside a wrong fix that drops re.IGNORECASE altogether, where at least one test must fail.

    So test both sides: a text with more than two UPI IDs has every one hidden, and an upper-case ID such as Priya@OKAXIS is hidden too. No real waiting: each test must finish within 3 seconds.

What you will learn

  • repro.py: the third UPI ID, shown on purpose
  • redact.py: every UPI ID hidden
  • test_redact.py: a test that would have caught it

More in Libraries

Build "re.sub's fourth argument is count" yourself

Monsoon season at DabbaDrop brings a new pile of tickets from Kunal. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Python traps 2 are free. This one comes with the all labs plan.