Add simple UUID module with uuid4()#1137
Conversation
Includes unit test. Signed-off-by: Damien George <damien@micropython.org>
And run it as part of CI. Signed-off-by: Damien George <damien@micropython.org>
Using `bytes.hex()` eliminates an import, and eliminates the call to `.decode()` to convert it to a str. Also run ruff format. Signed-off-by: Damien George <damien@micropython.org>
| class UUID: | ||
| def __init__(self, bytes): | ||
| if len(bytes) != 16: | ||
| raise ValueError("bytes arg must be 16 bytes long") |
There was a problem hiding this comment.
I tried to test if cpython gave the save error here but it gave a much messier exception. Possibly not worth worrying about, this should be user friendly enough.
There was a problem hiding this comment.
Yeah, indeed CPython does give a confusing error.
|
Thanks for the review! |
| def test_unique(self): | ||
| u1 = uuid.uuid4() | ||
| u2 = uuid.uuid4() | ||
| self.assertNotEqual(u1.hex, u2.hex) | ||
| self.assertNotEqual(str(u1), str(u2)) |
There was a problem hiding this comment.
Are there tests to ensure that, for a given port, os.urandom provides unique results? I could only find tests/extmod/os_urandom.py.
Perhaps we should then either test urandom more thoroughly or test uuid uniqueness here more thoroughly? For comparison, CPython generates 1000 UUIDs and ensures they're all unique (see here).
There was a problem hiding this comment.
Testing urandom() is out of scope of this PR. A port needs to make sure that works as expected, and then this module should just be able to rely on that.
We could add a loop here to test a few more uniqueness, but IMO 1000 is excessive.
There was a problem hiding this comment.
Added a test that 10 UUIDs are unique.
| return "-".join((h[0:8], h[8:12], h[12:16], h[16:20], h[20:32])) | ||
|
|
||
| def __repr__(self): | ||
| return "<UUID: %s>" % str(self) |
There was a problem hiding this comment.
This repr output is different to CPython; would it be better to make it compatible?
Python 3.12.3 (main, Jun 19 2026, 12:46:00) [GCC 13.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from uuid import uuid4
>>> uuid4()
UUID('84da63f4-127b-4c34-a7d9-b4321bf6f794')There was a problem hiding this comment.
Yes, could make it the same as CPython, that would be easy.
There was a problem hiding this comment.
Updated to match CPython.
| import os | ||
|
|
||
|
|
||
| class UUID: |
There was a problem hiding this comment.
I'm not sure if other methods/properties would make porting CPython code easier, it's hard to know which are in common use. version and variant seem like they would be high on the list - particularly if we decide to add support for UUIDv7 in the future - but it's probably best to hold off for now and keep it as minimal as possible.
There was a problem hiding this comment.
I also don't really know what users are going to need...
|
@mattytrentini if you think this is not enough functionality then we can consider instead your #504 implementation. That's fundamentally different in that it stores the UUID as an |
No, I think this minimal approach is best, at least for now. I've tried to scour some popular applications that used uuid but of the few I looked at, they just used it without calling any other methods. |
|
It's probably a good idea to eventually support more functions, like the namespace UUIDs. And adding a |
Signed-off-by: Damien George <damien@micropython.org>
Signed-off-by: Damien George <damien@micropython.org>
|
I exposed the |
|
I think this now strikes an excellent balance between functional and minimal. Looks good to me! |
|
Thanks @mattytrentini ! |
Summary
This PR pulls in @andrewleech 's minimal UUID implementation from https://github.com/pfalcon/pycopy-lib (originally submitted here as #312, #313 long ago).
Then I made a few updates to it:
manifest.pyunittestand run it under CIbytes.hex()instead ofbinascii.hexlify().decode()ruff formatTesting
Test added that runs under CI.
Trade-offs and Alternatives
This adds a minimal implementation of
uuid.uuid4()which is arguably the more important of the uuidX functions.Alternatives:
Generative AI
Not used.