Skip to content

bpo-41817: use new StrEnum to ensure all members are strings#22348

Merged
ethanfurman merged 2 commits into
python:masterfrom
ethanfurman:enum-tkinter_eventtype
Sep 22, 2020
Merged

bpo-41817: use new StrEnum to ensure all members are strings#22348
ethanfurman merged 2 commits into
python:masterfrom
ethanfurman:enum-tkinter_eventtype

Conversation

@ethanfurman

@ethanfurman ethanfurman commented Sep 22, 2020

Copy link
Copy Markdown
Member

@ethanfurman

Copy link
Copy Markdown
Member Author

Terry, if the default for stdlib __str__ for stdlib enums changes to module.enum_member_name are you okay with changing the custome __str__ you are using for the EventType enum at the moment?

@serhiy-storchaka

Copy link
Copy Markdown
Member

I think it would be better to restore the default __str__: __str__ = str.__str__ (it looks not safe to change __str__ of str subclass) and change self.type in Event.__repr__() to getattr(self.type, 'name', self.type).

@terryjreedy

terryjreedy commented Sep 22, 2020

Copy link
Copy Markdown
Member

Serhiy is indeed the one to answer the question as he is the tkinter maintainer. As a user, my concern is with breaking existing code and the possible uses of str(). EventTypes are used for the event.type attributes of events passed to event handlers. If a handler can be triggered by more that one type of event and the handler cares which, one condition would be event.type == EventType.Sometype, for which the string representations matter not. (I would probably define `Ev = tk.EventType if doing much of this.)

Conditions like str(event.type) == 'KeyPress' would break, and I don't consider their existence to be out of the question, as we use strings to bind the event. Before this issue, I might have done this. (I had to experiment to be sure of how to use the enum 'properly'.) The NMT tkinter reference does not mention EventType. And, of course, the mapping and code that uses the names predates enums. For output to a user, the module prefix might be noisy.

@ethanfurman

ethanfurman commented Sep 22, 2020

Copy link
Copy Markdown
Member Author

Terry, fair enough.

Serhiy, there are no issues with a custom __str__ on an Enum class, even when it is subclassing str. The question is whether we want str(EventType.KeyPress) to be "KeyPress" or "2".

As Serhiy explained on Python Dev: The issue with having a custom __str__ on a string subclass is that

str(subclassed_thing) != subclassed_thing

and it should be.

@serhiy-storchaka

Copy link
Copy Markdown
Member

I am also the one who introduced EventType. And I now think that maybe making __str__ returning a name instead of value was mistake. The main reason of defining __str__ was to use it in Event.__repr__().

But this is a different issue (I am working on it, the hard part is writing tests). This issue is about trailing commas which convert values into tuples. Thank you Ethan for catching this! Let to fix it.

@serhiy-storchaka

Copy link
Copy Markdown
Member

Would be nice to add a NEWS entry. This was a serious bug.

@ethanfurman

Copy link
Copy Markdown
Member Author

Adding news entry.

@terryjreedy

Copy link
Copy Markdown
Member

I cannot think of any use for the arbitrary numbers, whether string or int, added by tkinter to the tk strings.

@ethanfurman

Copy link
Copy Markdown
Member Author

Serhiy, thank you for the explanation on Python-Dev about str() vs __str__. I'm fixing StrEnum now to use str.__str__.

@miss-islington

Copy link
Copy Markdown
Contributor

Thanks @ethanfurman for the PR 🌮🎉.. I'm working now to backport this PR to: 3.8.
🐍🍒⛏🤖

@miss-islington

Copy link
Copy Markdown
Contributor

Thanks @ethanfurman for the PR 🌮🎉.. I'm working now to backport this PR to: 3.9.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Sep 22, 2020
…H-22348)

* use new StrEnum to ensure all members are strings
(cherry picked from commit ea0711a)

Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
@bedevere-bot

Copy link
Copy Markdown

GH-22366 is a backport of this pull request to the 3.8 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Sep 22, 2020
…H-22348)

* use new StrEnum to ensure all members are strings
(cherry picked from commit ea0711a)

Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
@bedevere-bot

Copy link
Copy Markdown

GH-22367 is a backport of this pull request to the 3.9 branch.

xzy3 pushed a commit to xzy3/cpython that referenced this pull request Oct 18, 2020
@ethanfurman
ethanfurman deleted the enum-tkinter_eventtype branch April 15, 2021 14:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants