Change misleading error message while registering an indicator in Python (#7257)
Regression Tests / build (push) Has been cancelled
Python Virtual Environments / build (push) Has been cancelled
Benchmarks / build (push) Has been cancelled
Build & Test Lean / build (push) Has been cancelled
Research Regression Tests / build (push) Has been cancelled

* Solve the bug and add unit test

- The bug was raised because if something went wrong trying to register an indicator with a C# or a custom consolidator the exception raised was lost, because the catch sentence didn't return it with another exception

- A unit test was added asserting a exception with a related message was raised each time something went wrong with the consolidator

* Fix bugs and enhance unit tests

* Fix bug
This commit is contained in:
Ricardo Andrés Marino Rojas
2023-05-22 13:07:04 -05:00
committed by GitHub
parent c9dde8a0df
commit a043313004
2 changed files with 82 additions and 24 deletions
+25 -24
View File
@@ -576,41 +576,42 @@ namespace QuantConnect.Algorithm
[DocumentationAttribute(ConsolidatingData)]
public void RegisterIndicator(Symbol symbol, PyObject indicator, PyObject pyObject, PyObject selector = null)
{
try
// First check if this is just a regular IDataConsolidator
IDataConsolidator dataConsolidator;
if (pyObject.TryConvert(out dataConsolidator))
{
// First check if this is just a regular IDataConsolidator
IDataConsolidator dataConsolidator;
if (!pyObject.TryConvert(out dataConsolidator))
{
// If not then try and wrap it as a custom Python consolidator
dataConsolidator = new DataConsolidatorPythonWrapper(pyObject);
}
RegisterIndicator(symbol, indicator, dataConsolidator, selector);
return;
}
try
{
dataConsolidator = new DataConsolidatorPythonWrapper(pyObject);
}
catch
{
}
// Finally, since above didn't work, just try it as a timespan
// Issue #4668 Fix
using (Py.GIL())
{
try
// Finally, since above didn't work, just try it as a timespan
// Issue #4668 Fix
using (Py.GIL())
{
// tryConvert does not work for timespan
TimeSpan? timeSpan = pyObject.As<TimeSpan>();
if (timeSpan != default(TimeSpan))
try
{
RegisterIndicator(symbol, indicator, timeSpan, selector);
// tryConvert does not work for timespan
TimeSpan? timeSpan = pyObject.As<TimeSpan>();
if (timeSpan != default(TimeSpan))
{
RegisterIndicator(symbol, indicator, timeSpan, selector);
return;
}
}
catch (Exception e)
{
throw new ArgumentException("Invalid third argument, should be either a valid consolidator or timedelta object. The following exception was thrown: ", e);
}
}
catch
{
throw new ArgumentException("Invalid third argument, should be either a valid consolidator or timedelta object");
}
}
RegisterIndicator(symbol, indicator, dataConsolidator, selector);
}
/// <summary>