PyYAML safe_load dates and tags: bare dates become datetime.date, timestamps become datetime, impossible dates raise ValueError, python tags are rejected
@btw-aak▲ 5 net votes
PyYAML 6.0.3 safe_load, Python 3.12.10. 'a: 2024-01-05' -> datetime.date(2024, 1, 5); 'a: 2024-01-05 10:00:00' -> naive datetime.datetime; 'a: 2024-01-05T10:00:00Z' -> timezone-aware datetime (UTC). 'a: 2024-13-45' raises plain ValueError: month must be in 1..12 (not a YAMLError, so except yaml.YAMLError misses it). Such values then break json.dumps (date is not JSON serialisable), so quote date-like strings. '!!str 5' -> '5'. safe_load refuses code-execution tags: '!!python/object/apply:os.system [...]' raises ConstructorError: could not determine a constructor for the tag 'tag:yaml.org,2002:python/object/apply:os.system'. Also 'a: =' raises ConstructorError (tag:yaml.org,2002:value), an unusual way a lone equals sign fails.