Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrapping a bound method with a @classmethod no longer works #90920

Closed
msullivan mannequin opened this issue Feb 16, 2022 · 4 comments
Closed

Wrapping a bound method with a @classmethod no longer works #90920

msullivan mannequin opened this issue Feb 16, 2022 · 4 comments
Assignees
Labels
3.9 only security fixes 3.10 only security fixes 3.11 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@msullivan
Copy link
Mannequin

msullivan mannequin commented Feb 16, 2022

BPO 46764
Nosy @rhettinger, @msullivan
PRs
  • bpo-46764: Fix wrapping bound method with @classmethod #31367
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/rhettinger'
    closed_at = None
    created_at = <Date 2022-02-16.01:56:29.217>
    labels = ['interpreter-core', 'type-bug', '3.9', '3.10', '3.11']
    title = 'Wrapping a bound method with a @classmethod no longer works'
    updated_at = <Date 2022-02-17.20:02:42.052>
    user = 'https://github.com/msullivan'

    bugs.python.org fields:

    activity = <Date 2022-02-17.20:02:42.052>
    actor = 'rhettinger'
    assignee = 'rhettinger'
    closed = False
    closed_date = None
    closer = None
    components = ['Interpreter Core']
    creation = <Date 2022-02-16.01:56:29.217>
    creator = 'msullivan'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 46764
    keywords = ['patch']
    message_count = 2.0
    messages = ['413310', '413450']
    nosy_count = 2.0
    nosy_names = ['rhettinger', 'msullivan']
    pr_nums = ['31367']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue46764'
    versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

    @msullivan
    Copy link
    Mannequin Author

    msullivan mannequin commented Feb 16, 2022

    class A:
        def foo(self, cls): return 1
    
    class B: pass
    
    class B:
        bar = classmethod(A().foo)
    
    B.bar()

    In Python 3.8 and prior, this worked. Since Python 3.9, it produces "TypeError: A.foo() missing 1 required positional argument: 'cls'"

    I tracked it down, and the issue was introduced by https://github.com/python/cpython/pull/8405/files, which makes classmethod's tp_descr_get invoke its argument tp_descr_get when present instead of calling PyMethod_New. That this was a semantics change that could break existing code may have been missed (though it is a fairly obscure such change).

    The reason it breaks this case in particular of bound methods, though, is that bound methods have a tp_descr_get that does nothing (increfs the method and then returns it). Dropping that tp_descr_get fixes this issue and doesn't introduce any test failures. Not sure if there is some potential downstream breakage of that?

    (This issue was originally reported to me by Jared Hance)

    @msullivan msullivan mannequin added 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Feb 16, 2022
    @AlexWaygood AlexWaygood added the type-bug An unexpected behavior, bug, or error label Feb 16, 2022
    @rhettinger
    Copy link
    Contributor

    This seems like a reasonable fix. I'll wait a bit so that others can comment as well.

    FWIW, we looking a potentially reverting the whole line of development starting with that commit. It has caused a number of problems and no longer looks tenable. See https://bugs.python.org/issue45356

    @rhettinger rhettinger self-assigned this Feb 17, 2022
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @iritkatriel
    Copy link
    Member

    The PR was merged, and I think it resolves this issue.

    @iritkatriel
    Copy link
    Member

    Unless it can still be backported.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes 3.10 only security fixes 3.11 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants