Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions include/pybind11/cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,12 @@ struct type_caster<T, enable_if_t<std::is_arithmetic<T>::value && !is_std_char_t
return PyLong_FromUnsignedLongLong((unsigned long long) src);
}

PYBIND11_TYPE_CASTER(T,
io_name<std::is_integral<T>::value>(
"typing.SupportsInt", "int", "typing.SupportsFloat", "float"));
PYBIND11_TYPE_CASTER(
T,
io_name<std::is_integral<T>::value>("typing.SupportsInt | typing.SupportsIndex",
"int",
"typing.SupportsFloat | typing.SupportsIndex",
"float"));
};

template <typename T>
Expand Down
5 changes: 4 additions & 1 deletion include/pybind11/complex.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ class type_caster<std::complex<T>> {
return PyComplex_FromDoubles((double) src.real(), (double) src.imag());
}

PYBIND11_TYPE_CASTER(std::complex<T>, const_name("complex"));
PYBIND11_TYPE_CASTER(
std::complex<T>,
io_name("typing.SupportsComplex | typing.SupportsFloat | typing.SupportsIndex",
"complex"));
};
PYBIND11_NAMESPACE_END(detail)
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
12 changes: 9 additions & 3 deletions tests/test_builtin_casters.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,10 @@ def __int__(self):

convert, noconvert = m.int_passthrough, m.int_passthrough_noconvert

assert doc(convert) == "int_passthrough(arg0: typing.SupportsInt) -> int"
assert (
doc(convert)
== "int_passthrough(arg0: typing.SupportsInt | typing.SupportsIndex) -> int"
)
assert doc(noconvert) == "int_passthrough_noconvert(arg0: int) -> int"

def requires_conversion(v):
Expand Down Expand Up @@ -327,7 +330,10 @@ def __float__(self):
return 41.45

convert, noconvert = m.float_passthrough, m.float_passthrough_noconvert
assert doc(convert) == "float_passthrough(arg0: typing.SupportsFloat) -> float"
assert (
doc(convert)
== "float_passthrough(arg0: typing.SupportsFloat | typing.SupportsIndex) -> float"
)
assert doc(noconvert) == "float_passthrough_noconvert(arg0: float) -> float"

def requires_conversion(v):
Expand Down Expand Up @@ -381,7 +387,7 @@ def test_tuple(doc):
assert (
doc(m.tuple_passthrough)
== """
tuple_passthrough(arg0: tuple[bool, str, typing.SupportsInt]) -> tuple[int, str, bool]
tuple_passthrough(arg0: tuple[bool, str, typing.SupportsInt | typing.SupportsIndex]) -> tuple[int, str, bool]

Return a triple in reversed order
"""
Expand Down
4 changes: 2 additions & 2 deletions tests/test_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ def test_cpp_function_roundtrip():
def test_function_signatures(doc):
assert (
doc(m.test_callback3)
== "test_callback3(arg0: collections.abc.Callable[[typing.SupportsInt], int]) -> str"
== "test_callback3(arg0: collections.abc.Callable[[typing.SupportsInt | typing.SupportsIndex], int]) -> str"
)
assert (
doc(m.test_callback4)
== "test_callback4() -> collections.abc.Callable[[typing.SupportsInt], int]"
== "test_callback4() -> collections.abc.Callable[[typing.SupportsInt | typing.SupportsIndex], int]"
)


Expand Down
4 changes: 2 additions & 2 deletions tests/test_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,13 @@ def test_qualname(doc):
assert (
doc(m.NestBase.Nested.fn)
== """
fn(self: m.class_.NestBase.Nested, arg0: typing.SupportsInt, arg1: m.class_.NestBase, arg2: m.class_.NestBase.Nested) -> None
fn(self: m.class_.NestBase.Nested, arg0: typing.SupportsInt | typing.SupportsIndex, arg1: m.class_.NestBase, arg2: m.class_.NestBase.Nested) -> None
"""
)
assert (
doc(m.NestBase.Nested.fa)
== """
fa(self: m.class_.NestBase.Nested, a: typing.SupportsInt, b: m.class_.NestBase, c: m.class_.NestBase.Nested) -> None
fa(self: m.class_.NestBase.Nested, a: typing.SupportsInt | typing.SupportsIndex, b: m.class_.NestBase, c: m.class_.NestBase.Nested) -> None
"""
)
assert m.NestBase.__module__ == "pybind11_tests.class_"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_custom_type_casters.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def test_noconvert_args(msg):
msg(excinfo.value)
== """
ints_preferred(): incompatible function arguments. The following argument types are supported:
1. (i: typing.SupportsInt) -> int
1. (i: typing.SupportsInt | typing.SupportsIndex) -> int

Invoked with: 4.0
"""
Expand Down
6 changes: 3 additions & 3 deletions tests/test_docstring_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ def test_docstring_options():

# options.enable_function_signatures()
assert m.test_function3.__doc__.startswith(
"test_function3(a: typing.SupportsInt, b: typing.SupportsInt) -> None"
"test_function3(a: typing.SupportsInt | typing.SupportsIndex, b: typing.SupportsInt | typing.SupportsIndex) -> None"
)

assert m.test_function4.__doc__.startswith(
"test_function4(a: typing.SupportsInt, b: typing.SupportsInt) -> None"
"test_function4(a: typing.SupportsInt | typing.SupportsIndex, b: typing.SupportsInt | typing.SupportsIndex) -> None"
)
assert m.test_function4.__doc__.endswith("A custom docstring\n")

Expand All @@ -37,7 +37,7 @@ def test_docstring_options():

# RAII destructor
assert m.test_function7.__doc__.startswith(
"test_function7(a: typing.SupportsInt, b: typing.SupportsInt) -> None"
"test_function7(a: typing.SupportsInt | typing.SupportsIndex, b: typing.SupportsInt | typing.SupportsIndex) -> None"
)
assert m.test_function7.__doc__.endswith("A custom docstring\n")

Expand Down
2 changes: 1 addition & 1 deletion tests/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ def test_generated_dunder_methods_pos_only():
)
assert (
re.match(
r"^__setstate__\(self: [\w\.]+, state: [\w\.]+, /\)",
r"^__setstate__\(self: [\w\.]+, state: [\w\. \|]+, /\)",
enum_type.__setstate__.__doc__,
)
is not None
Expand Down
8 changes: 4 additions & 4 deletions tests/test_factory_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ def test_init_factory_signature(msg):
msg(excinfo.value)
== """
__init__(): incompatible constructor arguments. The following argument types are supported:
1. m.factory_constructors.TestFactory1(arg0: m.factory_constructors.tag.unique_ptr_tag, arg1: typing.SupportsInt)
1. m.factory_constructors.TestFactory1(arg0: m.factory_constructors.tag.unique_ptr_tag, arg1: typing.SupportsInt | typing.SupportsIndex)
2. m.factory_constructors.TestFactory1(arg0: str)
3. m.factory_constructors.TestFactory1(arg0: m.factory_constructors.tag.pointer_tag)
4. m.factory_constructors.TestFactory1(arg0: object, arg1: typing.SupportsInt, arg2: object)
4. m.factory_constructors.TestFactory1(arg0: object, arg1: typing.SupportsInt | typing.SupportsIndex, arg2: object)

Invoked with: 'invalid', 'constructor', 'arguments'
"""
Expand All @@ -93,13 +93,13 @@ def test_init_factory_signature(msg):
__init__(*args, **kwargs)
Overloaded function.

1. __init__(self: m.factory_constructors.TestFactory1, arg0: m.factory_constructors.tag.unique_ptr_tag, arg1: typing.SupportsInt) -> None
1. __init__(self: m.factory_constructors.TestFactory1, arg0: m.factory_constructors.tag.unique_ptr_tag, arg1: typing.SupportsInt | typing.SupportsIndex) -> None

2. __init__(self: m.factory_constructors.TestFactory1, arg0: str) -> None

3. __init__(self: m.factory_constructors.TestFactory1, arg0: m.factory_constructors.tag.pointer_tag) -> None

4. __init__(self: m.factory_constructors.TestFactory1, arg0: object, arg1: typing.SupportsInt, arg2: object) -> None
4. __init__(self: m.factory_constructors.TestFactory1, arg0: object, arg1: typing.SupportsInt | typing.SupportsIndex, arg2: object) -> None
"""
)

Expand Down
48 changes: 24 additions & 24 deletions tests/test_kwargs_and_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@
def test_function_signatures(doc):
assert (
doc(m.kw_func0)
== "kw_func0(arg0: typing.SupportsInt, arg1: typing.SupportsInt) -> str"
== "kw_func0(arg0: typing.SupportsInt | typing.SupportsIndex, arg1: typing.SupportsInt | typing.SupportsIndex) -> str"
)
assert (
doc(m.kw_func1)
== "kw_func1(x: typing.SupportsInt, y: typing.SupportsInt) -> str"
== "kw_func1(x: typing.SupportsInt | typing.SupportsIndex, y: typing.SupportsInt | typing.SupportsIndex) -> str"
)
assert (
doc(m.kw_func2)
== "kw_func2(x: typing.SupportsInt = 100, y: typing.SupportsInt = 200) -> str"
== "kw_func2(x: typing.SupportsInt | typing.SupportsIndex = 100, y: typing.SupportsInt | typing.SupportsIndex = 200) -> str"
)
assert doc(m.kw_func3) == "kw_func3(data: str = 'Hello world!') -> None"
assert (
doc(m.kw_func4)
== "kw_func4(myList: collections.abc.Sequence[typing.SupportsInt] = [13, 17]) -> str"
== "kw_func4(myList: collections.abc.Sequence[typing.SupportsInt | typing.SupportsIndex] = [13, 17]) -> str"
)
assert (
doc(m.kw_func_udl)
== "kw_func_udl(x: typing.SupportsInt, y: typing.SupportsInt = 300) -> str"
== "kw_func_udl(x: typing.SupportsInt | typing.SupportsIndex, y: typing.SupportsInt | typing.SupportsIndex = 300) -> str"
)
assert (
doc(m.kw_func_udl_z)
== "kw_func_udl_z(x: typing.SupportsInt, y: typing.SupportsInt = 0) -> str"
== "kw_func_udl_z(x: typing.SupportsInt | typing.SupportsIndex, y: typing.SupportsInt | typing.SupportsIndex = 0) -> str"
)
assert doc(m.args_function) == "args_function(*args) -> tuple"
assert (
Expand All @@ -42,11 +42,11 @@ def test_function_signatures(doc):
)
assert (
doc(m.KWClass.foo0)
== "foo0(self: m.kwargs_and_defaults.KWClass, arg0: typing.SupportsInt, arg1: typing.SupportsFloat) -> None"
== "foo0(self: m.kwargs_and_defaults.KWClass, arg0: typing.SupportsInt | typing.SupportsIndex, arg1: typing.SupportsFloat | typing.SupportsIndex) -> None"
)
assert (
doc(m.KWClass.foo1)
== "foo1(self: m.kwargs_and_defaults.KWClass, x: typing.SupportsInt, y: typing.SupportsFloat) -> None"
== "foo1(self: m.kwargs_and_defaults.KWClass, x: typing.SupportsInt | typing.SupportsIndex, y: typing.SupportsFloat | typing.SupportsIndex) -> None"
)
assert (
doc(m.kw_lb_func0)
Expand Down Expand Up @@ -138,7 +138,7 @@ def test_mixed_args_and_kwargs(msg):
msg(excinfo.value)
== """
mixed_plus_args(): incompatible function arguments. The following argument types are supported:
1. (arg0: typing.SupportsInt, arg1: typing.SupportsFloat, *args) -> tuple
1. (arg0: typing.SupportsInt | typing.SupportsIndex, arg1: typing.SupportsFloat | typing.SupportsIndex, *args) -> tuple

Invoked with: 1
"""
Expand All @@ -149,7 +149,7 @@ def test_mixed_args_and_kwargs(msg):
msg(excinfo.value)
== """
mixed_plus_args(): incompatible function arguments. The following argument types are supported:
1. (arg0: typing.SupportsInt, arg1: typing.SupportsFloat, *args) -> tuple
1. (arg0: typing.SupportsInt | typing.SupportsIndex, arg1: typing.SupportsFloat | typing.SupportsIndex, *args) -> tuple

Invoked with:
"""
Expand Down Expand Up @@ -183,7 +183,7 @@ def test_mixed_args_and_kwargs(msg):
msg(excinfo.value)
== """
mixed_plus_args_kwargs_defaults(): incompatible function arguments. The following argument types are supported:
1. (i: typing.SupportsInt = 1, j: typing.SupportsFloat = 3.14159, *args, **kwargs) -> tuple
1. (i: typing.SupportsInt | typing.SupportsIndex = 1, j: typing.SupportsFloat | typing.SupportsIndex = 3.14159, *args, **kwargs) -> tuple

Invoked with: 1; kwargs: i=1
"""
Expand All @@ -194,7 +194,7 @@ def test_mixed_args_and_kwargs(msg):
msg(excinfo.value)
== """
mixed_plus_args_kwargs_defaults(): incompatible function arguments. The following argument types are supported:
1. (i: typing.SupportsInt = 1, j: typing.SupportsFloat = 3.14159, *args, **kwargs) -> tuple
1. (i: typing.SupportsInt | typing.SupportsIndex = 1, j: typing.SupportsFloat | typing.SupportsIndex = 3.14159, *args, **kwargs) -> tuple

Invoked with: 1, 2; kwargs: j=1
"""
Expand All @@ -211,7 +211,7 @@ def test_mixed_args_and_kwargs(msg):
msg(excinfo.value)
== """
args_kwonly(): incompatible function arguments. The following argument types are supported:
1. (i: typing.SupportsInt, j: typing.SupportsFloat, *args, z: typing.SupportsInt) -> tuple
1. (i: typing.SupportsInt | typing.SupportsIndex, j: typing.SupportsFloat | typing.SupportsIndex, *args, z: typing.SupportsInt | typing.SupportsIndex) -> tuple

Invoked with: 2, 2.5, 22
"""
Expand All @@ -233,12 +233,12 @@ def test_mixed_args_and_kwargs(msg):
)
assert (
m.args_kwonly_kwargs.__doc__
== "args_kwonly_kwargs(i: typing.SupportsInt, j: typing.SupportsFloat, *args, z: typing.SupportsInt, **kwargs) -> tuple\n"
== "args_kwonly_kwargs(i: typing.SupportsInt | typing.SupportsIndex, j: typing.SupportsFloat | typing.SupportsIndex, *args, z: typing.SupportsInt | typing.SupportsIndex, **kwargs) -> tuple\n"
)

assert (
m.args_kwonly_kwargs_defaults.__doc__
== "args_kwonly_kwargs_defaults(i: typing.SupportsInt = 1, j: typing.SupportsFloat = 3.14159, *args, z: typing.SupportsInt = 42, **kwargs) -> tuple\n"
== "args_kwonly_kwargs_defaults(i: typing.SupportsInt | typing.SupportsIndex = 1, j: typing.SupportsFloat | typing.SupportsIndex = 3.14159, *args, z: typing.SupportsInt | typing.SupportsIndex = 42, **kwargs) -> tuple\n"
)
assert m.args_kwonly_kwargs_defaults() == (1, 3.14159, (), 42, {})
assert m.args_kwonly_kwargs_defaults(2) == (2, 3.14159, (), 42, {})
Expand Down Expand Up @@ -294,11 +294,11 @@ def test_keyword_only_args(msg):
x.method(i=1, j=2)
assert (
m.first_arg_kw_only.__init__.__doc__
== "__init__(self: pybind11_tests.kwargs_and_defaults.first_arg_kw_only, *, i: typing.SupportsInt = 0) -> None\n"
== "__init__(self: pybind11_tests.kwargs_and_defaults.first_arg_kw_only, *, i: typing.SupportsInt | typing.SupportsIndex = 0) -> None\n"
)
assert (
m.first_arg_kw_only.method.__doc__
== "method(self: pybind11_tests.kwargs_and_defaults.first_arg_kw_only, *, i: typing.SupportsInt = 1, j: typing.SupportsInt = 2) -> None\n"
== "method(self: pybind11_tests.kwargs_and_defaults.first_arg_kw_only, *, i: typing.SupportsInt | typing.SupportsIndex = 1, j: typing.SupportsInt | typing.SupportsIndex = 2) -> None\n"
)


Expand Down Expand Up @@ -344,7 +344,7 @@ def test_positional_only_args():
# Mix it with args and kwargs:
assert (
m.args_kwonly_full_monty.__doc__
== "args_kwonly_full_monty(arg0: typing.SupportsInt = 1, arg1: typing.SupportsInt = 2, /, j: typing.SupportsFloat = 3.14159, *args, z: typing.SupportsInt = 42, **kwargs) -> tuple\n"
== "args_kwonly_full_monty(arg0: typing.SupportsInt | typing.SupportsIndex = 1, arg1: typing.SupportsInt | typing.SupportsIndex = 2, /, j: typing.SupportsFloat | typing.SupportsIndex = 3.14159, *args, z: typing.SupportsInt | typing.SupportsIndex = 42, **kwargs) -> tuple\n"
)
assert m.args_kwonly_full_monty() == (1, 2, 3.14159, (), 42, {})
assert m.args_kwonly_full_monty(8) == (8, 2, 3.14159, (), 42, {})
Expand Down Expand Up @@ -387,30 +387,30 @@ def test_positional_only_args():
# https://github.com/pybind/pybind11/pull/3402#issuecomment-963341987
assert (
m.first_arg_kw_only.pos_only.__doc__
== "pos_only(self: pybind11_tests.kwargs_and_defaults.first_arg_kw_only, /, i: typing.SupportsInt, j: typing.SupportsInt) -> None\n"
== "pos_only(self: pybind11_tests.kwargs_and_defaults.first_arg_kw_only, /, i: typing.SupportsInt | typing.SupportsIndex, j: typing.SupportsInt | typing.SupportsIndex) -> None\n"
)


def test_signatures():
assert (
m.kw_only_all.__doc__
== "kw_only_all(*, i: typing.SupportsInt, j: typing.SupportsInt) -> tuple\n"
== "kw_only_all(*, i: typing.SupportsInt | typing.SupportsIndex, j: typing.SupportsInt | typing.SupportsIndex) -> tuple\n"
)
assert (
m.kw_only_mixed.__doc__
== "kw_only_mixed(i: typing.SupportsInt, *, j: typing.SupportsInt) -> tuple\n"
== "kw_only_mixed(i: typing.SupportsInt | typing.SupportsIndex, *, j: typing.SupportsInt | typing.SupportsIndex) -> tuple\n"
)
assert (
m.pos_only_all.__doc__
== "pos_only_all(i: typing.SupportsInt, j: typing.SupportsInt, /) -> tuple\n"
== "pos_only_all(i: typing.SupportsInt | typing.SupportsIndex, j: typing.SupportsInt | typing.SupportsIndex, /) -> tuple\n"
)
assert (
m.pos_only_mix.__doc__
== "pos_only_mix(i: typing.SupportsInt, /, j: typing.SupportsInt) -> tuple\n"
== "pos_only_mix(i: typing.SupportsInt | typing.SupportsIndex, /, j: typing.SupportsInt | typing.SupportsIndex) -> tuple\n"
)
assert (
m.pos_kw_only_mix.__doc__
== "pos_kw_only_mix(i: typing.SupportsInt, /, j: typing.SupportsInt, *, k: typing.SupportsInt) -> tuple\n"
== "pos_kw_only_mix(i: typing.SupportsInt | typing.SupportsIndex, /, j: typing.SupportsInt | typing.SupportsIndex, *, k: typing.SupportsInt | typing.SupportsIndex) -> tuple\n"
)


Expand Down
20 changes: 13 additions & 7 deletions tests/test_methods_and_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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: typing.SupportsFloat) -> str"
"(arg0: typing.SupportsFloat | typing.SupportsIndex) -> str"
)
)

Expand All @@ -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: typing.SupportsInt, arg1: typing.SupportsInt)"
"(self: pybind11_tests.methods_and_attributes.ExampleMandA, arg0: typing.SupportsInt | typing.SupportsIndex, arg1: typing.SupportsInt | typing.SupportsIndex)"
" -> str"
)
)
Expand Down Expand Up @@ -491,7 +491,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: typing.SupportsInt)
1. m.methods_and_attributes.StrIssue(arg0: typing.SupportsInt | typing.SupportsIndex)
2. m.methods_and_attributes.StrIssue()

Invoked with: 'no', 'such', 'constructor'
Expand Down Expand Up @@ -534,21 +534,27 @@ def test_overload_ordering():
assert m.overload_order(0) == 4

assert (
"1. overload_order(arg0: typing.SupportsInt) -> int" in m.overload_order.__doc__
"1. overload_order(arg0: typing.SupportsInt | typing.SupportsIndex) -> 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: typing.SupportsInt) -> int" in m.overload_order.__doc__
"4. overload_order(arg0: typing.SupportsInt | typing.SupportsIndex) -> int"
in m.overload_order.__doc__
)

with pytest.raises(TypeError) as err:
m.overload_order(1.1)

assert "1. (arg0: typing.SupportsInt) -> int" in str(err.value)
assert "1. (arg0: typing.SupportsInt | typing.SupportsIndex) -> 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: typing.SupportsInt) -> int" in str(err.value)
assert "4. (arg0: typing.SupportsInt | typing.SupportsIndex) -> int" in str(
err.value
)


def test_rvalue_ref_param():
Expand Down
Loading
Loading