Skip to content

operator.iconcat() and operator.concat() accept a right operand that is not a sequence #162609

Not planned

Description

@jseop-lim

Bug report

The docs describe both functions for two sequences: a = iconcat(a, b) "is equivalent to a += b for a and b sequences", and concat(a, b) returns "a + b for a and b sequences" (operator.rst, #L500-L503). Neither implementation checks b, so iconcat() succeeds whenever the left operand's += does:

import operator
print(operator.iconcat([1], frozenset([2])))
print(operator.iconcat([1], iter([2])))

CPython (3.12.10, 3.14.4):

[1, 2]
[1, 2]

The C version passes both operands to PySequence_InPlaceConcat(), which calls the left operand's sq_inplace_concat or sq_concat without looking at the right one. It checks PySequence_Check(o) only on the fallback for types without those slots. The pure-Python version in Lib/operator.py checks only a and then runs a += b, so it skips the check on that fallback as well. concat() has the same shape (#L146-L151), though for list it is masked because list + frozenset raises anyway.

PyPy requires both operands to be sequences and raises TypeError: 'list' object can't be concatenated for the calls above. A PR to make PyPy match CPython (pypy/pypy#5781) was answered with "I think PyPy's behaviour is correct", reading the documented "a and b sequences" as a requirement.

So the docs and PyPy say b must be a sequence, while both CPython implementations accept anything the left operand's += accepts. Which one is intended?

  • If b is meant to be a sequence, the two implementations could check it, as PyPy does.
  • If the current behaviour is intended, the docs could say that only a needs to be a sequence and b is whatever a += b (or a + b) accepts, and other implementations could follow that.

#73325 asked for the opposite relaxation (allow a non-sequence a) and was not taken up; the comments there describe concat() as the Python API to the sq_concat slot and argue against changing its semantics. That reading supports keeping the behaviour and fixing the docs, but I would like to confirm before PyPy changes either way.

CPython versions tested on:

3.12, 3.14

Operating systems tested on:

macOS

Activity

  1. added
    type-bug
    on Nov 25, 2026
  2. added
    stdlib
    on Nov 25, 2026
  3. picnixz commented on Nov 25, 2026

    @picnixz
    Member

    I don't think we need anything to do on our side either actually. Checking the sequence protocol takes time and this is mainly a way to write += either for partialization. As long as it's identical to +=, I believe it's fine. I don't think we need to be more specific about sequences or not. For sequences, the docs are correct. For non-sequences, it's a byproduct of += depending on the LHS.

  4. added
    pending
    on Nov 25, 2026
  5. jseop-lim commented on Nov 26, 2026

    @jseop-lim
    Author

    Thanks, that answers it: the result for a non-sequence b is whatever a += b does, and only the sequence case is specified. Closing.

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

    pendingstdlibtype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions