Skip to content

Commit

Permalink
test(enum): add test for for #5020: silent conversion to float
Browse files Browse the repository at this point in the history
Enums (including enum class) are silently converted to floats during
method calls. This can cause overload resolution to fail.

I believe this behavior is a bug. This commit adds tests for that
behavior. These tests fail right now, but they should pass once the
behavior is fixed.

See: #5020
  • Loading branch information
codeinred committed Feb 14, 2024
1 parent 8b48ff8 commit 891b0a1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
10 changes: 10 additions & 0 deletions tests/test_enum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ TEST_SUBMODULE(enums, m) {
m.def("test_enum_to_uint", [](uint32_t) {});
m.def("test_enum_to_long_long", [](long long) {});

// Trying to pass an enum to a function that accepts a float should
// trigger a type error
m.def("test_enum_to_float", [](double) {});

// When performing overload resolution, calling f(0, ScopedEnum.TWO)
// should select f(float, ScopedEnum), NOT f(float, float)
m.def("test_enum_overload_resolution", [](double, double) { return "f(float, float)"; });
m.def("test_enum_overload_resolution",
[](double, ScopedEnum) { return "f(float, ScopedEnum)"; });

// test_duplicate_enum_name
enum SimpleEnum { ONE, TWO, THREE };

Expand Down
22 changes: 22 additions & 0 deletions tests/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,28 @@ def test_enum_to_int():
m.test_enum_to_long_long(m.ScopedBoolEnum.TRUE)


def test_enum_overload_resolution():
"""When performing overload resolution, enums should not be silently
converted to floats"""
assert m.test_enum_overload_resolution(0.0, 0.0) == "f(float, float)"
assert m.test_enum_overload_resolution(0, 0) == "f(float, float)"
assert (
m.test_enum_overload_resolution(0.0, m.ScopedEnum.Two) == "f(float, ScopedEnum)"
)
assert (

Check failure on line 240 in tests/test_enum.py

View workflow job for this annotation

GitHub Actions / 🐍 3.9 β€’ ubuntu-20.04 β€’ x64

test_enum_overload_resolution AssertionError: assert 'f(float, float)' == 'f(float, ScopedEnum)' - f(float, ScopedEnum) + f(float, float)

Check failure on line 240 in tests/test_enum.py

View workflow job for this annotation

GitHub Actions / 🐍 3.10 β€’ ubuntu-20.04 β€’ x64

test_enum_overload_resolution AssertionError: assert 'f(float, float)' == 'f(float, ScopedEnum)' - f(float, ScopedEnum) + f(float, float)

Check failure on line 240 in tests/test_enum.py

View workflow job for this annotation

GitHub Actions / 🐍 3.12 β€’ ubuntu-20.04 β€’ x64

test_enum_overload_resolution AssertionError: assert 'f(float, float)' == 'f(float, ScopedEnum)' - f(float, ScopedEnum) + f(float, float)

Check failure on line 240 in tests/test_enum.py

View workflow job for this annotation

GitHub Actions / 🐍 3.11 β€’ ubuntu-20.04 β€’ x64

test_enum_overload_resolution AssertionError: assert 'f(float, float)' == 'f(float, ScopedEnum)' - f(float, ScopedEnum) + f(float, float)

Check failure on line 240 in tests/test_enum.py

View workflow job for this annotation

GitHub Actions / 🐍 pypy-3.8 β€’ ubuntu-20.04 β€’ x64 -DPYBIND11_FINDPYTHON=ON

test_enum_overload_resolution AssertionError: assert 'f(float, float)' == 'f(float, ScopedEnum)' - f(float, ScopedEnum) + f(float, float)

Check failure on line 240 in tests/test_enum.py

View workflow job for this annotation

GitHub Actions / 🐍 pypy-3.9 β€’ ubuntu-20.04 β€’ x64

test_enum_overload_resolution AssertionError: assert 'f(float, float)' == 'f(float, ScopedEnum)' - f(float, ScopedEnum) + f(float, float)
m.test_enum_overload_resolution(0, m.ScopedEnum.Two) == "f(float, ScopedEnum)"
)


def test_enum_to_float():
"""Passing an enum to a function taking a float should trigger a type error"""
with pytest.raises(TypeError) as execinfo:

Check failure on line 247 in tests/test_enum.py

View workflow job for this annotation

GitHub Actions / 🐍 3.10 β€’ ubuntu-20.04 β€’ x64

test_enum_to_float Failed: DID NOT RAISE <class 'TypeError'>

Check failure on line 247 in tests/test_enum.py

View workflow job for this annotation

GitHub Actions / 🐍 3.12 β€’ ubuntu-20.04 β€’ x64

test_enum_to_float Failed: DID NOT RAISE <class 'TypeError'>

Check failure on line 247 in tests/test_enum.py

View workflow job for this annotation

GitHub Actions / 🐍 3.11 β€’ ubuntu-20.04 β€’ x64

test_enum_to_float Failed: DID NOT RAISE <class 'TypeError'>
m.test_enum_to_float(m.ScopedBoolEnum.TRUE)

Check failure on line 248 in tests/test_enum.py

View workflow job for this annotation

GitHub Actions / 🐍 3.9 β€’ ubuntu-20.04 β€’ x64

test_enum_to_float Failed: DID NOT RAISE <class 'TypeError'>

Check failure on line 248 in tests/test_enum.py

View workflow job for this annotation

GitHub Actions / 🐍 pypy-3.8 β€’ ubuntu-20.04 β€’ x64 -DPYBIND11_FINDPYTHON=ON

test_enum_to_float Failed: DID NOT RAISE <class 'TypeError'>

Check failure on line 248 in tests/test_enum.py

View workflow job for this annotation

GitHub Actions / 🐍 pypy-3.9 β€’ ubuntu-20.04 β€’ x64

test_enum_to_float Failed: DID NOT RAISE <class 'TypeError'>
assert str(execinfo.value).startswith(

Check failure on line 249 in tests/test_enum.py

View workflow job for this annotation

GitHub Actions / 🐍 3.6 β€’ ubuntu-20.04 β€’ x64 -DPYBIND11_FINDPYTHON=ON -DCMAKE_CXX_FLAGS="-D_=1"

test_enum_to_float AssertionError: assert False + where False = <built-in method startswith of str object at 0x7f7da7459108>('TypeError: test_enum_to_float(): incompatible function arguments.') + where <built-in method startswith of str object at 0x7f7da7459108> = 'test_enum_to_float(): incompatible function arguments. The following argument types are supported:\n 1. (arg0: float) -> None\n\nInvoked with: <ScopedBoolEnum.TRUE: 1>'.startswith + where 'test_enum_to_float(): incompatible function arguments. The following argument types are supported:\n 1. (arg0: float) -> None\n\nInvoked with: <ScopedBoolEnum.TRUE: 1>' = str(TypeError('test_enum_to_float(): incompatible function arguments. The following argument types are supported:\n 1. (arg0: float) -> None\n\nInvoked with: <ScopedBoolEnum.TRUE: 1>',)) + where TypeError('test_enum_to_float(): incompatible function arguments. The following argument types are supported:\n 1. (arg0: float) -> None\n\nInvoked with: <ScopedBoolEnum.TRUE: 1>',) = <ExceptionInfo TypeError('test_enum_to_float(): incompatible function arguments. The following argument types are supported:\n 1. (arg0: float) -> None\n\nInvoked with: <ScopedBoolEnum.TRUE: 1>',) tblen=1>.value

Check failure on line 249 in tests/test_enum.py

View workflow job for this annotation

GitHub Actions / 🐍 pypy-3.10 β€’ ubuntu-20.04 β€’ x64

test_enum_to_float AssertionError: assert False + where False = <bound method str.startswith of 'test_enum_to_float(): incompatible function arguments. The following argument types are supported:\n 1. (arg0: float) -> None\n\nInvoked with: <ScopedBoolEnum.TRUE: 1>'>('TypeError: test_enum_to_float(): incompatible function arguments.') + where <bound method str.startswith of 'test_enum_to_float(): incompatible function arguments. The following argument types are supported:\n 1. (arg0: float) -> None\n\nInvoked with: <ScopedBoolEnum.TRUE: 1>'> = 'test_enum_to_float(): incompatible function arguments. The following argument types are supported:\n 1. (arg0: float) -> None\n\nInvoked with: <ScopedBoolEnum.TRUE: 1>'.startswith + where 'test_enum_to_float(): incompatible function arguments. The following argument types are supported:\n 1. (arg0: float) -> None\n\nInvoked with: <ScopedBoolEnum.TRUE: 1>' = str(TypeError('test_enum_to_float(): incompatible function arguments. The following argument types are supported:\n 1. (arg0: float) -> None\n\nInvoked with: <ScopedBoolEnum.TRUE: 1>')) + where TypeError('test_enum_to_float(): incompatible function arguments. The following argument types are supported:\n 1. (arg0: float) -> None\n\nInvoked with: <ScopedBoolEnum.TRUE: 1>') = <ExceptionInfo TypeError('test_enum_to_float(): incompatible function arguments. The following argument types are supported:\n 1. (arg0: float) -> None\n\nInvoked with: <ScopedBoolEnum.TRUE: 1>') tblen=1>.value
"TypeError: test_enum_to_float(): incompatible function arguments."
)


def test_duplicate_enum_name():
with pytest.raises(ValueError) as excinfo:
m.register_bad_enum()
Expand Down

0 comments on commit 891b0a1

Please sign in to comment.