feat(types): Use typing.SupportsInt and typing.SupportsFloat and fix other typing based bugs. (#5540)

* init

Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>

* remove import

Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>

* remove uneeded function

Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>

* style: pre-commit fixes

* Add missing import

Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>

* style: pre-commit fixes

* Fix type behind detailed_message_enabled flag

Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>

* Fix type behind detailed_message_enabled flag

Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>

* Add io_name comment

Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>

* Extra loops to single function

Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>

* style: pre-commit fixes

* Remove unneeded forward declaration

Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>

* Switch variable name away from macro

Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>

* Switch variable name away from macro

Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>

* Switch variable name away from macro

Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>

* clang-tidy

Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>

* remove stack import

* Fix bug in std::function Callable type

Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>

* style: pre-commit fixes

* remove is_annotation argument

Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>

* style: pre-commit fixes

* Update function name and arg names

Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>

* style: pre-commit fixes

---------

Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Michael Carlstrom
2025-03-18 07:56:34 -07:00
committed by GitHub
parent 16b5abd428
commit dfe7e65b45
19 changed files with 324 additions and 198 deletions

View File

@@ -251,7 +251,7 @@ def test_no_mixed_overloads():
"#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for more details"
if not detailed_error_messages_enabled
else "error while attempting to bind static method ExampleMandA.overload_mixed1"
"(arg0: float) -> str"
"(arg0: typing.SupportsFloat) -> str"
)
)
@@ -264,7 +264,7 @@ def test_no_mixed_overloads():
"#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for more details"
if not detailed_error_messages_enabled
else "error while attempting to bind instance method ExampleMandA.overload_mixed2"
"(self: pybind11_tests.methods_and_attributes.ExampleMandA, arg0: int, arg1: int)"
"(self: pybind11_tests.methods_and_attributes.ExampleMandA, arg0: typing.SupportsInt, arg1: typing.SupportsInt)"
" -> str"
)
)
@@ -479,7 +479,7 @@ def test_str_issue(msg):
msg(excinfo.value)
== """
__init__(): incompatible constructor arguments. The following argument types are supported:
1. m.methods_and_attributes.StrIssue(arg0: int)
1. m.methods_and_attributes.StrIssue(arg0: typing.SupportsInt)
2. m.methods_and_attributes.StrIssue()
Invoked with: 'no', 'such', 'constructor'
@@ -521,18 +521,22 @@ def test_overload_ordering():
assert m.overload_order("string") == 1
assert m.overload_order(0) == 4
assert "1. overload_order(arg0: int) -> int" in m.overload_order.__doc__
assert (
"1. overload_order(arg0: typing.SupportsInt) -> int" in m.overload_order.__doc__
)
assert "2. overload_order(arg0: str) -> int" in m.overload_order.__doc__
assert "3. overload_order(arg0: str) -> int" in m.overload_order.__doc__
assert "4. overload_order(arg0: int) -> int" in m.overload_order.__doc__
assert (
"4. overload_order(arg0: typing.SupportsInt) -> int" in m.overload_order.__doc__
)
with pytest.raises(TypeError) as err:
m.overload_order(1.1)
assert "1. (arg0: int) -> int" in str(err.value)
assert "1. (arg0: typing.SupportsInt) -> int" in str(err.value)
assert "2. (arg0: str) -> int" in str(err.value)
assert "3. (arg0: str) -> int" in str(err.value)
assert "4. (arg0: int) -> int" in str(err.value)
assert "4. (arg0: typing.SupportsInt) -> int" in str(err.value)
def test_rvalue_ref_param():