Skip to content

Commit c283231

Browse files
authored
Improve error messages for unsupported Python objects in user-facing APIs (#9607)
* Improve error message when consolidator period Python object is unsupported * Make unsupported consolidator period error message more concise * Apply descriptive Python conversion errors to other user-facing sites and add QuoteBarConsolidator.FromResolution * List available overloads in unsupported Python argument error messages * Update QuantConnect.pythonnet to 2.0.61 * Update overload hint test expectations to Python-typed signatures * Exclude PyObject overloads from error hints and update to Python annotation style * Move PyObject overload filtering from consumers into MethodSignatureFormatter * Update Python alpha model name expectations for Python-style enum rendering
1 parent 046fb45 commit c283231

31 files changed

Lines changed: 255 additions & 30 deletions

‎Algorithm.CSharp/QuantConnect.Algorithm.CSharp.csproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<DebugType>portable</DebugType>
3333
</PropertyGroup>
3434
<ItemGroup>
35-
<PackageReference Include="QuantConnect.pythonnet" Version="2.0.60" />
35+
<PackageReference Include="QuantConnect.pythonnet" Version="2.0.61" />
3636
<PackageReference Include="Accord" Version="3.6.0" />
3737
<PackageReference Include="Accord.Fuzzy" Version="3.6.0" />
3838
<PackageReference Include="Accord.MachineLearning" Version="3.6.0" />

‎Algorithm.Framework/QuantConnect.Algorithm.Framework.csproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<PackageLicenseFile>LICENSE</PackageLicenseFile>
3030
</PropertyGroup>
3131
<ItemGroup>
32-
<PackageReference Include="QuantConnect.pythonnet" Version="2.0.60" />
32+
<PackageReference Include="QuantConnect.pythonnet" Version="2.0.61" />
3333
<PackageReference Include="Accord" Version="3.6.0" />
3434
<PackageReference Include="Accord.Math" Version="3.6.0" />
3535
<PackageReference Include="Accord.Statistics" Version="3.6.0" />

‎Algorithm.Framework/Selection/ScheduledUniverseSelectionModel.cs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,12 @@ public ScheduledUniverseSelectionModel(IDateRule dateRule, ITimeRule timeRule, P
9090
public ScheduledUniverseSelectionModel(DateTimeZone timeZone, IDateRule dateRule, ITimeRule timeRule, PyObject selector, UniverseSettings settings = null)
9191
{
9292
Func<DateTime, object> func;
93-
selector.TrySafeAs(out func);
93+
if (!selector.TrySafeAs(out func))
94+
{
95+
throw new ArgumentException(
96+
$"Unable to create a scheduled universe selector from {selector.ToDisplayString()}: it is not a function. " +
97+
"Please provide a function that takes the firing date time and returns the selected symbols.");
98+
}
9499
_timeZone = timeZone;
95100
_dateRule = dateRule;
96101
_timeRule = timeRule;

‎Algorithm.Python/QuantConnect.Algorithm.Python.csproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
<Compile Include="..\Common\Properties\SharedAssemblyInfo.cs" Link="Properties\SharedAssemblyInfo.cs" />
3838
</ItemGroup>
3939
<ItemGroup>
40-
<PackageReference Include="QuantConnect.pythonnet" Version="2.0.60" />
40+
<PackageReference Include="QuantConnect.pythonnet" Version="2.0.61" />
4141
</ItemGroup>
4242
<ItemGroup>
4343
<Content Include="OptionUniverseFilterGreeksShortcutsRegressionAlgorithm.py" />

‎Algorithm/QCAlgorithm.Python.cs‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1375,7 +1375,18 @@ public void SetBenchmark(PyObject benchmark)
13751375
SetBenchmark(pyBenchmark);
13761376
return;
13771377
}
1378-
SetBenchmark((Symbol)benchmark.AsManagedObject(typeof(Symbol)));
1378+
1379+
try
1380+
{
1381+
SetBenchmark((Symbol)benchmark.AsManagedObject(typeof(Symbol)));
1382+
}
1383+
catch (InvalidCastException exception)
1384+
{
1385+
throw new ArgumentException(
1386+
$"Unable to set the benchmark from {benchmark.ToDisplayString()}: it is not a supported benchmark type. " +
1387+
MethodSignatureFormatter.FormatOverloads(typeof(QCAlgorithm).GetMethods().Where(m => m.Name == nameof(SetBenchmark))),
1388+
exception);
1389+
}
13791390
}
13801391
}
13811392

‎Algorithm/QuantConnect.Algorithm.csproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<PackageLicenseFile>LICENSE</PackageLicenseFile>
3030
</PropertyGroup>
3131
<ItemGroup>
32-
<PackageReference Include="QuantConnect.pythonnet" Version="2.0.60" />
32+
<PackageReference Include="QuantConnect.pythonnet" Version="2.0.61" />
3333
<PackageReference Include="MathNet.Numerics" Version="5.0.0" />
3434
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
3535
<PackageReference Include="NodaTime" Version="3.0.5" />

‎AlgorithmFactory/QuantConnect.AlgorithmFactory.csproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<PackageLicenseFile>LICENSE</PackageLicenseFile>
2929
</PropertyGroup>
3030
<ItemGroup>
31-
<PackageReference Include="QuantConnect.pythonnet" Version="2.0.60" />
31+
<PackageReference Include="QuantConnect.pythonnet" Version="2.0.61" />
3232
<PackageReference Include="NodaTime" Version="3.0.5" />
3333
</ItemGroup>
3434
<ItemGroup>

‎Common/Data/Consolidators/BaseDataConsolidator.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace QuantConnect.Data.Consolidators
2525
public class BaseDataConsolidator : TradeBarConsolidatorBase<BaseData>
2626
{
2727
/// <summary>
28-
/// Create a new TickConsolidator for the desired resolution
28+
/// Create a new BaseDataConsolidator for the desired resolution
2929
/// </summary>
3030
/// <param name="resolution">The resolution desired</param>
3131
/// <returns>A consolidator that produces data on the resolution interval</returns>

‎Common/Data/Consolidators/PeriodCountConsolidatorBase.cs‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,20 @@ protected PeriodCountConsolidatorBase(Func<DateTime, CalendarInfo> func)
103103
/// </summary>
104104
/// <param name="pyObject">Python object that defines either a function object that defines the start time of a consolidated data or a timespan</param>
105105
protected PeriodCountConsolidatorBase(PyObject pyObject)
106-
: this(GetPeriodSpecificationFromPyObject(pyObject))
107106
{
107+
try
108+
{
109+
_periodSpecification = GetPeriodSpecificationFromPyObject(pyObject);
110+
}
111+
catch (InvalidCastException exception)
112+
{
113+
var type = GetType();
114+
throw new ArgumentException(
115+
$"Unable to create a consolidator period from {pyObject.ToDisplayString()}: it is not a supported period type. " +
116+
MethodSignatureFormatter.FormatOverloads(type.GetConstructors(), displayName: type.Name),
117+
exception);
118+
}
119+
_period = _periodSpecification.Period;
108120
}
109121

110122
/// <summary>

‎Common/Data/Consolidators/QuoteBarConsolidator.cs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ namespace QuantConnect.Data.Consolidators
2525
/// </summary>
2626
public class QuoteBarConsolidator : PeriodCountConsolidatorBase<QuoteBar, QuoteBar>
2727
{
28+
/// <summary>
29+
/// Create a new QuoteBarConsolidator for the desired resolution
30+
/// </summary>
31+
/// <param name="resolution">The resolution desired</param>
32+
/// <returns>A consolidator that produces data on the resolution interval</returns>
33+
public static QuoteBarConsolidator FromResolution(Resolution resolution)
34+
{
35+
return new QuoteBarConsolidator(resolution.ToTimeSpan());
36+
}
37+
2838
/// <summary>
2939
/// Initializes a new instance of the <see cref="QuoteBarConsolidator"/> class
3040
/// </summary>

0 commit comments

Comments
 (0)