basedpyright icon indicating copy to clipboard operation
basedpyright copied to clipboard

enforce operators include possible right hand types in their input parameter

Open KotlinIsland opened this issue 5 months ago • 6 comments

Description

class Left:
    def __add__(self, other: int) -> int:
        # here "other" is `Right`
        return other + 1 # Type error here


class Right:
    def __radd__(self, other: Left) -> str:
        return "i'm string :)"


Left() + Right()

KotlinIsland avatar Sep 13 '25 14:09 KotlinIsland

i dont get it, Left() + Right() is a str in basedpyright and at runtime so what's the issue?

DetachHead avatar Sep 15 '25 14:09 DetachHead

i dont get it, Left() + Right() is a str in basedpyright and at runtime so what's the issue?

this code raises a TypeError because it passes a Right to a function that only accepts an int

KotlinIsland avatar Sep 15 '25 14:09 KotlinIsland

Image

DetachHead avatar Sep 15 '25 14:09 DetachHead

Image

oopsies, that was an oversight. i've updated the op

KotlinIsland avatar Sep 15 '25 14:09 KotlinIsland

any reason why the error should be on the usage instead? having to account for every possible type in the definition would be annoying

DetachHead avatar Oct 03 '25 03:10 DetachHead

any reason why the error should be on the usage instead?

that would make sense, but it would be very disruptive. everything is written with this assumption

KotlinIsland avatar Oct 04 '25 01:10 KotlinIsland