From 7402fcff1c80b1fd5ce4ad1b0e2bea71c5970cfc Mon Sep 17 00:00:00 2001 From: g4lb Date: Mon, 30 Nov 2026 10:47:37 +0200 Subject: [PATCH 1/2] Reject non-ASCII digits in numbers Fixes #727 --- CHANGELOG.md | 4 ++++ tests/test_parser.py | 16 ++++++++++++++++ tomlkit/parser.py | 4 ++++ 3 files changed, 24 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 29373b09..45e8562f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ - Speed up parsing of comments: the comment text is scanned in one pass up to the line break or the first invalid control character, instead of one character at a time. ([#723](https://github.com/python-poetry/tomlkit/pull/723)) +### Fixed + +- The parser now rejects numbers with non-ASCII digits, such as `1٠` or `10`. Previously `int()` and `float()` converted them, so an invalid document parsed to a wrong value. + ## [0.15.3] - 2026-11-24 ### Changed diff --git a/tests/test_parser.py b/tests/test_parser.py index f3335e6a..265a6a53 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -351,3 +351,19 @@ def test_parser_preserves_valid_numeric_literals(raw: str, expected: float) -> N else: assert value == expected assert dumps(document) == content + + +@pytest.mark.parametrize( + "value", + [ + "1\u0660", + "0x1\u0660", + "1.\u0660", + "1e\u0661", + "1\uff10", + "1_\u0660", + ], +) +def test_parser_rejects_non_ascii_digits_in_numbers(value: str) -> None: + with pytest.raises(ParseError): + parse(f"x = {value}\n") diff --git a/tomlkit/parser.py b/tomlkit/parser.py index f2d235f0..1dcaa3ac 100644 --- a/tomlkit/parser.py +++ b/tomlkit/parser.py @@ -777,6 +777,10 @@ def _parse_number(self, raw: str, trivia: Trivia) -> Item | None: if "_" in clean: return None + # int() and float() accept any Unicode decimal digit, TOML only ASCII digits. + if not clean.isascii(): + return None + # Special float values must be lowercase in the original token. if clean in ("inf", "nan") and raw != clean: return None From 8285246e362cfce4a49e79be067f7d6575c09ba9 Mon Sep 17 00:00:00 2001 From: g4lb Date: Mon, 30 Nov 2026 10:53:41 +0200 Subject: [PATCH 2/2] Add the PR link to the changelog entry --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 45e8562f..4b4c1024 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ ### Fixed -- The parser now rejects numbers with non-ASCII digits, such as `1٠` or `10`. Previously `int()` and `float()` converted them, so an invalid document parsed to a wrong value. +- The parser now rejects numbers with non-ASCII digits, such as `1٠` or `10`. Previously `int()` and `float()` converted them, so an invalid document parsed to a wrong value. ([#728](https://github.com/python-poetry/tomlkit/pull/728)) ## [0.15.3] - 2026-11-24