Skip to content

Commit 8a450b0

Browse files
authored
Merge pull request #1040 from sirosen/update-delimited-fields-empty-default
Set empty_value to 'missing' for delimited fields
2 parents 65cf032 + a6a30cc commit 8a450b0

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ Other changes:
1717
Users should use ``await parser.async_parse()`` to access the async features
1818
of ``Parser``.
1919

20+
* *Backwards-incompatible*: `DelimitedList` and `DelimitedTuple` fields have
21+
changed their default `empty_value` from the empty string (`""`) to `missing`.
22+
This allows nested fields with a `load_default` to be used to better customize
23+
behavior.
24+
2025
* Drop support for Python 3.9, which is EOL (:pr:`1019`).
2126
* Drop support for Bottle < 0.13 (:pr:`1019`).
2227
* Drop support for Flask < 3.1.0 (:pr:`1023`).

src/webargs/fields.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ class DelimitedFieldMixin:
4242
delimiter: str = ","
4343
# delimited fields set is_multiple=False for webargs.core.is_multiple
4444
is_multiple: bool = False
45-
# NOTE: in 8.x this defaults to "" but in 9.x it will be 'missing'
46-
empty_value: typing.Any = ""
45+
empty_value: typing.Any = ma.missing
4746

4847
def _serialize(self, value, attr, obj, **kwargs):
4948
# serializing will start with parent-class serialization, so that we correctly

tests/test_core.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
INCLUDE,
1212
RAISE,
1313
Schema,
14-
missing,
1514
pre_load,
1615
validates_schema,
1716
)
@@ -1119,21 +1118,30 @@ class ZeroTuple(fields.DelimitedTuple):
11191118
assert parsed["ids"] == (1, 0, 3)
11201119

11211120

1122-
def test_delimited_list_using_missing_for_empty(web_request, parser):
1123-
# this is "future" because we plan to make this the default for webargs v9.0
1124-
class FutureList(fields.DelimitedList):
1125-
empty_value = missing
1126-
1121+
def test_delimited_list_missing_element_defaults_to_load_default(web_request, parser):
11271122
web_request.json = {"ids": "foo,,bar"}
11281123
schema_cls = Schema.from_dict(
1129-
{"ids": FutureList(fields.String(load_default="nil"))}
1124+
{"ids": fields.DelimitedList(fields.String(load_default="nil"))}
11301125
)
11311126
schema = schema_cls()
11321127

11331128
parsed = parser.parse(schema, web_request)
11341129
assert parsed["ids"] == ["foo", "nil", "bar"]
11351130

11361131

1132+
def test_delimited_list_using_empty_string_for_empty(web_request, parser):
1133+
# this is "past" because it was the default for webargs v8.x
1134+
class PastList(fields.DelimitedList):
1135+
empty_value = ""
1136+
1137+
web_request.json = {"ids": "foo,,bar"}
1138+
schema_cls = Schema.from_dict({"ids": PastList(fields.String(load_default="nil"))})
1139+
schema = schema_cls()
1140+
1141+
parsed = parser.parse(schema, web_request)
1142+
assert parsed["ids"] == ["foo", "", "bar"]
1143+
1144+
11371145
def test_missing_list_argument_not_in_parsed_result(web_request, parser):
11381146
# arg missing in request
11391147
web_request.json = {}

0 commit comments

Comments
 (0)