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

tkinter's after() AttributeError with functools.partial (no attribute __name__) #88570

Closed
philtgd mannequin opened this issue Jun 12, 2021 · 11 comments
Closed

tkinter's after() AttributeError with functools.partial (no attribute __name__) #88570

philtgd mannequin opened this issue Jun 12, 2021 · 11 comments
Labels
3.10 only security fixes 3.11 only security fixes topic-tkinter type-feature A feature request or enhancement

Comments

@philtgd
Copy link
Mannequin

philtgd mannequin commented Jun 12, 2021

BPO 44404
Nosy @terryjreedy, @serhiy-storchaka, @miss-islington, @E-Paine
PRs
  • bpo-44404: tkinter after support callable classes #26812
  • [3.10] bpo-44404: tkinter after support callable classes (GH-26812) #26921
  • 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-06-27.08:05:21.961>
    created_at = <Date 2021-06-12.20:53:12.881>
    labels = ['type-feature', 'expert-tkinter', '3.10', '3.11']
    title = "tkinter's after() AttributeError with functools.partial (no attribute __name__)"
    updated_at = <Date 2021-06-27.09:34:21.582>
    user = 'https://bugs.python.org/philtgd'

    bugs.python.org fields:

    activity = <Date 2021-06-27.09:34:21.582>
    actor = 'epaine'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-06-27.08:05:21.961>
    closer = 'serhiy.storchaka'
    components = ['Tkinter']
    creation = <Date 2021-06-12.20:53:12.881>
    creator = 'phil.tgd'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 44404
    keywords = ['patch']
    message_count = 11.0
    messages = ['395712', '395735', '396095', '396407', '396408', '396434', '396556', '396560', '396564', '396565', '396570']
    nosy_count = 5.0
    nosy_names = ['terry.reedy', 'serhiy.storchaka', 'miss-islington', 'epaine', 'phil.tgd']
    pr_nums = ['26812', '26921']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue44404'
    versions = ['Python 3.10', 'Python 3.11']

    @philtgd
    Copy link
    Mannequin Author

    philtgd mannequin commented Jun 12, 2021

    >>> 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__'
    

    @philtgd philtgd mannequin added 3.9 only security fixes type-bug An unexpected behavior, bug, or error topic-tkinter labels Jun 12, 2021
    @E-Paine
    Copy link
    Mannequin

    E-Paine mannequin commented Jun 13, 2021

    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")

    @E-Paine E-Paine mannequin added 3.10 only security fixes 3.11 only security fixes labels Jun 13, 2021
    @terryjreedy
    Copy link
    Member

    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.

    @serhiy-storchaka
    Copy link
    Member

    New changeset e9c8f78 by E-Paine in branch 'main':
    bpo-44404: tkinter after support callable classes (GH-26812)
    e9c8f78

    @serhiy-storchaka
    Copy link
    Member

    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.

    @serhiy-storchaka serhiy-storchaka added type-feature A feature request or enhancement and removed 3.9 only security fixes type-bug An unexpected behavior, bug, or error labels Jun 23, 2021
    @terryjreedy
    Copy link
    Member

    I was thinking the same.

    @E-Paine
    Copy link
    Mannequin

    E-Paine mannequin commented Jun 26, 2021

    Can this issue be closed?

    @terryjreedy
    Copy link
    Member

    When the backport is done or Serhiy changes his mind.

    Can you change labels on your own PRs?

    @miss-islington
    Copy link
    Contributor

    New changeset e1f3bd2 by Miss Islington (bot) in branch '3.10':
    bpo-44404: tkinter after support callable classes (GH-26812)
    e1f3bd2

    @serhiy-storchaka
    Copy link
    Member

    There were some issues with the backporting bot. Seems they were temporary.

    Thank you for your contribution E. Paine. For this issue and others.

    @E-Paine
    Copy link
    Mannequin

    E-Paine mannequin commented Jun 27, 2021

    Can you change labels on your own PRs?

    Sadly not (hence why I need to ask for e.g. skip news)

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.10 only security fixes 3.11 only security fixes topic-tkinter type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants