IronLanguages / IronLanguages/ironpython3

Concatenating a sequence with another type may not work as expected

Open
#560 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
2.8k
Forks
316
Avg merge
1d 9h
Merged PRs (30d)
1

Description

Normally, for operations like a + b, Python will try a.__add__(b) then b.__radd__(a). However for sequence types __add__ is a concat operation and method resolution is not the same. Even though sequence types have an __add__ method, the __radd__ method will take precedence. For example

class M:
    def __rmul__(self, other):
        return 23
    def __radd__(self, other):
        return 64

m = M()
for seq in ([1,2], (1,2)):
    try:
        seq.__add__(m)
    except TypeError:
        pass
    else:
        assert False
    try:
        seq.__mul__(m)
    except TypeError:
        pass
    else:
        assert False
    assert seq * m == 23
    assert seq + m == 64

There is currently some special handling in IronPython.Runtime.Binding.PythonProtocol.GetOperatorMethods for the __mul__ case but nothing for __add__.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in IronPython.Runtime.Binding.PythonProtocol.GetOperatorMethods, where the issue notes special handling for mul. Compare that handling with sequence add dispatch and use the provided M example to check behavior. Done means sequence + and * operations give the expected reflected-method results without changing direct add or mul behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.