[issue40066] Enum._convert should change __repr__ and/or __str__ to use module name instead of class name

2020-10-08 Thread Vedran Čačić

Vedran Čačić  added the comment:

> - do you think the change has merit?

Absolutely.

> - why /shouldn't/ we make the change?

Well, standard backward compatibility stuff. :-)

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40066] Enum._convert should change __repr__ and/or __str__ to use module name instead of class name

2020-10-05 Thread Ethan Furman


Ethan Furman  added the comment:

Python-Dev thread [0], summary below:

> As you may have noticed, Enums are starting to pop up all
> over the stdlib [1].
>
> To facilitate transforming existing module constants to
> IntEnums there is `IntEnum._convert_`.  In Issue36548 [2]
> Serhiy modified the __repr__ of RegexFlag:
>
>   >>> import re
>   >>> re.I
>   re.IGNORECASE
>
> I think for converted constants that that looks nice.
>  For anyone that wants the actual value, it is of course
> available as the `.value` attribute:
>
>   >>> re.I.value
>   2
>
> I'm looking for arguments relating to:
>
> - should _convert_ make the default __repr__ be
>   module_name.member_name?
>
> - should _convert_ make the default __str__ be the same,
>   or be the numeric value?

After discussions with Guido I made a (largely done) PR [3] which:

for stdlib global constants (such as RE)
   - repr() -> uses `module.member_name`
   - str() -> uses `member_name`

for stdlib non-global constants, and enums in general
   - repr() -> uses `class.member_name`
   - str() -> uses `member_name`

The questions I would most appreciate an answer to at this point:

- do you think the change has merit?
- why /shouldn't/ we make the change?

As a reminder, the underlying issue is trying to keep at least the stdlib Enum 
representations the same for those that are replacing preexisting constants.


[0] 
https://mail.python.org/archives/list/python-...@python.org/message/CHQW6THTDYNPPFWQ2KDDTUYSAJDCZFNP/

[1] I'm working on making their creation faster.  If anyone wanted to convert 
EnumMeta to C I would be grateful.

[2] https://bugs.python.org/issue36548

[3] https://github.com/python/cpython/pull/22392

--
nosy: +rhettinger

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40066] Enum._convert should change __repr__ and/or __str__ to use module name instead of class name

2020-09-25 Thread Ethan Furman


Ethan Furman  added the comment:

At this point, the PR has made the following changes:

- normal Enums
  - repr() -> "classname.membername"
  - str()  -> "membername"

- stdlib Enums available as module attributes (RegexFlag, AddressFamily, etc.)
  - repr() -> "modulename.membername"
  - str()  -> "membername"

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40066] Enum._convert should change __repr__ and/or __str__ to use module name instead of class name

2020-09-23 Thread Ethan Furman


Change by Ethan Furman :


--
keywords: +patch
pull_requests: +21433
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/22392

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40066] Enum._convert should change __repr__ and/or __str__ to use module name instead of class name

2020-09-14 Thread Vedran Čačić

Vedran Čačić  added the comment:

Noone said it is a requirement, I just said it would be nice to have it 
factored out as a decorator or something instead of having to write __repr__ 
over and over again.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40066] Enum._convert should change __repr__ and/or __str__ to use module name instead of class name

2020-09-14 Thread Ethan Furman


Ethan Furman  added the comment:

"repr as inverse of eval" is nice to have, but it is not a requirement.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40066] Enum._convert should change __repr__ and/or __str__ to use module name instead of class name

2020-09-14 Thread Vedran Čačić

Vedran Čačić  added the comment:

If it's considered to be not too backwards-incompatible, I think it would be 
nice to have str different from repr. That way we can finetune what exactly we 
need. But we can already do almost exactly that with *int* instead of *str*, so 
it's not too compelling.

Much more important thing is the "repr as inverse of eval". Is there any way we 
can have that for our own enums (as a mixin or a decorator)?

@module_global(re)
class RegexFlag(Enum):
...

It would be fantastic. :-)

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40066] Enum._convert should change __repr__ and/or __str__ to use module name instead of class name

2020-09-14 Thread Ethan Furman


Ethan Furman  added the comment:

Looks like the `re` module's flags have been updated separately in issue36548:

  >>> import re
  >>> re.I
  re.IGNORECASE

  >>> print(re.I)
  # should also be re.IGNORECASE

  >>> re.I|re.S|re.X
  re.IGNORECASE|re.DOTALL|re.VERBOSE


For stdlib Enum conversions are we happy with that?  Or should __str__ just 
print the numeric value?

--
nosy: +ezio.melotti, mrabarnett, serhiy.storchaka
versions: +Python 3.10 -Python 3.9

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40066] Enum._convert should change __repr__ and/or __str__ to use module name instead of class name

2020-04-10 Thread Vedran Čačić

Vedran Čačić  added the comment:

> _in some cases when enum instances are exposed as module globals_

Yes. And repr should be inverse of eval, but it's probably too late for that. 
:-/

--
nosy: +veky

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40066] Enum._convert should change __repr__ and/or __str__ to use module name instead of class name

2020-03-25 Thread Ethan Furman


New submission from Ethan Furman :

Serhiy had the idea of having Enum._convert also modify the __str__ and 
__repr__ of newly created enumerations to display the module name instead of 
the enumeration name (https://bugs.python.org/msg325007):

--> socket.AF_UNIX
   ==>  

--> print(socket.AF_UNIX)
AddressFamily.AF_UNIX==>  socket.AF_UNIX

Thoughts?

--
assignee: ethan.furman
messages: 365019
nosy: barry, eli.bendersky, ethan.furman
priority: normal
severity: normal
status: open
title: Enum._convert should change __repr__ and/or __str__ to use module name 
instead of class name
type: enhancement
versions: Python 3.9

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com