6
votes

I'm using Python.NET to load a C# Assembly to call C# code from Python. This works pretty cleanly, however I am having an issue calling a method that looks like this:

A method within Our.Namespace.Proj.MyRepo:

OutputObject GetData(string user, int anID, int? anOptionalID= null)

I can call the method for the case where the optional third argument is present but can't figure out what to pass for the third argument to match the null case.

import clr
clr.AddReference("Our.Namespace.Proj")
import System
from Our.Namespace.Proj import MyRepo

_repo = MyRepo()

_repo.GetData('me', System.Int32(1), System.Int32(2))  # works!

_repo.GetData('me', System.Int32(1))  # fails! TypeError: No method matches given arguments

_repo.GetData('me', System.Int32(1), None)  # fails! TypeError: No method matches given arguments

The iPython Notebook indicates that the last argument should be of type:

System.Nullable`1[System.Int32]

Just not sure how to create an object that will match the Null case.

Any suggestions on how to create a C# recognized Null object? I assumed passing the native Python None would work, but it does not.

3
I don't know Python.NET, but it seems, when you pass None to it, it probably only searches methods with reference type parameters. You could at least try an overload without the last parameter (and possible another overload with the last parameter being object to allow passing None (if this works) that then calls the first method with the last argument as int?.) - IS4
Ended up refactoring the C# side to have overloaded methods (one with and one without the third parameter). Python.NET handles this fine. The downside is I have to call one of two methods conditionally based on presence of the last parameter. I will try the suggestion of having the last parameter be object for a third overloaded case on the C# side to allow passing None. Thanks! - skulz00

3 Answers

4
votes

[EDIT]

This has been merged to pythonnet:

https://github.com/pythonnet/pythonnet/pull/460


I ran into the same issue with nullable primitives -- it seems to me that Python.NET doesn't support these types. I got around the problem by adding the following code in Python.Runtime.Converter.ToManagedValue() (\src\runtime\converter.cs)

if( obType.IsGenericType && obType.GetGenericTypeDefinition() == typeof(Nullable<>) )
{
    if( value == Runtime.PyNone )
    {
        result = null;
        return true;
    }
    // Set type to underlying type
    obType = obType.GetGenericArguments()[0];
}

I placed this code right underneath

if (value == Runtime.PyNone && !obType.IsValueType) {
    result = null;
    return true;
}

https://github.com/pythonnet/pythonnet/blob/4df6105b98b302029e524c7ce36f7b3cb18f7040/src/runtime/converter.cs#L320

2
votes

I have no way to test this, but try

_repo.GetData('me', System.Int32(1), System.Nullable[System.Int32]())

Since you're saying that the optional parameter is Nullable, you need to create a new Nullable object of type Int32, or new System.Nullable<int>() in C# code.

I would have assumed that the first failing example would work, given that this is how optional parameters work in C#; calling the function without specifying the parameter at all.

1
votes

You have to pass argument to generic function System.Nullable[System.Int32](0)