Repository navigation
Closed
Labels
3.9 (EOL)end of lifeend of lifestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
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 = None
closed_at = <Date 2021-10-28.16:03:07.339>
created_at = <Date 2020-02-18.19:16:15.217>
labels = ['type-bug', 'library', '3.9']
title = "functools: singledispatchmethod doesn't work with classmethod"
updated_at = <Date 2021-10-28.16:03:07.338>
user = 'https://github.com/vr2262'bugs.python.org fields:
activity = <Date 2021-10-28.16:03:07.338>
actor = 'lukasz.langa'
assignee = 'none'
closed = True
closed_date = <Date 2021-10-28.16:03:07.339>
closer = 'lukasz.langa'
components = ['Library (Lib)']
creation = <Date 2020-02-18.19:16:15.217>
creator = 'Viktor Roytman'
dependencies = []
files = []
hgrepos = []
issue_num = 39679
keywords = ['patch']
message_count = 15.0
messages = ['362233', '362253', '362434', '362442', '371230', '372405', '378344', '380221', '395910', '404147', '404348', '404362', '404363', '405195', '405196']
nosy_count = 13.0
nosy_names = ['ncoghlan', 'glyph', 'lukasz.langa', 'markgrandi', 'levkivskyi', 'miss-islington', 'xtreak', 'mental', 'Viktor Roytman', 'FFY00', 'EugenePY', 'dmkulazhenko', 'AlexWaygood']
pr_nums = ['29034', '29072', '29087']
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue39679'
versions = ['Python 3.9']Metadata
Metadata
Assignees
Labels
3.9 (EOL)end of lifeend of lifestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Activity
vr2262 commentedon Feb 18, 2020
I couldn't get the example given for the interaction between @singledispatchmethod and @classmethod to work https://docs.python.org/3/library/functools.html?highlight=singledispatch#functools.singledispatchmethod
Leads to
Curiously, @staticmethod does work, but not as documented (don't decorate the actual implementations):
Leads to
$ python -m good_staticmethod 0 TrueRemoving @classmethod from the implementation methods doesn't work, though
Traceback (most recent call last): File "/usr/lib/python3.8/runpy.py", line 193, in _run_module_as_main return _run_code(code, main_globals, None, File "/usr/lib/python3.8/runpy.py", line 86, in _run_code exec(code, run_globals) File "/home/viktor/scratch/bad_classmethod_alternative.py", line 20, in <module> print(Negator.neg(0)) File "/usr/lib/python3.8/functools.py", line 911, in _method return method.__get__(obj, cls)(*args, **kwargs) TypeError: _() missing 1 required positional argument: 'arg'tirkarthi commentedon Feb 19, 2020
I guess the method checks for annotation on cls [0] which will be classmethod/staticmethod object in the report and won't have annotations. The annotations should be looked up in the function the classmethod/staticmethod decorator wraps around as in cls.__func__ . Something like below so that the right annotations are picked up. In addition to this the registry should store the type annotation as key to cls or cls.__func__ depending on normal method or classmethod/staticmethod.
[0]
cpython/Lib/functools.py
Line 1076 in ab6423f
vr2262 commentedon Feb 21, 2020
I tried to apply this change but it didn't work, failing with this error
After digging around a bit, this diff seems to work (not sure if there's a better way to do it) (also this one doesn't seem to care whether @staticmethod is applied to the implementation methods):
tirkarthi commentedon Feb 22, 2020
Sorry, I had the part only to detect annotations attached. My part is something similar to yours except it stores the appropriate function in the registry itself instead of passing the arguments at __get__ .
vr2262 commentedon Jun 10, 2020
Sorry to bump this after months of inactivity but is there something I need to do here?
mgrandi commentedon Jun 26, 2020
same issue here, if we can't fix it then maybe we should edit the documentation to not suggest that @classmethod works?
glyph commentedon Oct 9, 2020
I also just discovered this. I thought it must have regressed at some point but I see the docs say "new in 3.8" and I'm using 3.8.
Is there a "no CI for examples in the docs" issue that this could be linked to?
1 remaining item
dmkulazhenko commentedon Jun 16, 2021
Based on what I've read, workaround:
AlexWaygood commentedon Oct 17, 2021
Happily, this bug appears to have been resolved in Python 3.10 due to the fact that a
classmethodwrapping a functionFwill now have an__annotations__dict equal toF.In Python 3.9:
In Python 3.10:
This change appears to have resolved the bug in
functools.singledispatchmethod. The bug remains in Python 3.9, however.ambv commentedon Oct 19, 2021
New changeset ad6d162 by Alex Waygood in branch 'main':
bpo-39679: Add tests for classmethod/staticmethod singledispatchmethods (GH-29034)
ad6d162
ambv commentedon Oct 19, 2021
New changeset c15ba30 by Miss Islington (bot) in branch '3.10':
bpo-39679: Add tests for classmethod/staticmethod singledispatchmethods (GH-29034) (GH-29072)
c15ba30
ambv commentedon Oct 19, 2021
Thanks for the new tests, Alex. Not closing this just yet because maybe we find a way to fix it in 3.9.8+.
ambv commentedon Oct 28, 2021
New changeset 97388c2 by Alex Waygood in branch '3.9':
[3.9] bpo-39679: Fix
singledispatchmethodclassmethod/staticmethodbug (GH-29087)97388c2
ambv commentedon Oct 28, 2021
Thanks for the 3.9 fix, Alex! ✨ 🍰 ✨
P403n1x87 commentedon Jan 18, 2024
Is there a reason why this was not backported to 3.8, when the documentation still says that decorators are supported?
AlexWaygood commentedon Jan 18, 2024
This was fixed in October 2021. Python 3.8 was old enough to enter "security-only" mode in May 2021. Python versions in security-only mode only have docs updates or bugfixes applied to them if the docs update or bugfix relates to a security issue. This bug didn't relate to a security issue.
Revert "IMPR: Use singledispatchmethod with SiteLinkCollection.normal…