Skip to content

Commit 4c9f67a

Browse files
Add "never return bool for success/failure" guideline to function design skill (#1778)
* Initial plan * Initial plan for updating agent config Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/70a7a3e2-5762-4a81-a1d4-e0c3c934df91 Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com> * Add 'Never return bool to signal success/failure' guideline to usethis-python-functions skill (v1.3) Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/70a7a3e2-5762-4a81-a1d4-e0c3c934df91 Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com>
1 parent d6f8ba7 commit 4c9f67a

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

.agents/skills/usethis-python-functions/SKILL.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: Guidelines for Python function design, including return types and s
44
compatibility: usethis, Python
55
license: MIT
66
metadata:
7-
version: "1.2"
7+
version: "1.3"
88
---
99

1010
# Function Design Guidelines
@@ -77,6 +77,43 @@ def use_tool() -> None:
7777
register_group("dev")
7878
```
7979

80+
## Never return bool to signal success or failure
81+
82+
Functions must not return `True` for success and `False` for failure. This is a C-style pattern that is un-Pythonic and error-prone. Use exception raising and handling instead.
83+
84+
### Why this matters
85+
86+
- Callers can silently ignore a `False` return, leading to bugs that go unnoticed.
87+
- Exceptions carry context (message, traceback, type) that a bare `bool` cannot.
88+
- Python's exception system is designed for this purpose — using it makes intent explicit and control flow clear.
89+
90+
### What to do instead
91+
92+
1. **Raise an exception** when an operation fails. Use a built-in exception type or define a custom one.
93+
2. **Return `None`** (or nothing) on success if the function has no meaningful value to return.
94+
3. **Let exceptions propagate** to callers, who can catch them at the appropriate level.
95+
96+
### Example
97+
98+
```python
99+
# Bad: caller must remember to check the return value.
100+
def add_config_entry(key: str, value: str) -> bool:
101+
if key in existing:
102+
return False
103+
existing[key] = value
104+
return True
105+
106+
# Good: raise on failure, return nothing on success.
107+
def add_config_entry(key: str, value: str) -> None:
108+
if key in existing:
109+
raise ValueError(f"Config entry '{key}' already exists.")
110+
existing[key] = value
111+
```
112+
113+
### When bool returns are fine
114+
115+
Returning `bool` is appropriate when the function answers a yes/no question (a predicate), such as `is_tool_used()` or `has_build_system()`. The guideline above applies only to functions where `bool` encodes an operation's outcome rather than a factual query.
116+
80117
## Avoid returning tuples
81118

82119
Functions should not return tuples. Tuple returns obscure the meaning of each element, making call sites harder to read and fragile to refactor.

skills-lock.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"find-skills": {
1515
"source": "vercel-labs/skills",
1616
"sourceType": "github",
17-
"computedHash": "d31e234f0c90694a670222cdd1dafa853e051d7066beda389f1097c22dadd461"
17+
"computedHash": "9e1c8b3103f92fa8092568a44fe64858de7c5c9dc65ce4bea8f168080e889cfd"
1818
}
1919
}
2020
}

0 commit comments

Comments
 (0)