classification
Title: tkinter's after() AttributeError with functools.partial (no attribute __name__)
Type: enhancement Stage: resolved
Components: Tkinter Versions: Python 3.11, Python 3.10
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: epaine, miss-islington, phil.tgd, serhiy.storchaka, terry.reedy
Priority: normal Keywords: patch

Created on 2021-06-12 20:53 by phil.tgd, last changed 2021-06-27 09:34 by epaine. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 26812 merged epaine, 2021-06-20 18:59
PR 26921 merged miss-islington, 2021-06-26 23:40
Messages (11)
msg395712 - (view) Author: Philip Sundt (phil.tgd) Date: 2021-06-12 20:53
```
>>> import tkinter
>>> from functools import partial
>>> r=tkinter.Tk()
>>> r.after(500, partial(print, "lol"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.7/tkinter/__init__.py", line 755, in after
    callit.__name__ = func.__name__
AttributeError: 'functools.partial' object has no attribute '__name__'
```
msg395735 - (view) Author: E. Paine (epaine) * Date: 2021-06-13 08:51
Reproducible in Python 3.9. The issue occurs because functools.partial is a class, not function. I believe the fix is simply something like:

try:
    callit.__name__ = func.__name__
except AttributeError:
    callit.__name__ = type(func).__name__

This will use the name 'partial'. The lack of '__name__' is noted in the functools docs so I agree with Philip that this is an issue with tkinter. Philip, do you want to open a pull request for this?

It should be noted that functools.partial is not required in this situation as 'after' takes arguments for the function call:
r.after(500, print, "lol")
msg396095 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-06-18 21:23
The docstring for .after says the 2nd argument must be a 'function'.  Whether this issue is a bugfix or enhancement request depends whether one interprets 'function' as 'callable' or something narrower that only includes objects that necessarily have __name__ attributes.  The latter would exclude partials and instances of user classes with __call__ methods.

While a one-time-use partial as in the example is superfluous, I think other uses and parameterized callback classes should be supported.  So I agree with changing the tkinter code rather than qualifying 'function' with 'has a .__name__ attribute'.  I think 'type(func).__name__' should be safe.
msg396407 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-06-23 10:30
New changeset e9c8f784fa13ea3a51df3b72a498a3896ec9e768 by E-Paine in branch 'main':
bpo-44404: tkinter `after` support callable classes (GH-26812)
https://github.com/python/cpython/commit/e9c8f784fa13ea3a51df3b72a498a3896ec9e768
msg396408 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-06-23 10:34
It is on borderline between new feature and bugfix (depends on your interpretation of the documentation), so I decided to backport it to not yet released 3.10, but not to 3.9 which has been already used for a year.
msg396434 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-06-23 16:52
I was thinking the same.
msg396556 - (view) Author: E. Paine (epaine) * Date: 2021-06-26 20:24
Can this issue be closed?
msg396560 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-06-26 23:44
When the backport is done or Serhiy changes his mind.

Can you change labels on your own PRs?
msg396564 - (view) Author: miss-islington (miss-islington) Date: 2021-06-27 08:02
New changeset e1f3bd2bb50a76ba15a2f8d561e2c9968ae3a1b2 by Miss Islington (bot) in branch '3.10':
bpo-44404: tkinter `after` support callable classes (GH-26812)
https://github.com/python/cpython/commit/e1f3bd2bb50a76ba15a2f8d561e2c9968ae3a1b2
msg396565 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-06-27 08:05
There were some issues with the backporting bot. Seems they were temporary.

Thank you for your contribution E. Paine. For this issue and others.
msg396570 - (view) Author: E. Paine (epaine) * Date: 2021-06-27 09:34
> Can you change labels on your own PRs?

Sadly not (hence why I need to ask for e.g. skip news)
History
Date User Action Args
2021-06-27 09:34:21epainesetmessages: + msg396570
2021-06-27 08:05:21serhiy.storchakasetstatus: open -> closed
resolution: fixed
messages: + msg396565

stage: patch review -> resolved
2021-06-27 08:02:12miss-islingtonsetnosy: + miss-islington
messages: + msg396564
2021-06-26 23:45:12terry.reedysetstage: patch review
2021-06-26 23:44:19terry.reedysetnosy: - miss-islington

messages: + msg396560
stage: patch review -> (no value)
2021-06-26 23:40:18miss-islingtonsetnosy: + miss-islington

pull_requests: + pull_request25494
stage: patch review
2021-06-26 20:24:49epainesetmessages: + msg396556
2021-06-23 16:52:26terry.reedysetmessages: + msg396434
2021-06-23 10:34:48serhiy.storchakasettype: behavior -> enhancement
stage: patch review -> (no value)
messages: + msg396408
versions: - Python 3.9
2021-06-23 10:30:27serhiy.storchakasetmessages: + msg396407
2021-06-20 18:59:35epainesetkeywords: + patch
stage: patch review
pull_requests: + pull_request25394
2021-06-18 21:23:10terry.reedysetnosy: + terry.reedy
messages: + msg396095
2021-06-13 08:51:06epainesetnosy: + serhiy.storchaka, epaine

messages: + msg395735
versions: + Python 3.10, Python 3.11
2021-06-12 20:53:12phil.tgdcreate