8000 Added `ToPythonAs()` extension method for explicit conversion using an arbitrary type by joaompneves · Pull Request #2419 · pythonnet/pythonnet · GitHub
[go: up one dir, main page]

Skip to content

Added ToPythonAs() extension method for explicit conversion using an arbitrary type #2419

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

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
- Jan Krivanek ([@jakrivan](https://github.com/jakrivan))
- Jeff Reback ([@jreback](https://github.com/jreback))
- Jeff Robbins ([@jeff17robbins](https://github.com/jeff17robbins))
- João Neves ([@joaompneves](https://github.com/joaompneves))
- Joe Frayne ([@jfrayne](https://github.com/jfrayne))
- Joe Lidbetter ([@jmlidbetter](https://github.com/jmlidbetter))
- Joe Savage ([@s4v4g3](https://github.com/s4v4g3))
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].

### Added

- Added `ToPythonAs()` extension method to allow for explicit conversion using a specific type not known at build time. ([#2419][i2419])

- Added `ToPythonAs<T>()` extension method to allow for explicit conversion using a specific type. ([#2311][i2311])

- Added `IComparable` and `IEquatable` implementations to `PyInt`, `PyFloat`, and `PyString`
Expand Down
29 changes: 29 additions & 0 deletions src/embed_tests/Codecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,23 @@ public void ExceptionDecoded()
Assert.AreEqual(TestExceptionMessage, error.Message);
}

[Test(Description = "Tests encoding of an object that explicitly implements an interface.")]
public void ExplicitObjectInterfaceEncoded()
{
var obj = new ExplicitInterfaceObject();
using var scope = Py.CreateScope();
scope.Exec(@"
def call(obj):
return dir(obj)
");
var callFunc = scope.Get("call");
// explicitly pass an interface (but not a generic type argument) to simulate a scenario where the type is not know at build time
var callArg = obj.ToPythonAs(typeof(IObjectInterface));
var members = callFunc.Invoke(callArg).As<string[]>();
CollectionAssert.Contains(members, nameof(IObjectInterface.MemberFromInterface));
CollectionAssert.DoesNotContain(members, nameof(ExplicitInterfaceObject.MemberFromObject));
}

[Test]
public void DateTimeDecoded()
{
Expand Down Expand Up @@ -533,4 +550,16 @@ public bool TryDecode<T>(PyObject pyObj, out T value)
return true;
}
}

interface IObjectInterface
{
int MemberFromInterface { get; }
}

class ExplicitInterfaceObject : IObjectInterface
{
int IObjectInterface.MemberFromInterface { get; }

public int MemberFromObject { get; }
}
}
1 change: 1 addition & 0 deletions src/embed_tests/Python.EmbeddingTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFrameworks>net472;net6.0</TargetFrameworks>
<RollForward>LatestMajor</RollForward>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This allows anyone that has recent versions of .net to run these tests. I can revert but I think its usefull, otherwise one needs to install .net6 which is already deprecated

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be conditioned on not running in CI

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any config/env var which I can check?

<AssemblyOriginatorKeyFile>..\pythonnet.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
</PropertyGroup>
Expand Down
7 changes: 6 additions & 1 deletion src/runtime/Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -983,9 +983,14 @@ public static PyObject ToPython(this object? o)
}

public static PyObject ToPythonAs<T>(this T? o)
{
return ToPythonAs(o, typeof(T));
}

public static PyObject ToPythonAs(this object? o, Type type)
{
if (o is null) return Runtime.None;
return Converter.ToPython(o, typeof(T)).MoveToPyObject();
return Converter.ToPython(o, type).MoveToPyObject();
}
}
}
Loading
0