MCPcopy Create free account
hub / github.com/pybind/pybind11 / test_float_convert

Function test_float_convert

tests/test_builtin_casters.py:327–360  ·  view source on GitHub ↗
(doc)

Source from the content-addressed store, hash-verified

325
326
327def test_float_convert(doc):
328 class Int:
329 def __int__(self):
330 return -5
331
332 class Index:
333 def __index__(self) -> int:
334 return -7
335
336 class Float:
337 def __float__(self):
338 return 41.45
339
340 convert, noconvert = m.float_passthrough, m.float_passthrough_noconvert
341 assert (
342 doc(convert)
343 == "float_passthrough(arg0: typing.SupportsFloat | typing.SupportsIndex) -> float"
344 )
345 assert doc(noconvert) == "float_passthrough_noconvert(arg0: float) -> float"
346
347 def requires_conversion(v):
348 pytest.raises(TypeError, noconvert, v)
349
350 def cant_convert(v):
351 pytest.raises(TypeError, convert, v)
352
353 requires_conversion(Float())
354 requires_conversion(Index())
355 assert pytest.approx(convert(Float())) == 41.45
356 assert pytest.approx(convert(Index())) == -7.0
357 assert isinstance(convert(Float()), float)
358 assert pytest.approx(convert(3)) == 3.0
359 requires_conversion(3)
360 cant_convert(Int())
361
362
363def test_numpy_int_convert():

Callers

nothing calls this directly

Calls 7

requires_conversionFunction · 0.85
FloatClass · 0.85
IndexClass · 0.85
isinstanceFunction · 0.85
cant_convertFunction · 0.85
IntClass · 0.85
docFunction · 0.70

Tested by

no test coverage detected