MCPcopy Index your code
hub / github.com/msgspec/msgspec / test_generic_struct

Method test_generic_struct

tests/unit/test_convert.py:2171–2192  ·  view source on GitHub ↗
(self, mapcls, from_attributes, array_like)

Source from the content-addressed store, hash-verified

2169class TestGenericStruct:
2170 @mapcls_from_attributes_and_array_like
2171 def test_generic_struct(self, mapcls, from_attributes, array_like):
2172 class Ex(Struct, Generic[T], array_like=array_like):
2173 x: T
2174 y: List[T]
2175
2176 sol = Ex(1, [1, 2])
2177 msg = mapcls(x=1, y=[1, 2])
2178
2179 res = convert(msg, Ex, from_attributes=from_attributes)
2180 assert res == sol
2181
2182 res = convert(msg, Ex[int], from_attributes=from_attributes)
2183 assert res == sol
2184
2185 res = convert(msg, Ex[Union[int, str]], from_attributes=from_attributes)
2186 assert res == sol
2187
2188 res = convert(msg, Ex[float], from_attributes=from_attributes)
2189 assert type(res.x) is float
2190
2191 with pytest.raises(ValidationError, match="Expected `str`, got `int`"):
2192 convert(msg, Ex[str], from_attributes=from_attributes)
2193
2194 @mapcls_from_attributes_and_array_like
2195 def test_generic_struct_union(self, mapcls, from_attributes, array_like):

Callers

nothing calls this directly

Calls 1

ExClass · 0.70

Tested by

no test coverage detected