Repository navigation
Bind extension methods to the extended type so they are callable from the instance - #150
Open
jhonabreul wants to merge 2 commits into
Open
jhonabreul wants to merge 2 commits into
jhonabreul wants to merge 2 commits into
Conversation
… the instance C# extension methods were only reachable from Python through their static class, so instance-style calls such as order_status.is_open() failed with AttributeError. Extension methods declared in non-framework assemblies are now indexed during the assembly scan and bound as members of the type they extend, including interface targets on implementing classes. The binder passes the instance as the first argument when an extension method is called through it. Real members with the same name win, and open generic targets are skipped since the binder cannot infer them from the instance.
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this implement/fix? Explain your changes.
C# extension methods were only reachable from Python through their static class, so the instance-style call every C# example uses failed:
Cause:
ClassManagerbuilds a type's Python class from its real members only, and extension methods are static methods on another type.The change:
ExtensionMethodsindexes the public extension methods of each scanned assembly by the type they extend, insideAssemblyManager.ScanAssembly. Framework assemblies andPython.Runtimeare skipped so LINQ is not grafted onto every collection.ClassManager.GetClassInfobinds the extensions of the type and of the interfaces it introduces as members, so snake-case aliases come for free. A real member with the same name wins, as in C#.MethodBinder.Bindtreats an extension called through an instance like a bound operator: it matches the Python arguments against the remaining parameters and prepends the instance.this T,this IEnumerable<T>) and by-ref targets are skipped because the binder cannot infer them from the instance.AssemblyManager.Initializeso a re-initialized engine rebuilds it.Limits: targets converted to Python natives (
string,decimal,DateTime,double) cannot be bound, and their extensions stay available through the static class.Does this close any currently open issues?
No.
Any other comments?
Tests, in
TestExtensionMethods:List<int>) is bound; open generic target is not.string) is not bound but its static form works.