You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
operator.iconcat() and operator.concat() accept a right operand that is not a sequence #162609
pendingThe issue will be closed if no feedback is providedstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error
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:
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.
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.
pendingThe issue will be closed if no feedback is providedstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error
Activity
picnixz commentedon Nov 25, 2026
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.jseop-lim commentedon Nov 26, 2026
Thanks, that answers it: the result for a non-sequence
bis whatevera += bdoes, and only the sequence case is specified. Closing.