You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given a CLR method Foo with overloads Foo(int a, int b) and Foo(float a, float b), calling Foo from Python with one int and one float argument results in a TypeError: no method matches given arguments for Foo.
If the method is not overloaded and only exists in the Foo(float a, float b) form, then calling from Python with one int and one float works fine, as does two ints, since the int arguments can be freely converted to float. The existence of the overload prevents this somehow.
I looked through the existing method resolution issues and didn't see this case.
usingPython.Runtime;namespacePythonNetFloatTest{classProgram{staticvoidMain(string[]args){stringpreamble="import clr\n"+"clr.AddReference('MyModule')\n"+"from MyModule import MyClass\n"+"import sys\n"+"print(sys.version)";stringcallWithFloatArgs="result = MyClass.Add(1.0, 2.0)";stringcallWithIntArgs="result = MyClass.Add(1, 2)";stringcallWithMixedArgs="result = MyClass.Add(1.0, 2)";PythonEngine.Initialize();using(Py.GIL()){using(PyScopescope=Py.CreateScope()){scope.Exec(preamble);scope.Exec(callWithFloatArgs);System.Console.WriteLine(scope.Get("result"));// prints "3.0"scope.Exec(callWithIntArgs);System.Console.WriteLine(scope.Get("result"));// prints "3"scope.Exec(callWithMixedArgs);// throws Python.Runtime.PythonException: // 'TypeError : No method matches given arguments for Add'System.Console.WriteLine(scope.Get("result"));}}}}}
Output:
3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 00:42:30) [MSC v.1916 64 bit (AMD64)]
3.0
3
Unhandled Exception: Python.Runtime.PythonException: TypeError : No method matches given arguments for Add
at Python.Runtime.Runtime.CheckExceptionOccurred()
at Python.Runtime.PyScope.Exec(String code, IntPtr _globals, IntPtr _locals)
at PythonNetFloatTest.Program.Main(String[] args) in .\PythonNetFloatTest\PythonNetFloatTest\Program.cs:line 33
If you remove the public static int Add(int a, int b) overload from MyModule, the output is instead:
3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 00:42:30) [MSC v.1916 64 bit (AMD64)]
3.0
3.0
3.0
The text was updated successfully, but these errors were encountered:
Uh oh!
There was an error while loading. Please reload this page.
Environment
Details
Given a CLR method
Foo
with overloadsFoo(int a, int b)
andFoo(float a, float b)
, callingFoo
from Python with oneint
and onefloat
argument results in aTypeError: no method matches given arguments for Foo
.If the method is not overloaded and only exists in the
Foo(float a, float b)
form, then calling from Python with oneint
and onefloat
works fine, as does twoints
, since theint
arguments can be freely converted tofloat
. The existence of the overload prevents this somehow.I looked through the existing method resolution issues and didn't see this case.
Full example
MyModule\MyClass.cs
PythonNetFloatTest\Program.cs
Output:
If you remove the
public static int Add(int a, int b)
overload fromMyModule
, the output is instead:The text was updated successfully, but these errors were encountered: