mirror of
https://github.com/pybind/pybind11.git
synced 2026-04-19 22:39:09 +00:00
* Add typing.SupportsIndex to int/float/complex type hints
This corrects a mistake where these types were supported but the type
hint was not updated to reflect that SupportsIndex objects are accepted.
To track the resulting test failures:
The output of
"$(cat PYROOT)"/bin/python3 $HOME/clone/pybind11_scons/run_tests.py $HOME/forked/pybind11 -v
is in
~/logs/pybind11_pr5879_scons_run_tests_v_log_2025-11-10+122217.txt
* Cursor auto-fixes (partial) plus pre-commit cleanup. 7 test failures left to do.
* Fix remaining test failures, partially done by cursor, partially manually.
* Cursor-generated commit: Added the Index() tests from PR 5879.
Summary:
Changes Made
1. **C++ Bindings** (`tests/test_builtin_casters.cpp`)
• Added complex_convert and complex_noconvert functions needed for the tests
2. **Python Tests** (`tests/test_builtin_casters.py`)
`test_float_convert`:
• Added Index class with __index__ returning -7
• Added Int class with __int__ returning -5
• Added test showing Index() works with convert mode: assert pytest.approx(convert(Index())) == -7.0
• Added test showing Index() doesn't work with noconvert mode: requires_conversion(Index())
• Added additional assertions for int literals and Int() class
`test_complex_cast`:
• Expanded the test to include convert and noconvert functionality
• Added Index, Complex, Float, and Int classes
• Added test showing Index() works with convert mode: assert convert(Index()) == 1 and assert isinstance(convert(Index()), complex)
• Added test showing Index() doesn't work with noconvert mode: requires_conversion(Index())
• Added type hint assertions matching the SupportsIndex additions
These tests demonstrate that custom __index__ objects work with float and complex in convert mode, matching the typing.SupportsIndex type hint added in PR
5891.
* Reflect behavior changes going back from PR 5879 to master. This diff will have to be reapplied under PR 5879.
* Add PyPy-specific __index__ handling for complex caster
Extract PyPy-specific __index__ backporting from PR 5879 to fix PyPy 3.10
test failures in PR 5891. This adds:
1. PYBIND11_INDEX_CHECK macro in detail/common.h:
- Uses PyIndex_Check on CPython
- Uses hasattr check on PyPy (workaround for PyPy 7.3.3 behavior)
2. PyPy-specific __index__ handling in complex.h:
- Handles __index__ objects on PyPy 7.3.7's 3.8 which doesn't
implement PyLong_*'s __index__ calls
- Mirrors the logic used in numeric_caster for ints and floats
This backports __index__ handling for PyPy, matching the approach
used in PR 5879's expand-float-strict branch.
73 lines
2.7 KiB
Python
73 lines
2.7 KiB
Python
from __future__ import annotations
|
|
|
|
from pybind11_tests import docstring_options as m
|
|
|
|
|
|
def test_docstring_options():
|
|
# options.disable_function_signatures()
|
|
assert not m.test_function1.__doc__
|
|
|
|
assert m.test_function2.__doc__ == "A custom docstring"
|
|
|
|
# docstring specified on just the first overload definition:
|
|
assert m.test_overloaded1.__doc__ == "Overload docstring"
|
|
|
|
# docstring on both overloads:
|
|
assert m.test_overloaded2.__doc__ == "overload docstring 1\noverload docstring 2"
|
|
|
|
# docstring on only second overload:
|
|
assert m.test_overloaded3.__doc__ == "Overload docstr"
|
|
|
|
# options.enable_function_signatures()
|
|
assert m.test_function3.__doc__.startswith(
|
|
"test_function3(a: typing.SupportsInt | typing.SupportsIndex, b: typing.SupportsInt | typing.SupportsIndex) -> None"
|
|
)
|
|
|
|
assert m.test_function4.__doc__.startswith(
|
|
"test_function4(a: typing.SupportsInt | typing.SupportsIndex, b: typing.SupportsInt | typing.SupportsIndex) -> None"
|
|
)
|
|
assert m.test_function4.__doc__.endswith("A custom docstring\n")
|
|
|
|
# options.disable_function_signatures()
|
|
# options.disable_user_defined_docstrings()
|
|
assert not m.test_function5.__doc__
|
|
|
|
# nested options.enable_user_defined_docstrings()
|
|
assert m.test_function6.__doc__ == "A custom docstring"
|
|
|
|
# RAII destructor
|
|
assert m.test_function7.__doc__.startswith(
|
|
"test_function7(a: typing.SupportsInt | typing.SupportsIndex, b: typing.SupportsInt | typing.SupportsIndex) -> None"
|
|
)
|
|
assert m.test_function7.__doc__.endswith("A custom docstring\n")
|
|
|
|
# when all options are disabled, no docstring (instead of an empty one) should be generated
|
|
assert m.test_function8.__doc__ is None
|
|
|
|
# Suppression of user-defined docstrings for non-function objects
|
|
assert not m.DocstringTestFoo.__doc__
|
|
assert not m.DocstringTestFoo.value_prop.__doc__
|
|
|
|
# Check existig behaviour of enum docstings
|
|
assert (
|
|
m.DocstringTestEnum1.__doc__
|
|
== "Enum docstring\n\nMembers:\n\n Member1\n\n Member2"
|
|
)
|
|
|
|
# options.enable_enum_members_docstring()
|
|
assert (
|
|
m.DocstringTestEnum2.__doc__
|
|
== "Enum docstring\n\nMembers:\n\n Member1\n\n Member2"
|
|
)
|
|
|
|
# options.disable_enum_members_docstring()
|
|
assert m.DocstringTestEnum3.__doc__ == "Enum docstring"
|
|
|
|
# options.disable_user_defined_docstrings()
|
|
assert m.DocstringTestEnum4.__doc__ == "Members:\n\n Member1\n\n Member2"
|
|
|
|
# options.disable_user_defined_docstrings()
|
|
# options.disable_enum_members_docstring()
|
|
# When all options are disabled, no docstring (instead of an empty one) should be generated
|
|
assert m.DocstringTestEnum5.__doc__ is None
|