Intermediate13 min read

Ask a local coding model for a change you can test

Reproduce a bug, prompt a local coding model with the contract, and evaluate its proposed Python change with unit tests.

Reproduce the bug

The library uses 24-hour times for reservations. Its helper currently accepts inputs such as 24:00 and 9:05. The contract is exactly two ASCII digits, a colon, and two ASCII digits. Hours must be 00 through 23 and minutes 00 through 59.

Save both files in the same folder. The tests use Python’s standard library.

Download library_slots.py
View library_slots.py
def minutes_since_midnight(value: str) -> int:
    hours, minutes = map(int, value.split(":"))
    return hours * 60 + minutes
Download test_library_slots.py
View test_library_slots.py
import unittest

from library_slots import minutes_since_midnight


class LibrarySlotTests(unittest.TestCase):
    def test_valid_times(self):
        for value, expected in (("00:00", 0), ("09:05", 545), ("23:59", 1439)):
            with self.subTest(value=value):
                self.assertEqual(minutes_since_midnight(value), expected)

    def test_rejects_bad_shape(self):
        for value in ("9:05", "09:5", " 09:05", "09:05\n", "09:05", "09-05"):
            with self.subTest(value=value):
                with self.assertRaises(ValueError):
                    minutes_since_midnight(value)

    def test_rejects_out_of_range_values(self):
        for value in ("24:00", "12:60", "-1:15"):
            with self.subTest(value=value):
                with self.assertRaises(ValueError):
                    minutes_since_midnight(value)
python -m unittest -v
You’re ready when

The valid-time test passes and several invalid inputs fail with “ValueError not raised.” Keep the test file as it is so you can compare the same cases after the change.

Give the model a contract

Open a chat with the 1.5B coding model in Ollama. The first run downloads about 1 GB. Send the prompt as one message. It includes the full function and the required behavior.

ollama run qwen2.5-coder:1.5b
Fix this Python function using only the standard library.
Accept exactly HH:MM using ASCII digits, with hours 00–23 and minutes 00–59.
Return the number of minutes since midnight. Raise ValueError for any
invalid string, including extra whitespace. Keep the function signature.
Return only the updated Python code, including any import it needs.

def minutes_since_midnight(value: str) -> int:
    hours, minutes = map(int, value.split(":"))
    return hours * 60 + minutes

Check the proposed change

Read the proposal and apply the replacement to library_slots.py if it fits the contract. Run the same tests. When a case fails, give the model that input, the expected behavior, and the current function. Re-run the complete suite after each revision.

python -m unittest -v

In one run, the model added numeric range checks but still accepted 9:05. It also caught its own error and returned None. The tests caught both issues.

You’re ready when

All three tests pass. Add another boundary case, such as 00:59 returning 59, to check behavior the original suite did not cover.

View a tested solution

Match the full string first, then check the two numeric ranges.

import re


def minutes_since_midnight(value: str) -> int:
    if re.fullmatch(r"[0-9]{2}:[0-9]{2}", value) is None:
        raise ValueError("Use HH:MM.")
    hours, minutes = map(int, value.split(":"))
    if hours >= 24 or minutes >= 60:
        raise ValueError("Use a 24-hour time.")
    return hours * 60 + minutes
Check your understanding

Why include 23:59 and 24:00?

Together they check the boundary between the latest valid time and the first out-of-range hour.

Sources

Search the guide