Skip to content

Commit b437602

Browse files
author
aleksandr.tseluyko
committed
Use 'Invalid value' wording for Literal validation errors
Closes #1009
1 parent bd3c43b commit b437602

5 files changed

Lines changed: 26 additions & 23 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

@@ -15366,7 +15369,7 @@ mpack_decode_bool(DecoderState *self, PyObject *val, TypeNode *type, PathNode *p
1536615369
return Py_False;
1536715370
}
1536815371
if (type->types & (MS_TYPE_BOOLLITERAL_TRUE | MS_TYPE_BOOLLITERAL_FALSE)) {
15369-
ms_raise_validation_error(path, "Invalid enum value %R%U", val);
15372+
ms_raise_validation_error(path, "Invalid value %R%U", val);
1537015373
return NULL;
1537115374
}
1537215375
return ms_validation_error("bool", type, path);
@@ -16999,7 +17002,7 @@ json_decode_true(JSONDecoderState *self, TypeNode *type, PathNode *path) {
1699917002
return Py_True;
1700017003
}
1700117004
if (type->types & MS_TYPE_BOOLLITERAL_FALSE) {
17002-
ms_raise_validation_error(path, "Invalid enum value %R%U", Py_True);
17005+
ms_raise_validation_error(path, "Invalid value %R%U", Py_True);
1700317006
return NULL;
1700417007
}
1700517008
return ms_validation_error("bool", type, path);
@@ -17024,7 +17027,7 @@ json_decode_false(JSONDecoderState *self, TypeNode *type, PathNode *path) {
1702417027
return Py_False;
1702517028
}
1702617029
if (type->types & MS_TYPE_BOOLLITERAL_TRUE) {
17027-
ms_raise_validation_error(path, "Invalid enum value %R%U", Py_False);
17030+
ms_raise_validation_error(path, "Invalid value %R%U", Py_False);
1702817031
return NULL;
1702917032
}
1703017033
return ms_validation_error("bool", type, path);
@@ -20683,7 +20686,7 @@ convert_bool(
2068320686
return Py_False;
2068420687
}
2068520688
if (type->types & (MS_TYPE_BOOLLITERAL_TRUE | MS_TYPE_BOOLLITERAL_FALSE)) {
20686-
ms_raise_validation_error(path, "Invalid enum value %R%U", obj);
20689+
ms_raise_validation_error(path, "Invalid value %R%U", obj);
2068720690
return NULL;
2068820691
}
2068920692
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
@@ -903,10 +903,10 @@ def test_multiple_literals(self):
903903
for val in [-1, -2, -3, "apple", "banana"]:
904904
assert dec.decode(msgspec.msgpack.encode(val)) == val
905905

906-
with pytest.raises(ValidationError, match="Invalid enum value 4"):
906+
with pytest.raises(ValidationError, match="Invalid value 4"):
907907
dec.decode(msgspec.msgpack.encode(4))
908908

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

912912
def test_nested_literals(self):
@@ -921,10 +921,10 @@ def test_nested_literals(self):
921921
for val in [-1, -2, -3, "apple", "banana"]:
922922
assert dec.decode(msgspec.msgpack.encode(val)) == val
923923

924-
with pytest.raises(ValidationError, match="Invalid enum value 4"):
924+
with pytest.raises(ValidationError, match="Invalid value 4"):
925925
dec.decode(msgspec.msgpack.encode(4))
926926

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

930930
@pytest.mark.parametrize(
@@ -947,7 +947,7 @@ def test_literal_bool(self, typ, good, bad):
947947

948948
def test_literal_bool_error_message(self):
949949
dec = msgspec.msgpack.Decoder(Literal[True])
950-
with pytest.raises(ValidationError, match="Invalid enum value False"):
950+
with pytest.raises(ValidationError, match="Invalid value False"):
951951
dec.decode(msgspec.msgpack.encode(False))
952952

953953
def test_mix_bool_and_bool_literal(self):
@@ -4451,7 +4451,7 @@ def roundtrip(msg):
44514451

44524452
assert roundtrip("1") == 1
44534453
assert roundtrip("-2") == -2
4454-
with pytest.raises(ValidationError, match="Invalid enum value 3"):
4454+
with pytest.raises(ValidationError, match="Invalid value 3"):
44554455
roundtrip("3")
44564456
with pytest.raises(ValidationError, match="Expected `int`, got `str`"):
44574457
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)
@@ -2366,7 +2366,7 @@ def test_lax_int_literal(self):
23662366
typ = Literal[1, -2]
23672367
assert convert("1", typ, strict=False) == 1
23682368
assert convert("-2", typ, strict=False) == -2
2369-
with pytest.raises(ValidationError, match="Invalid enum value 3"):
2369+
with pytest.raises(ValidationError, match="Invalid value 3"):
23702370
convert("3", typ, strict=False)
23712371
with pytest.raises(ValidationError, match="Expected `int`, got `str`"):
23722372
convert("A", typ, strict=False)

‎tests/unit/test_json.py‎

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

1217-
with pytest.raises(msgspec.ValidationError, match="Invalid enum value 4"):
1217+
with pytest.raises(msgspec.ValidationError, match="Invalid value 4"):
12181218
dec.decode(b"4")
12191219

12201220
with pytest.raises(msgspec.ValidationError, match="Expected `int`, got `str`"):
@@ -1226,7 +1226,7 @@ def test_str_literal_errors(self):
12261226
with pytest.raises(msgspec.ValidationError, match="Expected `str`, got `int`"):
12271227
dec.decode(b"4")
12281228

1229-
with pytest.raises(msgspec.ValidationError, match="Invalid enum value 'bad'"):
1229+
with pytest.raises(msgspec.ValidationError, match="Invalid value 'bad'"):
12301230
dec.decode(b'"bad"')
12311231

12321232

@@ -1943,7 +1943,7 @@ def test_decode_dict_literal_key(self):
19431943
dec = msgspec.json.Decoder(Dict[Literal["a", "b"], int])
19441944
assert dec.decode(b'{"a": 1, "b": 2}') == {"a": 1, "b": 2}
19451945

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

19491949
def test_decode_dict_enum_key(self):
@@ -2068,7 +2068,7 @@ def test_decode_dict_int_literal_key(self):
20682068
dec = msgspec.json.Decoder(Dict[Literal[-1, 2], int])
20692069
assert dec.decode(b'{"-1": 10, "2": 20}') == {-1: 10, 2: 20}
20702070

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

20742074
def test_encode_dict_float_key(self):

‎tests/unit/test_msgpack.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,13 +1008,13 @@ def test_str_literal(self):
10081008
assert dec.decode(enc.encode("one")) == "one"
10091009

10101010
with pytest.raises(
1011-
msgspec.ValidationError, match="Invalid enum value 'MISSING'"
1011+
msgspec.ValidationError, match="Invalid value 'MISSING'"
10121012
):
10131013
dec.decode(enc.encode("MISSING"))
10141014

10151015
with pytest.raises(
10161016
msgspec.ValidationError,
1017-
match=r"Invalid enum value 'MISSING' - at `\$\[0\]`",
1017+
match=r"Invalid value 'MISSING' - at `\$\[0\]`",
10181018
):
10191019
msgspec.msgpack.decode(enc.encode(["MISSING"]), type=List[literal])
10201020

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

10261026
assert dec.decode(enc.encode(1)) == 1
10271027

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

10311031
with pytest.raises(
1032-
msgspec.ValidationError, match=r"Invalid enum value 1000 - at `\$\[0\]`"
1032+
msgspec.ValidationError, match=r"Invalid value 1000 - at `\$\[0\]`"
10331033
):
10341034
msgspec.msgpack.decode(enc.encode([1000]), type=List[literal])
10351035

0 commit comments

Comments
 (0)