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

Constructors with arguments snake-cased version #93

Merged
Show file tree
Hide file tree
Changes from 3 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
32 changes: 32 additions & 0 deletions src/embed_tests/TestMethodBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,32 @@ def call_method(instance):
Assert.IsFalse(Exceptions.ErrorOccurred());
}

[Test]
public void BindsConstructorToSnakeCasedArgumentsVersion([Values] bool useCamelCase, [Values] bool passOptionalArgument)
{
using var _ = Py.GIL();

var argument1Name = useCamelCase ? "someArgument" : "some_argument";
var argument2Name = useCamelCase ? "anotherArgument" : "another_argument";
var argument2Code = passOptionalArgument ? $", {argument2Name}=\"another argument value\"" : "";

var module = PyModule.FromString("CallsCorrectOverloadWithoutErrors", @$"
from clr import AddReference
AddReference(""System"")
from Python.EmbeddingTest import *

def create_instance():
return TestMethodBinder.CSharpModel({argument1Name}=1{argument2Code})
");
var exception = Assert.Throws<ClrBubbledException>(() => module.GetAttr("create_instance").Invoke());
var sourceException = exception.InnerException;
Assert.IsInstanceOf<NotImplementedException>(sourceException);

var expectedMessage = passOptionalArgument
? "Constructor with arguments: someArgument=1. anotherArgument=\"another argument value\""
: "Constructor with arguments: someArgument=1. anotherArgument=\"another argument default value\"";
Assert.AreEqual(expectedMessage, sourceException.Message);
}

// Used to test that we match this function with Py DateTime & Date Objects
public static int GetMonth(DateTime test)
Expand All @@ -918,6 +944,12 @@ public CSharpModel()
new TestImplicitConversion()
};
}

public CSharpModel(int someArgument, string anotherArgument = "another argument default value")
{
throw new NotImplementedException($"Constructor with arguments: someArgument={someArgument}. anotherArgument=\"{anotherArgument}\"");
}

public void TestList(List<TestImplicitConversion> conversions)
{
if (!conversions.Any())
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.37" GeneratePathProperty="true">
<PackageReference Include="quantconnect.pythonnet" Version="2.0.38" 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.37\lib\net6.0\Python.Runtime.dll" DestinationFolder="$(OutDir)baseline" />
<Copy SourceFiles="$(NuGetPackageRoot)quantconnect.pythonnet\2.0.38\lib\net6.0\Python.Runtime.dll" DestinationFolder="$(OutDir)baseline" />
</Target>

<Target Name="CopyNewBuild" AfterTargets="Build">
Expand Down
5 changes: 5 additions & 0 deletions src/runtime/ClassManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,11 @@
methodList = methods[name] = new ();
}
methodList.Add(ctor, true);
// Same constructor, but with snake-cased arguments
if (ctor.GetParameters().Any(pi => pi.Name.ToSnakeCase() != pi.Name))
{
methodList.Add(ctor, false);
}
continue;

case MemberTypes.Property:
Expand Down Expand Up @@ -668,7 +673,7 @@
/// </summary>
private class ClassInfo
{
public Indexer? indexer;

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

View workflow job for this annotation

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

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

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

View workflow job for this annotation

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

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

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

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu, 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.37")]
[assembly: AssemblyFileVersion("2.0.37")]
[assembly: AssemblyVersion("2.0.38")]
[assembly: AssemblyFileVersion("2.0.38")]
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.37</Version>
<Version>2.0.38</Version>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<RepositoryUrl>https://github.com/pythonnet/pythonnet</RepositoryUrl>
Expand Down
Loading