diff --git a/Common/Extensions.cs b/Common/Extensions.cs
index aac449989a79..fda511dce500 100644
--- a/Common/Extensions.cs
+++ b/Common/Extensions.cs
@@ -3193,6 +3193,24 @@ public static PyList ToPyListUnSafe(this IEnumerable enumerable)
return pyList;
}
+ ///
+ /// Gets the from a that represents a C# type.
+ /// It throws an if the is not a C# type.
+ ///
+ [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())}");
+ }
+ }
+
///
/// Converts the numeric value of one or more enumerated constants to an equivalent enumerated string.
///
@@ -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();
+ }
+
+ ///
+ /// Converts the numeric value of one or more enumerated constants to an equivalent enumerated string.
+ ///
+ /// Numeric value
+ /// Python object that encapsulated a Enum Type
+ /// String that represents the enumerated object
+ [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();
}
///
diff --git a/Tests/Common/Util/ExtensionsTests.cs b/Tests/Common/Util/ExtensionsTests.cs
index 8601eadf07fa..ad67f0be34f1 100644
--- a/Tests/Common/Util/ExtensionsTests.cs
+++ b/Tests/Common/Util/ExtensionsTests.cs
@@ -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();
+
+ Assert.AreEqual(nameof(Resolution.Minute), enumString);
+ }
+ }
+
private PyObject ConvertToPyObject(object value)
{
using (Py.GIL())