Skip to content

Allow conversion of number to enum type when underlying type is assignable #1179

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
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Allow conversion of number to enum type when assignable
In .NET you can do the following:
```
enum FooEnum {
	One = 1,
	Three = 3,
	Four = 4,
}

FooEnum x = (FooEnum)2;
```

This patch allows this from Python code as well.
  • Loading branch information
slide committed Jul 6, 2020
commit b6da4fe4a066f88b7bd60d951d30ad61524ae4fd
10 changes: 10 additions & 0 deletions src/runtime/converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,16 @@ private static bool ToEnum(IntPtr value, Type obType, out object result, bool se
return true;
}

// .NET allows you to assign a value to an enum
// even if that value is not defined in the enum
// as long as it is assignable to the underlying
// type.
if(etype.IsAssignableFrom(result.GetType()))
{
result = Enum.ToObject(obType, result);
return true;
}

if (setError)
{
Exceptions.SetError(Exceptions.ValueError, "invalid enumeration value");
Expand Down
14 changes: 13 additions & 1 deletion src/testing/methodtest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ public TypeCode TestEnumConversion(TypeCode v)
return v;
}

public enum TestEnum : int
{
One = 1,
Three = 3,
Four = 4,
}

public TestEnum TestEnumValueTypeConversion(TestEnum v)
{
return v;
}

public FileAccess TestFlagsConversion(FileAccess v)
{
return v;
Expand Down Expand Up @@ -683,7 +695,7 @@ public static string OptionalAndDefaultParams2([Optional]int a, [Optional]int b,
return string.Format("{0}{1}{2}{3}", a, b, c, d);
}


}


Expand Down
6 changes: 3 additions & 3 deletions src/tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,9 +679,9 @@ def test_enum_array():
items[-1] = ShortEnum.Zero
assert items[-1] == ShortEnum.Zero

with pytest.raises(ValueError):
ob = Test.EnumArrayTest()
ob.items[0] = 99
ob = Test.EnumArrayTest()
ob.items[0] = 99
assert ob.items[0] == 99

with pytest.raises(TypeError):
ob = Test.EnumArrayTest()
Expand Down
12 changes: 6 additions & 6 deletions src/tests/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,13 +616,13 @@ def test_enum_conversion():
assert ob.EnumField == ShortEnum.One
assert ob.EnumField == 1

with pytest.raises(ValueError):
ob = ConversionTest()
ob.EnumField = 10
ob = ConversionTest()
ob.EnumField = 10
assert ob.EnumField == 10

with pytest.raises(ValueError):
ob = ConversionTest()
ob.EnumField = 255
ob = ConversionTest()
ob.EnumField = 255
assert ob.EnumField == 255

with pytest.raises(OverflowError):
ob = ConversionTest()
Expand Down
9 changes: 4 additions & 5 deletions src/tests/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,8 @@ def test_enum_with_flags_attr_conversion():
# This works because the FlagsField enum has FlagsAttribute.
Test.FieldTest().FlagsField = 99

# This should fail because our test enum doesn't have it.
with pytest.raises(ValueError):
Test.FieldTest().EnumField = 99
# This works because .NET allows assigning the underlying type
Test.FieldTest().EnumField = 99


def test_enum_conversion():
Expand All @@ -135,8 +134,8 @@ def test_enum_conversion():
ob.EnumField = Test.ShortEnum.One
assert ob.EnumField == 1

with pytest.raises(ValueError):
Test.FieldTest().EnumField = 20
ob.EnumField = 20
assert ob.EnumField == 20

with pytest.raises(OverflowError):
Test.FieldTest().EnumField = 100000
Expand Down
15 changes: 15 additions & 0 deletions src/tests/test_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,21 @@ def test_method_call_enum_conversion():
r = ob.TestEnumConversion(TypeCode.Int32)
assert r == TypeCode.Int32

def test_method_call_enum_valuetype_conversion():
"""Test enum which inherits from a value type
gets converted to enum in method call"""
ob = MethodTest()
r = ob.TestEnumValueTypeConversion(1)
assert r == MethodTest.TestEnum.One

r = ob.TestEnumValueTypeConversion(3)
assert r == MethodTest.TestEnum.Three

r = ob.TestEnumValueTypeConversion(2)
assert r == 2

with pytest.raises(TypeError):
r = ob.TestEnumValueTypeConversion('Hi')

def test_method_call_flags_conversion():
"""Test flags conversion in method call."""
Expand Down