From c721deb1cca66a0dbc25a8ad6e674568189aa1fe Mon Sep 17 00:00:00 2001 From: Giovanni Grano Date: Wed, 22 Apr 2026 17:11:33 +0200 Subject: [PATCH] follow-up for defaults --- src/py_avro_schema/_schemas.py | 19 +++++++++---------- tests/test_dataclass.py | 6 ++++++ 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/py_avro_schema/_schemas.py b/src/py_avro_schema/_schemas.py index ad21090..8a9b19b 100644 --- a/src/py_avro_schema/_schemas.py +++ b/src/py_avro_schema/_schemas.py @@ -1188,16 +1188,6 @@ def data(self, names: NamesType) -> JSONObj: field_data["aliases"] = sorted(self.aliases) if self.default != dataclasses.MISSING: default_value = self.schema.make_default(self.default) - - # When a field is a string, but it's default value produces a UUID-like, we do not pass through the UUID - # schema (which already sets an empty default). We need to catch here the strings that look like a UUID - # and set a deterministic default. - if ( - Option.DETERMINISTIC_DEFAULTS in self.options - and isinstance(default_value, str) - and (_UUID_PATTERN.match(default_value) or is_valid_datetime(default_value)) - ): - default_value = "" field_data["default"] = default_value if self.docs and Option.NO_DOC not in self.options: field_data["doc"] = self.docs @@ -1232,6 +1222,15 @@ def _record_field(self, py_field: dataclasses.Field) -> RecordField: default = py_field.default if callable(py_field.default_factory): # type: ignore default = py_field.default_factory() # type: ignore + # When a field is a string, but it's default value produces a UUID-like, we do not pass through the UUID + # schema (which already sets an empty default). We need to catch here the strings that look like a UUID + # and set a deterministic default. + if ( + Option.DETERMINISTIC_DEFAULTS in self.options + and isinstance(default, str) + and (_UUID_PATTERN.match(default) or is_valid_datetime(default)) + ): + default = "" aliases, actual_type = get_field_aliases_and_actual_type(py_field.type) # type: ignore field_obj = RecordField( py_type=actual_type, # type: ignore diff --git a/tests/test_dataclass.py b/tests/test_dataclass.py index de3da3b..aefbf1f 100644 --- a/tests/test_dataclass.py +++ b/tests/test_dataclass.py @@ -953,6 +953,7 @@ def timestamp_millis() -> str: @dataclasses.dataclass class PyType: time: str = dataclasses.field(default_factory=lambda: timestamp_millis()) + version: str = "2017-01-01" expected = { "type": "record", @@ -963,6 +964,11 @@ class PyType: "type": "string", "default": "", }, + { + "name": "version", + "type": "string", + "default": "2017-01-01", + }, ], } assert_schema(PyType, expected, options=pas.Option.DETERMINISTIC_DEFAULTS)