Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
45 changes: 33 additions & 12 deletions Common/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3193,6 +3193,24 @@ public static PyList ToPyListUnSafe(this IEnumerable enumerable)
return pyList;
}

/// <summary>
/// Gets the <see cref="Type"/> from a <see cref="PyObject"/> that represents a C# type.
/// It throws an <see cref="ArgumentException"/> if the <see cref="PyObject"/> is not a C# type.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Type GetType(PyObject pyObject)
{
if (pyObject.TryConvert(out Type type))
{
return type;
}

using (Py.GIL())
{
throw new ArgumentException($"GetType(): {Messages.Extensions.ObjectFromPythonIsNotACSharpType(pyObject.Repr())}");
}
}

/// <summary>
/// Converts the numeric value of one or more enumerated constants to an equivalent enumerated string.
/// </summary>
Expand All @@ -3202,18 +3220,21 @@ public static PyList ToPyListUnSafe(this IEnumerable enumerable)
[Obsolete("Deprecated as of 2025-07. Please use `str()`.")]
public static string GetEnumString(this int value, PyObject pyObject)
{
Type type;
if (pyObject.TryConvert(out type))
{
return value.ToStringInvariant().ConvertTo(type).ToString();
}
else
{
using (Py.GIL())
{
throw new ArgumentException($"GetEnumString(): {Messages.Extensions.ObjectFromPythonIsNotACSharpType(pyObject.Repr())}");
}
}
var type = GetType(pyObject);
return value.ToStringInvariant().ConvertTo(type).ToString();
}

/// <summary>
/// Converts the numeric value of one or more enumerated constants to an equivalent enumerated string.
/// </summary>
/// <param name="value">Numeric value</param>
/// <param name="pyObject">Python object that encapsulated a Enum Type</param>
/// <returns>String that represents the enumerated object</returns>
[Obsolete("Deprecated as of 2025-07. Please use `str()`.")]
public static string GetEnumString(this Enum value, PyObject pyObject)
{
var type = GetType(pyObject);
return value.ToString();
}

/// <summary>
Expand Down
24 changes: 24 additions & 0 deletions Tests/Common/Util/ExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2089,6 +2089,30 @@ public void TryGetDecimalFromCsv_ReturnsDecimalValue(int index, decimal expected
Assert.AreEqual(expectedValue, result);
}

[Test]
public void GetsEnumStringInPython([Values] bool useIntValue)
{
using (Py.GIL())
{
var module = PyModule.FromString(
"GetsEnumStringInPython",
@"
from AlgorithmImports import *

def get_enum_string(value):
return Extensions.get_enum_string(value, Resolution)
"
);

using var getEnumString = module.GetAttr("get_enum_string");
var enumValue = Resolution.Minute;
using var pyEnumValue = useIntValue ? Convert.ToInt64(enumValue).ToPython() : enumValue.ToPython();
var enumString = getEnumString.Invoke(pyEnumValue).As<string>();

Assert.AreEqual(nameof(Resolution.Minute), enumString);
}
}

private PyObject ConvertToPyObject(object value)
{
using (Py.GIL())
Expand Down
Loading