Skip to content

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

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

Activity

  1. serhiy-storchaka commented on Mar 29, 2026

    @serhiy-storchaka
    Member

    This looks like a duplicate of #146587 opened three hours ago.

    Did you guys get this from the same source?

  2. neutrinoceros commented on Mar 29, 2026

    @neutrinoceros
    ContributorAuthor

    Did you guys get this from the same source?

    Not that I'm aware. This looks entirely coincidental to me, but of course I'm okay with closing this one as a dupe. Thanks for pointing it out !

  3. neutrinoceros commented on Mar 29, 2026

    @neutrinoceros
    ContributorAuthor

    Not that I'm aware.

    to be clear what I meant here: I discovered the problem independently about 13h ago and reported it in astropy, then numpy.
    I don't know wether #146587 was an entirely independent discovery or if it built from my early (misplaced) reports.

  4. serhiy-storchaka commented on Mar 29, 2026

    @serhiy-storchaka
    Member

    Most likely.

  5. inventshah commented on Mar 29, 2026

    @inventshah
    Contributor

    Coincidental, but glad there is an established use case! I was only on the beta to play with Tachyon, and couldn't pass up an opportunity to git bisect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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