python / python/cpython

Behaviour change in partialmethod in Python3.11

未关闭
#99,152 5 条评论 1 个 reaction 已指派 1 人 在 GitHub 查看

@rhettinger 已经在做这个了。

开始于 2022年11月7日。

stdlib type-bug
主要语言
Python
星标
77.2k
派生
36k
PR 合并指标
PR 指标待抓取

描述

Bug report

The behavior of partialmethod changed in python3.11. This caused a library that I use testsuite to fail.

Here is a minimal use case that demonstrates this:

from functools import partialmethod


class Cell:
    def __init__(self):
        self.alive = False

    def set_state(self, state):
        self.alive = bool(state)


class Wrapper:
    def set_state_wrap(self, cell: Cell, state):
        cell.set_state(state)


wrapper = Wrapper()

Cell.set_alive = partialmethod(wrapper.set_state_wrap, True)

cell = Cell()
cell.set_alive()
print(cell.alive)

In python3.10, this prints True. In python3.11 it errors with:

Traceback (most recent call last):
  File "/home/gary/partialmethod.py", line 23, in <module>
    c.set_alive()
TypeError: Wrapper.set_state_wrap() missing 1 required positional argument: 'state'

The arguments passed to Wrapper.set_state_wrap are:
python3.10: wrapper, cell, True
python3.11: cell, True

So partialmethod is forgetting that set_state_wrap is a method of wrapper, and it needs to pass that in as the first arg.

I know that the way that they are using partialmethod here is not how it is intended to be used, and feels wrong. However this change is behavior if intended should be documented.

Your environment

  • CPython versions tested on: Python 3.11.0 (broken) Python 3.10.7 (working)
  • Operating system and architecture: Fedora Linux 36

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。