Repository: copier-org/copier (python)

Issue:
When running `copier update`, responses to questions with "when==false" retain the last answers instead of reverting to the defaults.
### Describe the problem

Hi,

In the [documentation](https://copier.readthedocs.io/en/stable/configuring/#advanced-prompt-formatting), it states:
> - when: Condition that, if false, skips the question. ...  
> If a question is skipped, its answer is not recorded, but its default value is available in the render context.

However, when `copier update` is executed, the answers to skipped questions persist as their last values instead of updating to the default values.

### Template

https://github.com/kubotty/copier-default-debug

### To Reproduce

1. Apply the template:
```bash
mkdir copier-default-check
cd copier-default-check
git init
copier copy https://github.com/kubotty/copier-default-debug.git -r 0.4.0 .
```
```
🎤 First question
Yes
🎤 Second question
Yes

Copying from template version 0.4.0
    create  file.md
    create  README.md
    create  .copier-answers.yml
```

Check the content of `file.md`:
```bash
cat file.md
```
```
- True
- True
- Yes
- Yes
```

2. Update and change the answer, but it retains the last answer:
```bash
git add .
git commit -m v0.4.0
copier update -r 0.5.0
```
```
Updating to template version 0.5.0
🎤 First question
No
```
```bash
cat file.md
```
```
- False
- True
- No
- Yes
```


### Logs

```console

```

### Expected behavior

The default values should be reflected in the updates:
```bash
copier update -r 0.5.0
    Updating to template version 0.5.0
    🎤 First question
    No
```
After checking the content of `file.md`, it should show:
```bash
cat file.md
```
Expected Output:
```
- False
- False
- No
- No
```

### Screenshots/screencasts/logs

_No response_

### Operating system

Windows

### Operating system distribution and version

Windows 11 Enterprize

### Copier version

9.6.0

### Python version

3.12

### Installation method

pipx+pypi

### Additional context

_No response_

Pull request description:
fix(updating): ignore last answer of `when: false` questions
I've fixed a bug that caused an update or recopy involving the switch of a question's `when` value from `when: true` to `when: false` to reuse the recorded answer, specifically for computed values the default value wasn't used to re-compute the answer. 

To fix it, we now delete the last answer of a `when: false` question.

Fixes #2057.

Patch:
diff --git a/copier/main.py b/copier/main.py
index ce462f0..69c6f15 100644
--- a/copier/main.py
+++ b/copier/main.py
@@ -563,6 +563,10 @@ class Worker:
             if not question.get_when():
                 # Omit its answer from the answers file.
                 self.answers.hide(var_name)
+                # Delete last answers to re-compute the answer from the default
+                # value (if it exists).
+                if var_name in self.answers.last:
+                    del self.answers.last[var_name]
                 # Skip immediately to the next question when it has no default
                 # value.
                 if question.default is MISSING:


Test patch:
diff --git a/tests/test_recopy.py b/tests/test_recopy.py
index 20bcedb..09dd2a5 100644
--- a/tests/test_recopy.py
+++ b/tests/test_recopy.py
@@ -130,3 +130,50 @@ def test_recopy_dont_validate_computed_value(
     run_recopy(dst, overwrite=True)
     answers = load_answersfile_data(dst)
     assert "computed" not in answers
+
+
+def test_conditional_computed_value(tmp_path_factory: pytest.TempPathFactory) -> None:
+    src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
+
+    build_file_tree(
+        {
+            src / "copier.yml": (
+                """\
+                first:
+                    type: bool
+
+                second:
+                    type: bool
+                    default: "{{ first }}"
+                    when: "{{ first }}"
+                """
+            ),
+            src / "{{ _copier_conf.answers_file }}.jinja": (
+                "{{ _copier_answers|to_nice_yaml }}"
+            ),
+            src / "log.txt.jinja": "{{ first }} {{ second }}",
+        }
+    )
+    git_save(src)
+
+    run_copy(str(src), dst, data={"first": True}, defaults=True)
+    answers = load_answersfile_data(dst)
+    assert answers["first"] is True
+    assert answers["second"] is True
+    assert (dst / "log.txt").read_text() == "True True"
+
+    git_save(dst, "v1")
+
+    run_recopy(dst, data={"first": False}, overwrite=True)
+    answers = load_answersfile_data(dst)
+    assert answers["first"] is False
+    assert "second" not in answers
+    assert (dst / "log.txt").read_text() == "False False"
+
+    git_save(dst, "v2")
+
+    run_recopy(dst, data={"first": True}, defaults=True, overwrite=True)
+    answers = load_answersfile_data(dst)
+    assert answers["first"] is True
+    assert answers["second"] is True
+    assert (dst / "log.txt").read_text() == "True True"
diff --git a/tests/test_updatediff.py b/tests/test_updatediff.py
index 07f42c3..b4f5f0d 100644
--- a/tests/test_updatediff.py
+++ b/tests/test_updatediff.py
@@ -1869,3 +1869,55 @@ def test_conflict_on_update_with_unicode_in_content(
             zzz🐍
             """
         )
+
+
+def test_conditional_computed_value(tmp_path_factory: pytest.TempPathFactory) -> None:
+    src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
+
+    build_file_tree(
+        {
+            src / "copier.yml": (
+                """\
+                first:
+                    type: bool
+
+                second:
+                    type: bool
+                    default: "{{ first }}"
+                    when: "{{ first }}"
+                """
+            ),
+            src / "{{ _copier_conf.answers_file }}.jinja": (
+                "{{ _copier_answers|to_nice_yaml }}"
+            ),
+            src / "log.txt.jinja": "{{ first }} {{ second }}",
+        }
+    )
+    with local.cwd(src):
+        git_init("v1")
+        git("tag", "v1")
+
+    run_copy(str(src), dst, data={"first": True}, defaults=True)
+    answers = load_answersfile_data(dst)
+    assert answers["first"] is True
+    assert answers["second"] is True
+    assert (dst / "log.txt").read_text() == "True True"
+
+    with local.cwd(dst):
+        git_init("v1")
+
+    run_update(dst, data={"first": False}, overwrite=True)
+    answers = load_answersfile_data(dst)
+    assert answers["first"] is False
+    assert "second" not in answers
+    assert (dst / "log.txt").read_text() == "False False"
+
+    with local.cwd(dst):
+        git("add", ".")
+        git("commit", "-m", "v2")
+
+    run_update(dst, data={"first": True}, defaults=True, overwrite=True)
+    answers = load_answersfile_data(dst)
+    assert answers["first"] is True
+    assert answers["second"] is True
+    assert (dst / "log.txt").read_text() == "True True"