Improved support for generic method overloading by lostmsu · Pull Request #1657 · pythonnet/pythonnet · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 21 additions & 17 deletions src/runtime/methodbinder.cs
11 changes: 7 additions & 4 deletions src/runtime/methodbinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,18 @@ public static NewReference mp_subscript(BorrowedReference tp, BorrowedReference
return Exceptions.RaiseTypeError("type(s) expected");
}

MethodBase? mi = self.m.IsInstanceConstructor
? self.m.type.Value.GetConstructor(types)
MethodBase[] overloads = self.m.IsInstanceConstructor
? self.m.type.Value.GetConstructor(types) is { } ctor
? new[] { ctor }
: Array.Empty<MethodBase>()
: MethodBinder.MatchParameters(self.m.info, types);
if (mi == null)
if (overloads.Length == 0)
{
return Exceptions.RaiseTypeError("No match found for given type params");
}

var mb = new MethodBinding(self.m, self.target, self.targetType) { info = mi };
MethodObject overloaded = self.m.WithOverloads(overloads);
var mb = new MethodBinding(overloaded, self.target, self.targetType);
return mb.Alloc();
}

Expand Down
5 changes: 4 additions & 1 deletion src/runtime/methodobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal class MethodObject : ExtensionType
internal PyString? doc;
internal MaybeType type;

public MethodObject(Type type, string name, MethodBase[] info, bool allow_threads = MethodBinder.DefaultAllowThreads)
public MethodObject(MaybeType type, string name, MethodBase[] info, bool allow_threads = MethodBinder.DefaultAllowThreads)
{
this.type = type;
this.name = name;
Expand All @@ -47,6 +47,9 @@ public MethodObject(Type type, string name, MethodBase[] info, bool allow_thread

public bool IsInstanceConstructor => name == "__init__";

public MethodObject WithOverloads(MethodBase[] overloads)
=> new(type, name, overloads, allow_threads: binder.allow_threads);

internal MethodBase[] info
{
get
Expand Down
3 changes: 3 additions & 0 deletions src/testing/methodtest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,9 @@ public static int Overloaded(int i, string s)
return i;
}

public virtual void OverloadedConstrainedGeneric<T>(T generic) where T : MethodTest { }
public virtual void OverloadedConstrainedGeneric<T>(T generic, string str) where T: MethodTest { }

public static string CaseSensitive()
{
return "CaseSensitive";
Expand Down
12 changes: 12 additions & 0 deletions tests/test_generic.py