From ab891c4fbdcd42f4cedb11a704d0b3467c8f3a4b Mon Sep 17 00:00:00 2001 From: oyeong011 Date: Tue, 8 Sep 2026 11:48:59 +0900 Subject: [PATCH] Reject blank float arguments instead of silently using zero The float converter passes blank strings into Number(), which returns zero. Reject empty/whitespace-only strings while preserving surrounding whitespace on valid numeric input. Confidence: high Scope-risk: narrow Tested: Regression red/green, full test suite, exported API QA --- lib/argparse.js | 2 +- test/test_argparse.js | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/argparse.js b/lib/argparse.js index 87b2f31..1613933 100644 --- a/lib/argparse.js +++ b/lib/argparse.js @@ -2715,7 +2715,7 @@ const ArgumentParser = _callable(class ArgumentParser extends _AttributeHolder(_ }) this.register('type', 'float', function (x) { const result = _string_to_number(x) - if (isNaN(result)) { + if (isNaN(result) || (typeof x === 'string' && !x.trim())) { throw new TypeError(sub('could not convert string to float: %r', x)) } return result diff --git a/test/test_argparse.js b/test/test_argparse.js index 82127bd..0ceb6fb 100644 --- a/test/test_argparse.js +++ b/test/test_argparse.js @@ -7961,3 +7961,16 @@ VV VV VV assert(!demo_parser.format_help().includes('\x1b[')) } }).run() + +describe('blank float arguments', () => { + it('rejects empty and whitespace-only values', () => { + const parser = new argparse.ArgumentParser({ exit_on_error: false }) + parser.add_argument('--float', { type: 'float' }) + for (const value of ['', ' ', '\t\n']) { + assert.throws(() => parser.parse_args(['--float', value]), argparse.ArgumentError) + } + for (const value of ['0', ' 0 ', '\t1.5\n']) { + assert.strictEqual(parser.parse_args(['--float', value]).float, Number(value)) + } + }) +})