Skip to content

Commit cfd877f

Browse files
committed
Add test
1 parent 566684c commit cfd877f

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

tests/validators/test_dataclasses.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import dataclasses
22
import re
3+
import sys
34
from typing import Any, Dict, List, Optional, Union
45

56
import pytest
@@ -1190,3 +1191,34 @@ def test_custom_dataclass_names():
11901191
},
11911192
{'input': 123, 'loc': ('foo', 'none'), 'msg': 'Input should be None', 'type': 'none_required'},
11921193
]
1194+
1195+
1196+
@pytest.mark.skipif(sys.version_info < (3, 10), reason='slots are only supported for dataclasses in Python > 3.10')
1197+
def test_slots() -> None:
1198+
kwargs = {'slots': True}
1199+
1200+
@dataclasses.dataclass(**kwargs)
1201+
class Model:
1202+
x: int
1203+
1204+
schema = core_schema.dataclass_schema(
1205+
Model,
1206+
core_schema.dataclass_args_schema(
1207+
'Model', [core_schema.dataclass_field(name='x', schema=core_schema.int_schema())]
1208+
),
1209+
)
1210+
1211+
val = SchemaValidator(schema)
1212+
m: Model
1213+
1214+
m = val.validate_python({'x': 123})
1215+
assert m == Model(x=1)
1216+
1217+
with pytest.raises(ValidationError):
1218+
val.validate_python({'x': 'abc'})
1219+
1220+
val.validate_assignment(m, 'x', 456)
1221+
assert m.x == 456
1222+
1223+
with pytest.raises(ValidationError):
1224+
val.validate_assignment(m, 'x', 'abc')

0 commit comments

Comments
 (0)