-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't overload __add__ for subclass #1987
Comments
@JukkaL Any idea what could be causing this? |
It's this code in
This seems to be overly conservative. I'd suggest just removing the check and seeing if this breaks something. I'm sure there is some edge case where things can go wrong, but it's probably not important enough to worry about it. |
Given that it applies only in the special case of a method like |
Any updates on this? Overloaded methods like class A:
def __mul__(self, other: A) -> 'B':
...
def __add__(self, other: A) -> 'C':
...
If you want types, the work around would be to manually define the class A:
def multiply(self, other: A) -> 'B':
...
def add(self, other: A) -> 'C':
... ...then doing |
Can you show a complete example? I tried this and it gives me the expected output, no errors: class B: pass
class C: pass
class A:
def __mul__(self, other: A) -> 'B':
...
def __add__(self, other: A) -> 'C':
...
reveal_type(A()*A()) # B
reveal_type(A()+A()) # C |
Superseded by #4985 |
Consider the following working code:
Think of 'S' as 'str' and 'A' as 'ascii', a subclass used only for literals containing only ASCII characters. The idea is that adding ASCII to ASCII produces ASCII, but all other combinations produce just strings.
The subclass 'A' redefines the method
__add__
to express this idea. This version of the code works. However, the base class contains redundant overloads: 'A' is a subclass of 'S' so the base class is equivalent to the following, which doesn't use overloads:And here's the rub: with this definition of 'S', the definition of
__add__
in 'A' (still using overloads) is rejected:(The error points to the first
@overload
, but it seems to apply to the entire set of overloads.)Now here's the real rub: if I change
__add__
consistently tomethod
, the error goes away![UPDATE I can't repro this for
__radd__
-- it seems unique to__add__
.]The text was updated successfully, but these errors were encountered: