- Notifications
You must be signed in to change notification settings - Fork749
Explicit interface implementations#1233
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
Previously, if a class implemented an interface explicitly, thosemethods and properties become invisible to Python. To use them, you hadto make an explicit cast to the interface type.
dnfadmin commentedSep 23, 2020 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
codecov-commenter commentedSep 23, 2020 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Codecov Report
@@ Coverage Diff @@## master #1233 +/- ##======================================= Coverage 86.25% 86.25% ======================================= Files 1 1 Lines 291 291 ======================================= Hits 251 251 Misses 40 40
Flags with carried forward coverage won't be shown.Click here to find out more. Continue to review full report at Codecov.
|
I am not convinced this should be the expected behavior. Technically explicit interface implementations are private methods, and Python.NET does not expose them by design. Is there a scenario, where you can not cast your instance to the desired interface? Or do you just want duck typing for some reason? |
Ah, that explains it. I ran into this when trying to switch out IronPython in a project. Since it worked there I got the impression that it would work with Python.NET also. I did not know that external interface implementations are private. Thanks for the explanation! I'll see if there is a way I can do the casting automatically in our module loader, so that end users don't have to worry about it. |
@lostmsu Can you link some docs regarding the privacy of explicit interface implementations? I thought they were mainly used to get around name and signature conflicts, I wasn't aware of a semantic significance. |
danabr commentedSep 24, 2020 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@lostmsu : There's still an issue when a method returns an interface. If the returned object implements its interface explicitly, its methods becomes invisible to Python: publicinterfaceISayHello1{stringSayHello();}publicclassInterfaceTest2:ISayHello1{publicISayHello1GetISayHello1(){returnthis;}stringISayHello1.SayHello(){return"ISayHello1";}} deftest_interface_object_returned_through_method():"""Test explicitly implemented methods are visible"""fromPython.TestimportInterfaceTest2ob=InterfaceTest2()hello1=ob.GetISayHello1()asserthello1.SayHello()=='ISayHello1' This test fails with:
The caller of GetISayHello1 shouldn't really need to cast the returned object to I'd be happy for advise on how to handle this. |
As said, I'm not aware of any semantic difference between the "usual" and explicit implementation of an interface, so IMO this change makes sense. |
The C# documentation ([1]) does say that:
So behaving differently in Python.NET might not be a good idea, although perhaps never a problem in practice. Could the issue with returning an interface from a method be solved some other way? e.g. by doing the same thing that we do when casting to the interface in Python when we convert the return value of a method to a Python value? |
lostmsu commentedSep 24, 2020 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@danabr you would need to postprocess after this line: pythonnet/src/runtime/methodbinder.cs Line 698 in451fae6
and maybe in some other places. |
What does this implement/fix? Explain your changes.
Previously, if a class implemented an interface explicitly, those
methods and properties become invisible to Python. To use them, you had
to make an explicit cast to the interface type.
Does this close any currently open issues?
No
Any other comments?
Checklist
Check all those that are applicable and complete.
AUTHORS
CHANGELOG