Support comparison operators by christabella · Pull Request #1347 · pythonnet/pythonnet
What does this implement/fix? Explain your changes.
Continuation of https://github.com/pythonnet/pythonnet/pull/1324/files but specifically for comparison operators e.g. >, >=, <, == etc. based on a discussion with @tminka --- in a nutshell, we want to check each C# class for any comparison operator methods when calling ClassBase.tp_richcompare, before proceeding with the usual logic (see #294) which handles C# classes that implement an IComparable interface.
Would appreciate a review from @lostmsu who helped with the previous PR that this builds on. Thanks!
Does this close any currently open issues?
Closes #1312
More concrete examples can also be found in other Infer.NET tutorials e.g. having to use op_GreaterThan https://github.com/dotnet/infer/blob/67b4f80d97018460bcb817f76ec874d0f33f1651/test/TestPython/test_tutorials.py#L31
Any other comments?
Some remaining tasks could be:
- Add tests for derived objects, and for other edge cases (e.g. if truly no operator methods defined, asserting good error messages)
- When C# class defines
operator ==(), the class should also define .Equals and .GetHashCode() https://stackoverflow.com/questions/10790370/whats-wrong-with-defining-operator-but-not-defining-equals-or-gethashcode
Checklist
Check all those that are applicable and complete.
- Make sure to include one or more tests for your change
- If an enhancement PR, please create docs and at best an example
- Add yourself to
AUTHORS - Updated the
CHANGELOG
The tests pass in net472 but not on netcoreapp3.1