This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: UUID objects can't be casted by `hex()`
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: fcurella, mark.dickinson, ronaldoussoren, serhiy.storchaka, thatiparthy
Priority: normal Keywords: patch

Created on 2018-10-30 15:12 by fcurella, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 10244 closed fcurella, 2018-10-30 15:22
Messages (9)
msg328929 - (view) Author: (fcurella) * Date: 2018-10-30 15:12
Casting a UUID to an `int` or to a string works as expected:

```
import uuid

value = uuid.UUID()
str(value)
int(value)
```

but casting to an `hex()` raises an exception:

```
import uuid

value = uuid.UUID()

# uuid instances already have the correct value stored in the `.hex` attribute
value.hex

# this raises `TypeError: 'UUID' object cannot be interpreted as an integer`
hex(value)

# this behaves correctly
hex(value.int)

```

Adding support for `hex()` should be simple enough as adding the following to the UUID class in https://github.com/python/cpython/blob/54752533b2ed1c898ffe5ec2e795c6910ee46a39/Lib/uuid.py#L69:

```
def __index__(self):
    return self.int
```
msg328933 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2018-10-30 15:48
IMHO implementing "__index__" for UUID would not the correct solution. That method is meant be used by integer-like objects. One of the side effects of implementing "__index__" is that indexing lists with UUID instances would start to work, which is IMHO not correct.
msg328935 - (view) Author: (fcurella) * Date: 2018-10-30 16:09
I must admit I was surprised to find out that `hex()` uses `__index__` (which is supposed to return an integer) and not something like a `__hex__` method returning the hex.

Maybe we should change the behaviour of `hex()` instead? (in a backward-compatible way, of course)
msg328936 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-10-30 16:58
hex() used the __hex__() special method in Python 2. This was changed in Python 3 for purpose.

https://docs.python.org/3/whatsnew/3.0.html#operators-and-special-methods
msg328940 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2018-10-30 19:47
Agreed that UUID should not implement `__index__`. I wouldn't expect to be able to use a UUID as a list index, for example.

This is marked as "behavior", but I have a hard time seeing this as a bug. It's not even a usability bug, given how easy it is to get the hex value of a UUID with `.hex`.

Maybe the UUID class should grow a `__format__` implementation that respects the `x` format specifier?
msg328941 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-10-30 19:55
Note that str() for UUIDs already returns the hexadecimal representation, just with minuses.

I don't see large usability difference between 'uuid={:x}'.format(u) and 'uuid={.hex}'.format(u). And in many case 'uuid={}'.format(u) is appropriate as well.
msg328947 - (view) Author: (fcurella) * Date: 2018-10-30 21:08
marking as 'enhancement'.
msg328974 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2018-10-31 07:46
> I don't see large usability difference between 'uuid={:x}'.format(u) and 'uuid={.hex}'.format(u).

Agreed.

@fcurella: marking as enhancement is fine, but you need to be specific about what exact enhancement you're proposing. Adding __hex__ back isn't a realistic option; it was deliberately removed in Python 3.
msg328996 - (view) Author: (fcurella) * Date: 2018-10-31 15:12
I'm not sure what can be done to make UUIDs work with `hex()`. The only way I see is to add back something _like_ `__hex__`, but with a different name.

But in that case, I can see how the same arguments that were originally brought up against `__hex__` could apply to the new dunder method.

I'm closing the issue as 'wont fix'.
History
Date User Action Args
2022-04-11 14:59:07adminsetgithub: 79296
2018-10-31 15:12:38fcurellasetstatus: open -> closed
resolution: wont fix
messages: + msg328996

stage: patch review -> resolved
2018-10-31 07:46:49mark.dickinsonsetmessages: + msg328974
2018-10-30 21:09:06fcurellasetstatus: open
2018-10-30 21:08:36fcurellasetstatus: open -> (no value)
type: behavior -> enhancement
messages: + msg328947
2018-10-30 19:55:22serhiy.storchakasetmessages: + msg328941
2018-10-30 19:47:09mark.dickinsonsetmessages: + msg328940
2018-10-30 18:25:39thatiparthysetnosy: + thatiparthy
2018-10-30 16:58:30serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg328936
2018-10-30 16:09:22fcurellasetmessages: + msg328935
2018-10-30 15:48:40ronaldoussorensetnosy: + ronaldoussoren
messages: + msg328933
2018-10-30 15:46:09serhiy.storchakasetnosy: + mark.dickinson
2018-10-30 15:22:20fcurellasetkeywords: + patch
stage: patch review
pull_requests: + pull_request9557
2018-10-30 15:12:16fcurellacreate