Fix operator stub signatures: drop self param, detect reverse operators#17
Open
eevleevs wants to merge 1 commit into
Open
Fix operator stub signatures: drop self param, detect reverse operators#17eevleevs wants to merge 1 commit into
eevleevs wants to merge 1 commit into
Conversation
.NET operators are static methods (e.g. op_Addition(Point, Point)) with two parameters. The generator was prepending self on top of both parameters, producing 3-param signatures like __add__(self, p1, p2) instead of the correct 2-param __add__(self, p2). This fix: - Drops the first parameter for forward operators (it becomes self) - Drops the second parameter for reverse operators (e.g. op_Multiply(float, Vector) maps to __rmul__ instead of __mul__) - Adds isReverse detection by comparing parameter types to the declaring type - Comparison operators (__eq__, __ne__, __gt__, etc.) have no reverse variants Fixes the Liskov Substitution Principle violation when subclassing .NET types with operator overrides in Python.
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.
Problem
.NET operators are static methods (e.g. \op_Addition(Point p1, Point p2)). The generator was prepending \self\ on top of both parameters, producing 3-param signatures:
\\python
Before (wrong):
def add(self, p1: Point, p2: Point) -> Point: ...
\\
Python's dunder protocol shifts the first operand into \self, so the correct signature has only 2 params:
\\python
After (correct):
def add(self, p2: Point) -> Point: ...
\\
This caused two type-checker errors when subclassing .NET types:
Fix
Drop the redundant parameter — For operators, the first .NET parameter becomes \self\ and is removed from the parameter list (\skipIndex\ in \GetParameters)
Detect reverse operators — When the first parameter type doesn't match the declaring type (e.g. \op_Multiply(float, Vector)\ on the \Vector\ class), the method maps to the reverse dunder (_rmul_\ instead of _mul_). The second parameter becomes \self\ and is dropped instead.
Comparison operators (_eq_, _ne_, _gt_, _lt_, _ge_, _le_) have no reverse variants in Python — they always use the same name regardless of operand order.
Verification
Regenerated stubs for Tekla Structures DLLs and confirmed correct output:
\\python
Point operators (symmetric):
def add(self, p2: Point) -> Point: ...
def sub(self, p2: Point) -> Point: ...
def eq(self, p2: Point) -> bool: ...
Vector operators (asymmetric):
def mul(self, Multiplier: float) -> Vector: ... # op_Multiply(Vector, float)
def rmul(self, Multiplier: float) -> Vector: ... # op_Multiply(float, Vector) → reverse!
\\
Type checker (\ y) passes cleanly on subclasses that override these operators.