pybind11/tests/test_docs_advanced_cast_custom.py

38 lines
1.2 KiB
Python
Raw Normal View History

Option for arg/return type hints and correct typing for std::filesystem::path (#5450) * Added arg/return type handling. * Added support for nested arg/return type in py::typing::List * Added support for arg/return type in stl/filesystem * Added tests for arg/return type in stl/filesystem and py::typing::List * Added arg/return name to more py::typing classes * Added arg/return type to Callable[...] * Added tests for typing container classes (also nested) * Changed typing classes to avoid using C++14 auto return type deduction. * Fixed clang-tidy errors. * Changed Enable to SFINAE * Added test for Tuple[T, ...] * Added RealNumber with custom caster for testing typing classes. * Added tests for Set, Iterable, Iterator, Union, and Optional * Added tests for Callable * Fixed Callable with ellipsis test * Changed TypeGuard/TypeIs to use return type (being the narrower type) + Tests * Added test for use of fallback type name with stl vector * Updated documentation. * Fixed unnecessary constructor call in test. * Fixed reference counting in example type caster. * Fixed clang-tidy issues. * Fix for clang-tidy * Updated cast method to use pybind11 API rather than Python C API in custom caster example * Updated load to use pybind11 API rather than Python C API in custom caster example * Changed test of arg/return name to use pybind11 API instead of Python C API * Updated code in adcanced/cast example and improved documentation text * Fixed references in custom type caster docs * Fixed wrong logical and operator in test * Fixed wrong logical operator in doc example * Added comment to test about `float` vs `float | int` * Updated std::filesystem::path docs in cast/overview section * Remove one stray dot. --------- Co-authored-by: Ralf W. Grosse-Kunstleve <rgrossekunst@nvidia.com>
2024-12-08 19:30:49 +00:00
from __future__ import annotations
from typing import TYPE_CHECKING, Sequence
if TYPE_CHECKING:
from conftest import SanitizedString
from pybind11_tests import docs_advanced_cast_custom as m
def assert_negate_function(
input_sequence: Sequence[float],
target: tuple[float, float],
) -> None:
output = m.negate(input_sequence)
assert isinstance(output, tuple)
assert len(output) == 2
assert isinstance(output[0], float)
assert isinstance(output[1], float)
assert output == target
def test_negate(doc: SanitizedString) -> None:
assert doc(m.negate) == "negate(arg0: Sequence[float]) -> tuple[float, float]"
assert_negate_function([1.0, -1.0], (-1.0, 1.0))
assert_negate_function((1.0, -1.0), (-1.0, 1.0))
assert_negate_function([1, -1], (-1.0, 1.0))
assert_negate_function((1, -1), (-1.0, 1.0))
def test_docs() -> None:
###########################################################################
# PLEASE UPDATE docs/advanced/cast/custom.rst IF ANY CHANGES ARE MADE HERE.
###########################################################################
point1 = [1.0, -1.0]
point2 = m.negate(point1)
assert point2 == (-1.0, 1.0)