Wrap returned objects in interface if method return type is interface by danabr · Pull Request #1240 · pythonnet/pythonnet
danabr
marked this pull request as ready for review
This allows callers to call all methods of an interface, regardless of
whether the method was implemented implicitly or explicitly. Before this
change, you had to make an explicit cast to the interface to be able to
call the explicitly implemented method. Consider the following code:
```C#
namespace Python.Test {
public interface ITestInterface
{
void Foo();
void Bar();
}
public class TestImpl : ITestInterface
{
public void Foo() { };
public void ITestInterface.Bar() { };
public void Baz() { };
public static ITestInterface GetInterface()
{
return new TestImpl();
}
}
}
```
And the following Python code, demonstrating the behavior before this
change:
```python
from Python.Test import TestImpl, ITestInterface
test = TestImpl.GetInterface()
test.Foo() # works
test.Bar() # AttributeError: 'TestImpl' object has no attribute 'Bar'
test.Baz() # works! - baz
```
After this change, the behavior is as follows:
```
test = TestImpl.GetInterface()
test.Foo() # works
test.Bar() # works
test.Baz() # AttributeError: 'ITestInterface' object has no attribute 'Baz'
```
This is a breaking change due to that `Baz` is no longer visible in
Python.
Even when a method is declared to return an array of interfaces, the CLR may use an array of the concrete type. Keep track of the intended type in `ArrayObject` so that elements of the array can be properly wrapped in `InterfaceObject` when accessed.
Martin-Molinero pushed a commit to QuantConnect/pythonnet that referenced this pull request
Apr 26, 2022Reflect PR#8 MISSING CONVERTER.CS L516-528 Changes Reflect PR #14 Reflect PR #15 Reflect PR #19 Reflect PR #25 Reflect PR #34 Reflect PR #35 Implement List Conversion, Reflect PR #37 Tests Reflect PR #38 Partial: Assembly Manager Improvements Reflect PR #38 Reflect PR #42 KeyValuePairEnumerableObject Reflect PR #10 Runtime DecimalType Add TimeDelta and DateTime tests Fix DecimalConversion test for float conversion Converter mod tweaks Adjust a few broken PyTests Use _pydecimal to not interfere with Lean/decimal.py Add MethodBinder tests MethodBinder implicit resolution Fix bad cherry pick Refactoring precedence resolution Deal with operator binding Fix `TestNoOverloadException` unit test Fix for DomainReload tests Add InEquality Operator Test Dont PyObjects precedence in Operator methods Revert "Merge pull request pythonnet#1240 from danabr/auto-cast-ret-val-to-interface" This reverts commit 50d947f, reversing changes made to d44f1da. Fix Primitive Conversion to Int Post rebase fix Add PrimitiveIntConversion test Add test for interface derived classes Add to Authors.md Load in current directory into Python Path Include Python Lib in package Update as QuantConnect.PythonNet; include console exe in package Drop MaybeType from ClassManager for performance Package nPython from same configuration Address KWargs and Params; also cleanup Add unit tests Add pytest params unit test to testrunner Remove testing case from TestRuntime.cs Fix HandleParamsArray Test case Version bump Update QC Tests Refactor Params Fix Fix assembly info Handle breaking PyTests Cleanup Optimize Params Handling First reflection improvements Add TypeAccessor improvements and a bunch more tests More improvements Bump version to 2.0.2 Revert ClassManager changes Remove readonly Replace FastMember with Fasterflect Add global MemberGetter/MemberSetter cache Minor changes Make Fasterflect work with all regression tests Fix performance regressions Revert accidental pythonnet/runtime/.gitkeep removal Handle sending a python list to an enumerable expecting method - Converter with handle sending a python List to a method expecting a csharp enumerable. Adding unit test Bump version to 2.0.3 Update to net5.0 - Updating all projects to target net.50 - Remove domain test since it's not supported in net5.0 Bump pythonNet version 2.0.4 Add reproducing test Apply fix Catch implicit conversion throw Cleanup solution Cleanup V2 Assert Error message Small performance improvement Drop print statement from unit test Bump version to 2.0.5 Bump references to new version Fix for methods with different numerical precision overloads - Fix for methods with different numerical precision overloads. Method precedence will give higher priority to higher resolution numerical arguments. Adding unit test Version bump to 2.0.6 KeyValuePair conversion and performance - Improve DateTime conversion performance - Add support for KeyValuePair conversions - Minor improvements for convertions and method binder TypeManager and decimal improvements Reduce unrequired casting Version bump to 2.0.7 Apply fixes Project fix for linux systems Add unit test Converter cleanup More adjustments and fixes Add additional Py test & cleanup Use the generic match when others fail Add test for non-generic choice Address review Cleanup Version bump 2.0.8 Add performance test, also add caching Make Cache static to apply to all binders Make adjustments from testing Add test where overload exists with an already typed generic parameter use `ContainsGenericParameters` to check for unassigned generics Implement fix Add accompanying test Add additional tests Fix minor issue with py Date -> DateTime Refactor solution, use margs directly convert in ResolveGenericMethod Version Bump 2.0.9 Add missing exception clearing. Adding unit test Version bump 2.0.10 Handle readonly conversion to list. Adding unit tests Bump version to 2.0.11
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters