Repository navigation
Give types with an implicit numeric conversion Python's number protocol - #151
Open
jhonabreul wants to merge 4 commits into
Open
jhonabreul wants to merge 4 commits into
jhonabreul wants to merge 4 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.
Python operators never consult C# implicit conversions, so a type convertible to decimal or double still raised TypeError on addition, comparison and float(). Such types now get arithmetic, comparison, __float__ and __int__ methods delegating to the conversion, grafted at class binding the same way EnumOps is for enums. Operators the type declares itself are kept.
This was referenced Oct 14, 2026
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.
Stacked on #150; the last two commits are this change.
A C# type with an implicit conversion to a number is usable as a number in C#, but not in Python:
Cause: Python evaluates
a + bthroughtype(a).__add__, pythonnet wires those slots from C# operator methods only, and anop_Implicitconversion is never consulted by an operator expression.The change:
ImplicitNumericOps<T>is an[Ops]class likeEnumOps<T>: arithmetic, comparison, unary negation,__float__and__int__methods delegating to the type's implicit numeric conversion.ClassManager.GetClassInfografts those methods onto any type whose public staticop_Implicitreturns a numeric type, skipping operator names the type declares itself.OperatorMethod.GetOperatorCandidatesincludes them soFixupSlotswires the number slots, and a new__float__slot definition maps tonb_float.1 < xontox > 1by itself.Does this close any currently open issues?
No.
Any other comments?
Tests, in
TestImplicitNumericOperators:sum,maxandsortedover convertible instances.float()on a decimal-backed type,float()andint()on a double-backed type.+keeps it, while the undeclared-is synthesized.__add__or__float__.