`x *= seq` raises TypeError for int subclasses and IntEnum where `x * seq` works
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 1〜2日
- 初心者へのやさしさ
- 25/100
調査の方向性
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.
索引モデルが issue の本文から書いたものです。
説明
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
- 主要言語
- Python
- スター
- 78.2k
- フォーク
- 36.6k
- 平均マージ
- 19時間 57分
- マージ済み PR(30日)
- 630
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
python/cpython のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
stdlib type-bug
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
メンテナーはふだん 1 日以内に返信
-
extension-modules stdlib topic-XML type-bug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
python/cpython#161807 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
stdlib type-feature
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
python/cpython#161788 · コメント 7 件 ·
メンテナーはふだん 1 日以内に返信
-
extension-modules topic-IO type-bug
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
メンテナーはふだん 1 日以内に返信
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
cpp-best-practices/project_validator#267 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
Project-N-E-K-O/N.E.K.O#3725 ·
メンテナーはふだん 1 日以内に返信
-
[Bug] Sending a Telegram message chain twice duplicates mentions対応中かも @123XieC が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
AstrBotDevs/AstrBot#11125 ·
メンテナーはふだん 1 日以内に返信
-
backend bug priority: high
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
nooriegarg/ContribIQ#73 ·
メンテナーはふだん 1 日以内に返信
-
bug p3 security
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
メンテナーはふだん 1 日以内に返信