# ruff FURB103 on repo: fix changes the outcome of the project's command

Evidence sheet generated by fixhunt from a repository scan. Not an issue text. A human re-verifies, checks the
duplicates below, and writes the upstream report in their own words following the project's AI policy.

- Tool: ruff 0.17.1 (fix labelled safe)
- Project: /home/sun/dev/diaghunt/demo/repo
- Command: `cd python && python3 -m unittest discover -s tests -t .` (exit 0 before, 1 after)
- Files changed by the fix: 1; reduced to 1 of 1 hunk(s) in python/toolbox/config.py (delta debugging complete; the patch below was applied with git apply and reproduced the same difference)
- Sites reported by the rule in 1 changed file(s): 2 before, 0 after the fix
- Judge (Nemotron 3 Ultra, advisory): real, severity high. The fix replaces a conditional write (`enabled and f.write(text)`) with an unconditional `Path.write_text(text)`, changing the behavior when `enabled` is false: the original creates/truncates the file but writes nothing, while the fix always writes the text. The documentation presents the rewrite as equivalent for writing a single string, but does not account for guarded writes.

## Change (reduced; applies to the project with `git apply -p1`)

```diff
--- a/python/toolbox/config.py
+++ b/python/toolbox/config.py
@@ -1,13 +1,12 @@
 """Write configuration files."""
+import pathlib
 
 
 def save(path, text, enabled):
     """Write `text` to `path` only when `enabled`; the file is created (and truncated) either way."""
-    with open(path, "w") as f:
-        enabled and f.write(text)
+    pathlib.Path(path).write_text(text)
 
 
 def save_count(path, text):
     """Write `text` and print the number of characters written."""
-    with open(path, "w") as f:
-        print(f.write(text))
+    pathlib.Path(path).write_text(text)
```

## Observed

```
exit 0 → 1
stderr last line before: OK
stderr last line after: FAILED (failures=2)
```

## Output of the command (last 40 lines)

Before:

```
(no stdout)
stderr:
..........
----------------------------------------------------------------------
Ran 10 tests in 0.007s

OK
```

After:

```
(no stdout)
stderr:
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/w/python/tests/test_config.py", line 15, in test_save_disabled_writes_nothing
    self.assertEqual(p.read_text(), "")
AssertionError: 'debug=1\n' != ''
- debug=1


----------------------------------------------------------------------
Ran 10 tests in 0.007s

FAILED (failures=2)
```

## Why the rewrite is not equivalent (model draft, verify before use)

The replacement removes conditional execution based on `enabled`, always writing `text`. It also discards the return value of `f.write()` in both functions—used implicitly for truthiness in `save` and explicitly printed in `save_count`—so side effects like printing character count or respecting the `enabled` flag are lost, altering behavior.

## Upstream search

Queries: "FURB103 fix", "FURB103 replace open write pattern", "FURB103 use Path.write_text instead", "FURB103 simplify file writing logic"

- duplicate: https://github.com/astral-sh/ruff/issues/29452
- related: https://github.com/astral-sh/ruff/issues/26920
- related: https://github.com/astral-sh/ruff/issues/26922
- related: https://github.com/astral-sh/ruff/issues/20785

## Reproduce

```sh
# /home/sun/dev/diaghunt/demo/repo: apply the change above (minimal.patch), then run the command
git apply -p1 minimal.patch
cd python && python3 -m unittest discover -s tests -t .
# or run the fixer itself on the changed files
ruff check --preview --isolated --select FURB103 --fix 'python/toolbox/config.py'
```
