Skip to content

PyFloats should not be implicitly rounded to integers #1342

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tminka opened this issue Dec 31, 2020 · 1 comment
Closed

PyFloats should not be implicitly rounded to integers #1342

tminka opened this issue Dec 31, 2020 · 1 comment

Comments

@tminka
Copy link
Contributor

tminka commented Dec 31, 2020

Environment

  • Pythonnet version: Latest master
  • Python version: 3.7.8
  • Operating System: Windows 10 64-bit
  • .NET Runtime: Core 3.1

Details

Incorrectly passing a float value to a method accepting an integer argument causes the float value to be rounded.

from Python.Test import MethodTest
MethodTest.TestOverloadedNoObject(5.5)

Surprisingly, the above code invokes the method with the integer 5. I expect this to raise an error instead, even if the value passed was 5.0. I expect a method with an integer argument to behave like list indexing in Python, which refuses any float value:

a = [1,2,3]
a[1.0]
TypeError: list indices must be integers or slices, not float
@tminka
Copy link
Contributor Author

tminka commented Jan 5, 2021

There is a similar problem with array indexing. The below test cases (extending test_array.py) fail by not raising an error:

def test_typed_array():
    ...
    with pytest.raises(TypeError):
        ob = Test.TypedArrayTest()
        _ = ob.items[0.5]

    with pytest.raises(TypeError):
        ob = Test.TypedArrayTest()
        ob.items[0.5] = Spam("0")

def test_multi_dimensional_array():
    ...
    with pytest.raises(TypeError):
        ob = Test.MultiDimensionalArrayTest()
        _ = ob.items[0.5, 0]

    with pytest.raises(TypeError):
        ob = Test.MultiDimensionalArrayTest()
        ob.items[0.5, 0] = 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant