Skip to content

Commit 390ef67

Browse files
committed
Improve helper method
1 parent c54095f commit 390ef67

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

Common/Securities/Security.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ public void SetMarginModel(PyObject pyObject)
861861
/// <param name="pyObject">Python class that represents a custom shortable provider</param>
862862
public void SetShortableProvider(PyObject pyObject)
863863
{
864-
ShortableProvider = PythonUtil.CreateModelOrWrapper<IShortableProvider, ShortableProviderPythonWrapper>(pyObject);
864+
ShortableProvider = PythonUtil.CreateModelOrWrapper<IShortableProvider, ShortableProviderPythonWrapper>(pyObject, true);
865865
}
866866

867867
/// <summary>
@@ -880,7 +880,7 @@ public void SetShortableProvider(IShortableProvider shortableProvider)
880880
/// <exception cref="ArgumentException"></exception>
881881
public void SetDataFilter(PyObject pyObject)
882882
{
883-
DataFilter = PythonUtil.CreateModelOrWrapper<ISecurityDataFilter, SecurityDataFilterPythonWrapper>(pyObject);
883+
DataFilter = PythonUtil.CreateModelOrWrapper<ISecurityDataFilter, SecurityDataFilterPythonWrapper>(pyObject, true);
884884
}
885885

886886
/// <summary>

Common/Util/PythonUtil.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -368,27 +368,31 @@ public static IEnumerable<Symbol> ConvertToSymbols(PyObject input)
368368
/// <typeparam name="TInterface">The interface type expected</typeparam>
369369
/// <typeparam name="TWrapper">The Python wrapper type for TInterface</typeparam>
370370
/// <param name="pyObject">The Python object to convert</param>
371+
/// <param name="requiresImplementation">True to require Python objects to inherit/implement the model</param>
371372
/// <returns>Either a pure C# instance or a Python wrapper implementing TInterface</returns>
372373
/// <exception cref="ArgumentException">Thrown when pyObject is not a valid TInterface</exception>
373-
public static TInterface CreateModelOrWrapper<TInterface, TWrapper>(PyObject pyObject)
374+
public static TInterface CreateModelOrWrapper<TInterface, TWrapper>(
375+
PyObject pyObject,
376+
bool requiresImplementation = false)
374377
where TInterface : class
375378
where TWrapper : TInterface
376379
{
377380
using (Py.GIL())
378381
{
382+
// This is a pure C# object
379383
if (pyObject.TryConvert<TInterface>(out var model))
380384
{
381-
// This object is pure C#
382385
return model;
383386
}
384387

385-
if (Extensions.TryConvert<TInterface>(pyObject, out _, allowPythonDerivative: true))
388+
// If we require the object to implement the model
389+
if (requiresImplementation && !Extensions.TryConvert<TInterface>(pyObject, out _, allowPythonDerivative: true))
386390
{
387-
// Create the appropriate Python wrapper
388-
return (TInterface)Activator.CreateInstance(typeof(TWrapper), pyObject);
391+
throw new ArgumentException($"{typeof(TInterface).Name}: {pyObject.Repr()} is not a valid argument");
389392
}
390393

391-
throw new ArgumentException($"Invalid argument: {pyObject.Repr()} is not a valid {typeof(TInterface).Name}");
394+
// Create the appropriate Python wrapper
395+
return (TInterface)Activator.CreateInstance(typeof(TWrapper), pyObject);
392396
}
393397
}
394398
}

0 commit comments

Comments
 (0)