From c922230366d609d7f2175915b077697e9fc58936 Mon Sep 17 00:00:00 2001 From: Vincent Gao Date: Sun, 28 Jun 2026 13:41:27 +0200 Subject: [PATCH] fix: HexInt.to_yaml crashes with TypeError when given an int HexInt.to_yaml() calls utils.is_hexadecimal(data) which performs a regex match on the input. When data is a Python int, re.match() raises TypeError since it requires a string. Reorder the isinstance(data, int) check before the is_hexadecimal call so int values are correctly serialized to hex strings like "0xff", and non-string non-int values produce a clean YAMLSerializationError instead of a TypeError crash. Fixes round-trip: as_document({'x': 26}, Map({'x': HexInt()})) now works instead of crashing. --- strictyaml/scalar.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/strictyaml/scalar.py b/strictyaml/scalar.py index cb6e33f..b4659ef 100644 --- a/strictyaml/scalar.py +++ b/strictyaml/scalar.py @@ -201,11 +201,10 @@ def validate_scalar(self, chunk): return int(val, 16) def to_yaml(self, data): - if utils.is_hexadecimal(data): - if isinstance(data, int): - return hex(data) - else: - return data + if isinstance(data, int) and not isinstance(data, bool): + return hex(data) + if utils.is_string(data) and utils.is_hexadecimal(data): + return data raise YAMLSerializationError("'{}' not a hexademial integer.".format(data))