Skip to content

PyFloats should not be implicitly rounded to integers #1342

Closed

Description

@tminka

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

Activity

  1. added 3 commits that reference this issue on Dec 31, 2020
    9f93ae6
    7c4a6b5
    efe84c2
  2. tminka commented on Jan 5, 2021

    @tminka
    ContributorAuthor

    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
  3. added a commit that references this issue on Feb 5, 2024
    0f33f71
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions