Skip to content

Fix Route handlers convert falsy values (0, False) to empty strings #572#890

Merged
jph00 merged 1 commit into
AnswerDotAI:mainfrom
civvic:vic_falsy_routing
Jun 27, 2026
Merged

Fix Route handlers convert falsy values (0, False) to empty strings #572#890
jph00 merged 1 commit into
AnswerDotAI:mainfrom
civvic:vic_falsy_routing

Conversation

@civvic

@civvic civvic commented Jun 23, 2026

Copy link
Copy Markdown

Fix route handlers converting falsy values (0, False) to empty strings (#572)

Problem

Route handlers returning 0 or False rendered as an empty string instead of '0' / 'False'. Values like 1 and True worked correctly, making the behaviour inconsistent.

Cause

In _resp (fasthtml/core.py), the guard if not resp: resp='' is true for all falsy values. It's meant to catch "no response", but 0 and False are also falsy, so they were silently replaced with '' before reaching the str(resp) conversion lower down.

Fix

Narrow the check to only the value that actually means "no response":

-    if not resp: resp=''
+    if resp is None: resp=''

Now 0 and False fall through to resp = str(resp) and render as '0' and 'False'. None still renders empty, and empty containers ((), []) are unaffected since they were never caught by this line — they flow through the FT-rendering path as before.

Testing

@rt('/zero')
def get(): return 0

assert cli.get('/zero').text == '0'
assert cli.get('/false').text == 'False'
assert cli.get('/one').text == '1'
assert cli.get('/true').text == 'True'

Closes #572.

@jph00

jph00 commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

Thanks :)

@jph00 jph00 merged commit 72ec0a8 into AnswerDotAI:main Jun 27, 2026
@jph00 jph00 added the bug Something isn't working label Jun 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Route handlers convert falsy values (0, False) to empty strings

2 participants