Skip to content

Changed behavior of <instancemethod>.__get__ in Python 3.11 #113157

Open
@mschmitzer

Description

@mschmitzer

Bug report

Bug description:

There is a change in behavior in Python 3.11 that does not seem to be described in the "what's new" document.

In the following code:

class A:
    def meth(self):
      print(self)

class B:
    pass

a = A()
b = B()
b.meth = a.meth.__get__(b, B)
b.meth()

The b.meth() call receives a as the self argument up to Python 3.10, but b in 3.11 and 3.12.

This pattern is used by the Pyramid framework. The change breaks Pyramid applications that use add_request_method(someobject.somemethod, "name").

I have to admit that I do not understand the descriptor protocol (or the documentation of __get__) well enough to say whether this is a legitimate use case, but it does exist in the wild.

CPython versions tested on:

3.9, 3.10, 3.11, 3.12

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

Labels

type-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions