Skip to content

right operand's __radd__ method is ignored when the left operand is a NamedTuple (3.15.0a7) #146592

@neutrinoceros

Description

@neutrinoceros

Bug report

Bug description:

I'm seeing a regression in how an + operation is resolved between a NamedTuple and an arbitrary type with a __radd__ implementation, which looks like an LSP violation since builtin tuple instance still behave the same as with Python 3.14

from typing import NamedTuple

class T(NamedTuple):
     x: int

class A:
    def __init__(self, *args):
       self.lst = list(args)
    def __radd__(self, other):
        return A(*self.lst, other)
 
print(((1,)+A()).lst) # [(1,)]
print((T(x=1)+A()).lst) # raises
Traceback (most recent call last):
  File "/Users/clm/dev/gh/astropy/astropy/t.py", line 13, in <module>
    print((T(x=1)+A()).lst) # raises
           ~~~~~~^~~~
TypeError: can only concatenate tuple (not "A") to tuple

The __radd__ implementation on the right operand appears to be entirely skipped, and we only see the error from the left operand's __add__ method.

This was originally discovered in astropy's test suite.

xref numpy/numpy#31088
xref astropy/astropy#19503

CPython versions tested on:

3.15

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions