← 输入框|github.com|刷新
Skip to content

Cannot get correct overload of generic method #821

Open

Description

@Tobimaru

Environment

  • Pythonnet version: zip archive of master branch
  • Python version: 3.6
  • Operating System: Ubuntu 18.04

Details

  • Describe what you were trying to get done.

Invoke a generic method with multiple overloads.

  • What commands did you run to trigger this issue?

On the C# side I have the following generic method with overloads:

Variable<T> Variable.Constant<T>(T)
VariableArray<T> Variable.Constant<T>(T[])    

On the python side, I'm trying to invoke the second overload by doing:

a = Variable.Constant(System.Array[System.Double]([1.0, 2.0, 3.0]))

But when I check the type of a, it gives Variable[System.Double[]] and not VariableArray[System.Double]. This makes sense since it's ambiguous without specifying the type of the output variable or specifying the type for the function. Since in python I can't pin the output variable to a specific type, I tried to resolve the ambiguity with:

a = Variable.Constant[System.Double](System.Array[System.Double]([1.0, 2.0, 3.0]))

but then I get the exception:

TypeError: No method matches given arguments for Constant

I also tried:

a = Variable.Constant.Overloads[System.Double](System.Array[System.Double]([1.0, 2.0, 3.0]))

but then I get the exception:

TypeError: No match found for signature

It seems that there is pull-request (#227) ongoing, but it looks to have been work in progress for quite a while now. Is this is not priority, is there a workaround that I can try?

Activity

  1. filmor commented on Mar 6, 2019

    @filmor
    Member

    The method overload resolution of Python.NET is very tricky to get right, as we're dealing with two different type systems that should somehow be made compatible. I'm not sure how #227 would solve this.

  2. Tobimaru commented on Mar 7, 2019

    @Tobimaru
    Author

    Is there any way to work around this problem? The only thing I can think of is to write a small C# library that explicitly instantiates the generic methods with the types that I need. I hope that python.NET can then resolve the overloads.

  3. filmor commented on Mar 8, 2019

    @filmor
    Member

    Yes, that approach works fine, and I'm currently not aware of a workaround that doesn't require an additional library.

  4. den-run-ai commented on Mar 8, 2019

    @den-run-ai
    Contributor

    You can always use reflection, search this issue tracker or stackoverflow "python.net" tag.

  5. Tobimaru commented on Mar 20, 2019

    @Tobimaru
    Author

    Sorry for the late response. Thanks for the info. I tried with a wrapper library around the generic methods and that seems to work. But now I'm having issues with [] overloads. I haven't tried with using reflections yet, I'll give it a try.

  6. Tobimaru commented on Mar 23, 2019

    @Tobimaru
    Author

    Reflection doesn't seem to work either. The problem is that in the module that I'm trying to use (infer.net), Variable is an abstract class and Constant is a static method of this abstract class. I don't know how to invoke the method of an abstract class. If I do:

    var_type = dll.GetType("Variable")
    method = var_type.GetMethod("Constant")

    Then I get the error:

    AttributeError: 'NoneType' object has no attribute 'GetMethod'
  7. christabella commented on Jan 13, 2021

    @christabella
    Contributor

    This is probably too late but an infer.net-specific workaround could be to use Invoker.InvokeStatic(Variable, "Constant", args) e.g.
    https://github.com/dotnet/infer/blob/0c75b5ef4190c5eee55e663741e645cb997b7236/test/TestPython/test_tutorials.py#L217

  8. added a commit that references this issue on Sep 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions