Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PEP8 method overload binding #87

Merged
merged 2 commits into from
Apr 16, 2024
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
94 changes: 87 additions & 7 deletions src/embed_tests/ClassManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,93 @@ public int another_int_property()
{
return 654;
}

public virtual int get_value(int x)
{
throw new Exception("get_value(int x)");
}

public virtual int get_value_2(int x)
{
throw new Exception("get_value_2(int x)");
}

public int get_value_3(int x)
{
throw new Exception("get_value_3(int x)");
}

public int GetValue(int x)
{
throw new Exception("GetValue(int x)");
}

public virtual int GetValue(int x, int y)
{
throw new Exception("GetValue(int x, int y)");
}

public virtual int GetValue2(int x)
{
throw new Exception("GetValue2(int x)");
}

public int GetValue3(int x)
{
throw new Exception("GetValue3(int x)");
}
}

private class AlreadyDefinedSnakeCaseMemberTestDerivedClass : AlreadyDefinedSnakeCaseMemberTestBaseClass
{
public int SomeIntProperty { get; set; } = 111;

public override int AnotherIntProperty { get; set; } = 222;

public override int get_value(int x)
{
throw new Exception("override get_value(int x)");
}

public override int GetValue(int x, int y)
{
throw new Exception("override GetValue(int x, int y)");
}

public override int GetValue2(int x)
{
throw new Exception("override GetValue2(int x)");
}

public new int GetValue3(int x)
{
throw new Exception("new GetValue3(int x)");
}
}

[TestCase(typeof(AlreadyDefinedSnakeCaseMemberTestBaseClass), "get_value", new object[] { 2, 3 }, "GetValue(int x, int y)")]
// 1 int arg, binds to the original c# class get_value(int x)
[TestCase(typeof(AlreadyDefinedSnakeCaseMemberTestBaseClass), "get_value", new object[] { 2 }, "get_value(int x)")]
// 2 int args, binds to the snake-cased overriden GetValue(int x, int y)
[TestCase(typeof(AlreadyDefinedSnakeCaseMemberTestDerivedClass), "get_value", new object[] { 2, 3 }, "override GetValue(int x, int y)")]
[TestCase(typeof(AlreadyDefinedSnakeCaseMemberTestDerivedClass), "get_value", new object[] { 2 }, "override get_value(int x)")]
[TestCase(typeof(AlreadyDefinedSnakeCaseMemberTestDerivedClass), "get_value_2", new object[] { 2 }, "override GetValue2(int x)")]
[TestCase(typeof(AlreadyDefinedSnakeCaseMemberTestDerivedClass), "get_value_3", new object[] { 2 }, "new GetValue3(int x)")]
public void BindsSnakeCasedMethodAsOverload(Type type, string methodName, object[] args, string expectedMessage)
{
var obj = Activator.CreateInstance(type);
using var pyObj = obj.ToPython();

using var method = pyObj.GetAttr(methodName);
var pyArgs = args.Select(x => x.ToPython()).ToArray();

var exception = Assert.Throws<Exception>(() => method.Invoke(pyArgs));
Assert.AreEqual(expectedMessage, exception.Message);

foreach (var x in pyArgs)
{
x.Dispose();
}
}

[Test]
Expand All @@ -734,13 +821,6 @@ public void DoesntBindSnakeCasedMemberIfAlreadyOriginallyDefinedAsMethod()
Assert.AreEqual(654, method.Invoke().As<int>());
}

private class AlreadyDefinedSnakeCaseMemberTestDerivedClass : AlreadyDefinedSnakeCaseMemberTestBaseClass
{
public int SomeIntProperty { get; set; } = 111;

public int AnotherIntProperty { get; set; } = 222;
}

[Test]
public void DoesntBindSnakeCasedMemberIfAlreadyOriginallyDefinedAsPropertyInBaseClass()
{
Expand Down
4 changes: 2 additions & 2 deletions src/perf_tests/Python.PerformanceTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.*" />
<PackageReference Include="quantconnect.pythonnet" Version="2.0.32" GeneratePathProperty="true">
<PackageReference Include="quantconnect.pythonnet" Version="2.0.33" GeneratePathProperty="true">
<IncludeAssets>compile</IncludeAssets>
</PackageReference>
</ItemGroup>
Expand All @@ -25,7 +25,7 @@
</Target>

<Target Name="CopyBaseline" AfterTargets="Build">
<Copy SourceFiles="$(NuGetPackageRoot)quantconnect.pythonnet\2.0.32\lib\net5.0\Python.Runtime.dll" DestinationFolder="$(OutDir)baseline" />
<Copy SourceFiles="$(NuGetPackageRoot)quantconnect.pythonnet\2.0.33\lib\net5.0\Python.Runtime.dll" DestinationFolder="$(OutDir)baseline" />
</Target>

<Target Name="CopyNewBuild" AfterTargets="Build">
Expand Down
3 changes: 1 addition & 2 deletions src/runtime/ClassManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,6 @@
if (name == "__init__" && !impl.HasCustomNew())
continue;

CheckForSnakeCasedAttribute(name);
if (!methods.TryGetValue(name, out var methodList))
{
methodList = methods[name] = new MethodOverloads(true);
Expand All @@ -505,7 +504,7 @@
if (!OperatorMethod.IsOperatorMethod(meth))
{
var snakeCasedMethodName = name.ToSnakeCase();
if (snakeCasedMethodName != name && !originalMemberNames.Contains(snakeCasedMethodName))
if (snakeCasedMethodName != name)
{
if (!methods.TryGetValue(snakeCasedMethodName, out methodList))
{
Expand Down Expand Up @@ -646,7 +645,7 @@
/// </summary>
private class ClassInfo
{
public Indexer? indexer;

Check warning on line 648 in src/runtime/ClassManager.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu, 3.11, x64)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 648 in src/runtime/ClassManager.cs

View workflow job for this annotation

GitHub Actions / Build and Test (macos, 3.8, x64)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 648 in src/runtime/ClassManager.cs

View workflow job for this annotation

GitHub Actions / Build and Test (macos, 3.11, x64)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 648 in src/runtime/ClassManager.cs

View workflow job for this annotation

GitHub Actions / Build and Test (macos, 3.9, x64)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 648 in src/runtime/ClassManager.cs

View workflow job for this annotation

GitHub Actions / Build and Test (macos, 3.10, x64)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
public readonly Dictionary<string, PyObject> members = new();

internal ClassInfo()
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
[assembly: InternalsVisibleTo("Python.EmbeddingTest, PublicKey=00240000048000009400000006020000002400005253413100040000110000005ffd8f49fb44ab0641b3fd8d55e749f716e6dd901032295db641eb98ee46063cbe0d4a1d121ef0bc2af95f8a7438d7a80a3531316e6b75c2dae92fb05a99f03bf7e0c03980e1c3cfb74ba690aca2f3339ef329313bcc5dccced125a4ffdc4531dcef914602cd5878dc5fbb4d4c73ddfbc133f840231343e013762884d6143189")]
[assembly: InternalsVisibleTo("Python.Test, PublicKey=00240000048000009400000006020000002400005253413100040000110000005ffd8f49fb44ab0641b3fd8d55e749f716e6dd901032295db641eb98ee46063cbe0d4a1d121ef0bc2af95f8a7438d7a80a3531316e6b75c2dae92fb05a99f03bf7e0c03980e1c3cfb74ba690aca2f3339ef329313bcc5dccced125a4ffdc4531dcef914602cd5878dc5fbb4d4c73ddfbc133f840231343e013762884d6143189")]

[assembly: AssemblyVersion("2.0.32")]
[assembly: AssemblyFileVersion("2.0.32")]
[assembly: AssemblyVersion("2.0.33")]
[assembly: AssemblyFileVersion("2.0.33")]
2 changes: 1 addition & 1 deletion src/runtime/Python.Runtime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<RootNamespace>Python.Runtime</RootNamespace>
<AssemblyName>Python.Runtime</AssemblyName>
<PackageId>QuantConnect.pythonnet</PackageId>
<Version>2.0.32</Version>
<Version>2.0.33</Version>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<RepositoryUrl>https://github.com/pythonnet/pythonnet</RepositoryUrl>
Expand Down
Loading