Code editing task: locked_box
Topic: Misc; change kind: adaptive

Original code:
from typing import Optional

class MyBox:
    def __init__(self, data: str):
        self.data = data

    def lock(self, pin: int) -> 'LockedMyBox':
        return LockedMyBox(self.data, pin)

    def duplicate(self) -> 'MyBox':
        return MyBox(self.data)


class LockedMyBox(MyBox):
    def __init__(self, data: str, pin: int):
        super().__init__(data)
        self._pin = pin

    def unlock(self, pin: int) -> Optional[MyBox]:
        if self._pin == pin:
            return MyBox(self.data)
        return None

    def duplicate(self) -> 'LockedMyBox':
        return LockedMyBox(self.data, self._pin)

Instruction (descriptive):
Apply the following two changes to both the `LockedMyBox` and `MyBox` classes:
1. Remove the `duplicate()` method, as it is no longer needed.
2. Add a new method `peek()` on both classes, which retrieves the contents inside the box. In the case of `LockedMyBox`, this method should throw an exception.

Instruction (lazy):
Remove the `duplicate` methods and add a new `peek` method to see the data inside the box. If the box is locked, `peek` should throw an error.

Edited code:
from typing import Optional

class MyBox:
    def __init__(self, data: str):
        self.data = data

    def lock(self, pin: int) -> 'LockedMyBox':
        return LockedMyBox(self.data, pin)

    def peek(self) -> str:
        return self.data


class LockedMyBox(MyBox):
    def __init__(self, data: str, pin: int):
        super().__init__(data)
        self._pin = pin

    def unlock(self, pin: int) -> Optional[MyBox]:
        if self._pin == pin:
            return MyBox(self.data)
        return None

    def peek(self) -> str:
        raise ValueError("Box is locked!")

Tests:
### START TESTS ###
if True:  # pragma: no cover
    box = MyBox("test data")
    assert box.peek() == "test data", "Failed to initialize MyBox with data."

    box = MyBox("peek test")
    assert box.peek() == "peek test", "Failed to peek into MyBox."


    box = MyBox("lock test")
    locked_box = box.lock(1234)
    assert isinstance(locked_box, LockedMyBox), "Failed to lock MyBox."

    # Ensure peeking on the locked box raises an error
    try:
        locked_box.peek()
        assert False, "Should have raised an error when peeking into a locked box."
    except AttributeError:
        assert False, "The LockedMyBox class should have a peek method."
    except Exception:
        pass


    box = MyBox("duplicate test")
    try:  # Ensure there is no method called "duplicate"
        x = box.duplicate
        assert False, "Should not have a duplicate method."
    except AttributeError:
        pass


    box = MyBox("unlock test")
    locked_box = box.lock(4321)

    # Wrong pin should return None
    assert locked_box.unlock(9999) is None, "Unlocked with wrong pin."

    # Correct pin should return unlocked box
    unlocked_box = locked_box.unlock(4321)
    assert isinstance(
        unlocked_box, MyBox), "Failed to unlock LockedMyBox with correct pin."


    box = MyBox("duplicate test")
    locked_box = box.lock(5678)
    # make sure there is no method called "duplicate" on LockedMyBox
    try:
        x = locked_box.duplicate
        assert False, "Should not have a duplicate method."
    except AttributeError:
        pass

    # lock, then unlock, then peek
    box = MyBox("peek test")
    locked_box = box.lock(1234)
    unlocked_box = locked_box.unlock(1234)
    assert unlocked_box is not None, "Failed to unlock box."
    assert unlocked_box.peek() == "peek test", "Failed to peek into unlocked box."

    # lock, then unlock, then lock, then peek
    box = MyBox("peek test")
    locked_box = box.lock(1234)
    unlocked_box = locked_box.unlock(1234)
    assert unlocked_box is not None, "Failed to unlock box."
    assert unlocked_box.lock(1234) is not None, "Failed to lock unlocked box."
    locked_box = unlocked_box.lock(1234)
    try:
        locked_box.peek()
        assert False, "Should have raised an error when peeking into a locked box."
    except AttributeError:
        assert False, "The LockedMyBox class should have a peek method."
    except Exception:
        pass

    # lock, then unlock, then lock, then unlock, then peek
    box = MyBox("peek test")
    locked_box = box.lock(1234)
    unlocked_box = locked_box.unlock(1234)
    assert unlocked_box is not None, "Failed to unlock box."
    assert unlocked_box.lock(1234) is not None, "Failed to lock unlocked box."
    locked_box = unlocked_box.lock(1234)
    unlocked_box = locked_box.unlock(1234)
    assert unlocked_box is not None, "Failed to unlock box."
    assert unlocked_box.peek() == "peek test", "Failed to peek into unlocked box."