Skip to content

bpo-37203: Correct classmethod Python emulation in the Descriptor HowTo Guide#13912

Closed
geryogam wants to merge 1 commit into
python:masterfrom
geryogam:patch-3
Closed

bpo-37203: Correct classmethod Python emulation in the Descriptor HowTo Guide#13912
geryogam wants to merge 1 commit into
python:masterfrom
geryogam:patch-3

Conversation

@geryogam

@geryogam geryogam commented Jun 8, 2019

Copy link
Copy Markdown
Contributor

With the current Python equivalent ClassMethod implementation of classmethod given in Raymond Hettinger's Descriptor HowTo Guide, the following code snippet:

class A:
    @ClassMethod
    def f(cls, *, x):
        pass

print(A.f)
A.f(x=3)

prints:

<function ClassMethod.__get__..newfunc at 0x106b76268>

and raises:

TypeError: newfunc() got an unexpected keyword argument 'x'

instead of only printing:

<bound method A.f of <class '__main__.A'>>

like the @classmethod decorator would do.

So the ClassMethod implementation fails in two regards:

  • it does not return a bound method to a class;
  • it does not handle keyword-only parameters.

With this PR ClassMethod will correctly emulate classmethod. This approach (types.MethodType) is already used in the Python equivalent Function implementation of functions given earlier in the same guide.

https://bugs.python.org/issue37203

@bedevere-bot bedevere-bot added docs Documentation in the Doc dir awaiting review labels Jun 8, 2019
@geryogam geryogam changed the title Update descriptor.rst Correct classmethod emulation in Descriptor HowTo Guide Jun 8, 2019
@geryogam geryogam changed the title Correct classmethod emulation in Descriptor HowTo Guide bpo-37203: Correct classmethod emulation in Descriptor HowTo Guide Jun 8, 2019
@geryogam geryogam changed the title bpo-37203: Correct classmethod emulation in Descriptor HowTo Guide bpo-37203: Correct classmethod Python emulation in the Descriptor HowTo Guide Jun 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review docs Documentation in the Doc dir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants