New submission from Géry <gery.o...@gmail.com>:

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\_\_.<locals>.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 arguments.

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.

----------
assignee: docs@python
components: Documentation
messages: 345031
nosy: docs@python, eric.araujo, ezio.melotti, maggyero, mdk, rhettinger, 
willingc
priority: normal
pull_requests: 13785
severity: normal
status: open
title: Correct classmethod emulation in Descriptor HowTo Guide
type: behavior
versions: Python 3.7

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue37203>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to