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

Abierto
#161,968 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
1-2 días
Aptitud para principiantes
25/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Estancado
Stack tecnológico
c, python
Área
compilers

Línea de trabajo

Start with PyNumber_InPlaceMultiply in Objects/abstract.c and compare its handling of the right operand's sq_repeat with PyNumber_Multiply. Find the relevant existing numeric or sequence-operation tests; the issue does not name a test file. Done means in-place multiplication matches ordinary multiplication for the reported int subclass and IntEnum cases, with tests passing.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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
  • gh-161969
Lenguaje dominante
Python
Estrellas
78.2k
Forks
36.6k
Merge medio
19 h 57 min
PR fusionados (30 d)
630

Preparar el entorno

Abrir en Codespaces

Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de python/cpython

Todos los issues de python/cpython

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.