@@ -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