Skip to content

Commit 847c304

Browse files
aleksandr.tseluykoSiyet
authored andcommitted
Use 'Invalid value' wording for Literal validation errors
Closes #1009
1 parent 3b2543b commit 847c304

5 files changed

Lines changed: 26 additions & 25 deletions

File tree

‎src/msgspec/_core.c‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,10 @@ _Lookup_OnMissing(Lookup *lookup, PyObject *val, PathNode *path) {
884884
}
885885
PyObject *suffix = PathNode_ErrSuffix(path);
886886
if (suffix != NULL) {
887-
PyErr_Format(mod->ValidationError, "Invalid enum value %R%U", val, suffix);
887+
const char *format = (lookup->cls != NULL)
888+
? "Invalid enum value %R%U"
889+
: "Invalid value %R%U";
890+
PyErr_Format(mod->ValidationError, format, val, suffix);
888891
Py_DECREF(suffix);
889892
}
890893

@@ -15390,7 +15393,7 @@ mpack_decode_bool(DecoderState *self, PyObject *val, TypeNode *type, PathNode *p
1539015393
return Py_False;
1539115394
}
1539215395
if (type->types & (MS_TYPE_BOOLLITERAL_TRUE | MS_TYPE_BOOLLITERAL_FALSE)) {
15393-
ms_raise_validation_error(path, "Invalid enum value %R%U", val);
15396+
ms_raise_validation_error(path, "Invalid value %R%U", val);
1539415397
return NULL;
1539515398
}
1539615399
return ms_validation_error("bool", type, path);
@@ -17023,7 +17026,7 @@ json_decode_true(JSONDecoderState *self, TypeNode *type, PathNode *path) {
1702317026
return Py_True;
1702417027
}
1702517028
if (type->types & MS_TYPE_BOOLLITERAL_FALSE) {
17026-
ms_raise_validation_error(path, "Invalid enum value %R%U", Py_True);
17029+
ms_raise_validation_error(path, "Invalid value %R%U", Py_True);
1702717030
return NULL;
1702817031
}
1702917032
return ms_validation_error("bool", type, path);
@@ -17048,7 +17051,7 @@ json_decode_false(JSONDecoderState *self, TypeNode *type, PathNode *path) {
1704817051
return Py_False;
1704917052
}
1705017053
if (type->types & MS_TYPE_BOOLLITERAL_TRUE) {
17051-
ms_raise_validation_error(path, "Invalid enum value %R%U", Py_False);
17054+
ms_raise_validation_error(path, "Invalid value %R%U", Py_False);
1705217055
return NULL;
1705317056
}
1705417057
return ms_validation_error("bool", type, path);
@@ -20708,7 +20711,7 @@ convert_bool(
2070820711
return Py_False;
2070920712
}
2071020713
if (type->types & (MS_TYPE_BOOLLITERAL_TRUE | MS_TYPE_BOOLLITERAL_FALSE)) {
20711-
ms_raise_validation_error(path, "Invalid enum value %R%U", obj);
20714+
ms_raise_validation_error(path, "Invalid value %R%U", obj);
2071220715
return NULL;
2071320716
}
2071420717
return ms_validation_error("bool", type, path);

‎tests/unit/test_common.py‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -923,10 +923,10 @@ def test_multiple_literals(self):
923923
for val in [-1, -2, -3, "apple", "banana"]:
924924
assert dec.decode(msgspec.msgpack.encode(val)) == val
925925

926-
with pytest.raises(ValidationError, match="Invalid enum value 4"):
926+
with pytest.raises(ValidationError, match="Invalid value 4"):
927927
dec.decode(msgspec.msgpack.encode(4))
928928

929-
with pytest.raises(ValidationError, match="Invalid enum value 'carrot'"):
929+
with pytest.raises(ValidationError, match="Invalid value 'carrot'"):
930930
dec.decode(msgspec.msgpack.encode("carrot"))
931931

932932
def test_nested_literals(self):
@@ -941,10 +941,10 @@ def test_nested_literals(self):
941941
for val in [-1, -2, -3, "apple", "banana"]:
942942
assert dec.decode(msgspec.msgpack.encode(val)) == val
943943

944-
with pytest.raises(ValidationError, match="Invalid enum value 4"):
944+
with pytest.raises(ValidationError, match="Invalid value 4"):
945945
dec.decode(msgspec.msgpack.encode(4))
946946

947-
with pytest.raises(ValidationError, match="Invalid enum value 'carrot'"):
947+
with pytest.raises(ValidationError, match="Invalid value 'carrot'"):
948948
dec.decode(msgspec.msgpack.encode("carrot"))
949949

950950
@pytest.mark.parametrize(
@@ -967,7 +967,7 @@ def test_literal_bool(self, typ, good, bad):
967967

968968
def test_literal_bool_error_message(self):
969969
dec = msgspec.msgpack.Decoder(Literal[True])
970-
with pytest.raises(ValidationError, match="Invalid enum value False"):
970+
with pytest.raises(ValidationError, match="Invalid value False"):
971971
dec.decode(msgspec.msgpack.encode(False))
972972

973973
def test_mix_bool_and_bool_literal(self):
@@ -4471,7 +4471,7 @@ def roundtrip(msg):
44714471

44724472
assert roundtrip("1") == 1
44734473
assert roundtrip("-2") == -2
4474-
with pytest.raises(ValidationError, match="Invalid enum value 3"):
4474+
with pytest.raises(ValidationError, match="Invalid value 3"):
44754475
roundtrip("3")
44764476
with pytest.raises(ValidationError, match="Expected `int`, got `str`"):
44774477
roundtrip("A")

‎tests/unit/test_convert.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,7 @@ def test_str_literal(self):
905905
typ = Literal["A", "B"]
906906
assert convert("A", typ) == "A"
907907
assert convert("B", typ) == "B"
908-
with pytest.raises(ValidationError, match="Invalid enum value 'C'"):
908+
with pytest.raises(ValidationError, match="Invalid value 'C'"):
909909
convert("C", typ)
910910
with pytest.raises(ValidationError, match="Expected `str`, got `int`"):
911911
convert(1, typ)
@@ -914,9 +914,9 @@ def test_int_literal(self):
914914
typ = Literal[1, -2]
915915
assert convert(1, typ) == 1
916916
assert convert(-2, typ) == -2
917-
with pytest.raises(ValidationError, match="Invalid enum value 3"):
917+
with pytest.raises(ValidationError, match="Invalid value 3"):
918918
convert(3, typ)
919-
with pytest.raises(ValidationError, match="Invalid enum value -3"):
919+
with pytest.raises(ValidationError, match="Invalid value -3"):
920920
convert(-3, typ)
921921
with pytest.raises(ValidationError, match="Expected `int`, got `str`"):
922922
convert("A", typ)
@@ -2378,7 +2378,7 @@ def test_lax_int_literal(self):
23782378
typ = Literal[1, -2]
23792379
assert convert("1", typ, strict=False) == 1
23802380
assert convert("-2", typ, strict=False) == -2
2381-
with pytest.raises(ValidationError, match="Invalid enum value 3"):
2381+
with pytest.raises(ValidationError, match="Invalid value 3"):
23822382
convert("3", typ, strict=False)
23832383
with pytest.raises(ValidationError, match="Expected `int`, got `str`"):
23842384
convert("A", typ, strict=False)

‎tests/unit/test_json.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,7 +1218,7 @@ def test_literal(self, values):
12181218
def test_int_literal_errors(self):
12191219
dec = msgspec.json.Decoder(Literal[1, 2, 3])
12201220

1221-
with pytest.raises(msgspec.ValidationError, match="Invalid enum value 4"):
1221+
with pytest.raises(msgspec.ValidationError, match="Invalid value 4"):
12221222
dec.decode(b"4")
12231223

12241224
with pytest.raises(msgspec.ValidationError, match="Expected `int`, got `str`"):
@@ -1230,7 +1230,7 @@ def test_str_literal_errors(self):
12301230
with pytest.raises(msgspec.ValidationError, match="Expected `str`, got `int`"):
12311231
dec.decode(b"4")
12321232

1233-
with pytest.raises(msgspec.ValidationError, match="Invalid enum value 'bad'"):
1233+
with pytest.raises(msgspec.ValidationError, match="Invalid value 'bad'"):
12341234
dec.decode(b'"bad"')
12351235

12361236
def test_bool_literal_true_only(self):
@@ -1970,7 +1970,7 @@ def test_decode_dict_literal_key(self):
19701970
dec = msgspec.json.Decoder(Dict[Literal["a", "b"], int])
19711971
assert dec.decode(b'{"a": 1, "b": 2}') == {"a": 1, "b": 2}
19721972

1973-
with pytest.raises(msgspec.ValidationError, match="Invalid enum value 'c'"):
1973+
with pytest.raises(msgspec.ValidationError, match="Invalid value 'c'"):
19741974
dec.decode(b'{"a": 1, "c": 2}')
19751975

19761976
def test_decode_dict_enum_key(self):
@@ -2095,7 +2095,7 @@ def test_decode_dict_int_literal_key(self):
20952095
dec = msgspec.json.Decoder(Dict[Literal[-1, 2], int])
20962096
assert dec.decode(b'{"-1": 10, "2": 20}') == {-1: 10, 2: 20}
20972097

2098-
with pytest.raises(msgspec.ValidationError, match="Invalid enum value 3"):
2098+
with pytest.raises(msgspec.ValidationError, match="Invalid value 3"):
20992099
dec.decode(b'{"-1": 10, "3": 20}')
21002100

21012101
def test_encode_dict_float_key(self):

‎tests/unit/test_msgpack.py‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,14 +1007,12 @@ def test_str_literal(self):
10071007

10081008
assert dec.decode(enc.encode("one")) == "one"
10091009

1010-
with pytest.raises(
1011-
msgspec.ValidationError, match="Invalid enum value 'MISSING'"
1012-
):
1010+
with pytest.raises(msgspec.ValidationError, match="Invalid value 'MISSING'"):
10131011
dec.decode(enc.encode("MISSING"))
10141012

10151013
with pytest.raises(
10161014
msgspec.ValidationError,
1017-
match=r"Invalid enum value 'MISSING' - at `\$\[0\]`",
1015+
match=r"Invalid value 'MISSING' - at `\$\[0\]`",
10181016
):
10191017
msgspec.msgpack.decode(enc.encode(["MISSING"]), type=List[literal])
10201018

@@ -1025,11 +1023,11 @@ def test_int_literal(self):
10251023

10261024
assert dec.decode(enc.encode(1)) == 1
10271025

1028-
with pytest.raises(msgspec.ValidationError, match="Invalid enum value 1000"):
1026+
with pytest.raises(msgspec.ValidationError, match="Invalid value 1000"):
10291027
dec.decode(enc.encode(1000))
10301028

10311029
with pytest.raises(
1032-
msgspec.ValidationError, match=r"Invalid enum value 1000 - at `\$\[0\]`"
1030+
msgspec.ValidationError, match=r"Invalid value 1000 - at `\$\[0\]`"
10331031
):
10341032
msgspec.msgpack.decode(enc.encode([1000]), type=List[literal])
10351033

0 commit comments

Comments
 (0)