Skip to content

x *= seq raises TypeError for int subclasses and IntEnum where x * seq works #161968

Open

Description

@Broyojo

Bug report

When the left operand is an instance of a Python class that supports __index__ but has no __mul__/__imul__ that handles sequences (an int subclass, an IntEnum member, or any class with __index__), x *= [1] raises TypeError, while x * [1] and x = x * [1] work. With a plain int on the left it works.

import enum, operator

class MyInt(int):
    pass

class Number(enum.IntEnum):
    TWO = 2

x = MyInt(2)
print(x * [1])              # [1, 1]
x *= [1]                    # TypeError: unsupported operand type(s) for *=: 'MyInt' and 'list'

y = Number.TWO
y *= [1]                    # TypeError: unsupported operand type(s) for *=: 'Number' and 'list'

operator.imul(MyInt(2), [1])  # same TypeError

Expected: [1, 1] in each case, the same as x * [1].

The cause is in PyNumber_InPlaceMultiply (Objects/abstract.c): the right operand's sq_repeat is only tried in an else if (mw != NULL) branch, which is skipped whenever the left operand's type has a tp_as_sequence at all, even if that has no repeat slot. Every heap type has tp_as_sequence, so for these classes the list's repeat is never reached. PyNumber_Multiply does not have this problem because it checks the right operand independently.

CPython versions tested on:

3.15, CPython main branch

Operating systems tested on:

Linux


Found while building a Lean 4 model of CPython (https://broyojo.com/python-formalization/). AI tools (Claude) helped investigate the findings and prepare this report. The examples were rerun on CPython 3.15.2 and main, and the report received technical review. Record: corelibs-inplace-mul-heap-left

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    on Nov 14, 2026
  2. added 2 commits that reference this issue on Nov 14, 2026
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

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions