Skip to content

gh-148740: Fix uuid CLI with custom UUIDs for UUIDv3/v5 namespaces#148741

Merged
picnixz merged 9 commits intopython:mainfrom
sumpygump:fix/148740-uuid-cli-usage
Apr 29, 2026
Merged

gh-148740: Fix uuid CLI with custom UUIDs for UUIDv3/v5 namespaces#148741
picnixz merged 9 commits intopython:mainfrom
sumpygump:fix/148740-uuid-cli-usage

Conversation

@sumpygump
Copy link
Copy Markdown

@sumpygump sumpygump commented Apr 18, 2026

  • This applies to generating version 3 and 5 UUIDs
  • Will correctly bail when invalid UUID is supplied as namespace

Example usage with correction:

./python -m uuid --uuid uuid5 --namespace '0d6a16cc-34a7-47d8-b660-214d0ae184d2' --name some.user
a32f4562-2058-5b20-8055-5f38b28285b6

Example error if invalid UUID is supplied as --namespace argument:

./python -m uuid --uuid uuid5 --namespace 'invalid uuid' --name some.user
usage: python -m uuid [-h] [-u {uuid1,uuid3,uuid4,uuid5,uuid6,uuid7,uuid8}] [-n {any UUID,@dns,@url,@oid,@x500}] [-N NAME] [-C NUM]
python -m uuid: error: badly formed hexadecimal UUID string: 'invalid uuid'.

My solution was to update the argparse options to use metavar instead of choices for the namespace
argument. This allows to supply suggestions of options that can be supplied for the --namespace arguments instead of requiring ONLY the ones in the list.

My intent was to keep the help message consistent with how it looked before. Note that it still displays the possible values in the help text for the namespace: -n, --namespace {any UUID,@dns,@url,@oid,@x500}:

./python -m uuid --help
usage: python -m uuid [-h] [-u {uuid1,uuid3,uuid4,uuid5,uuid6,uuid7,uuid8}] [-n {any UUID,@dns,@url,@oid,@x500}] [-N NAME] [-C NUM]

Generate a UUID using the selected UUID function.

options:
  -h, --help            show this help message and exit
  -u, --uuid {uuid1,uuid3,uuid4,uuid5,uuid6,uuid7,uuid8}
                        function to generate the UUID (default: uuid4)
  -n, --namespace {any UUID,@dns,@url,@oid,@x500}
                        uuid3/uuid5 only: a UUID, or a well-known predefined UUID addressed by namespace name (default: None)
  -N, --name NAME       uuid3/uuid5 only: name used as part of generating the UUID (default: None)
  -C, --count NUM       generate NUM fresh UUIDs (default: 1)

 - This applies to generating version 3 and 5 UUIDs
 - Will correctly bail when invalid UUID is supplied as namespace
@python-cla-bot
Copy link
Copy Markdown

python-cla-bot Bot commented Apr 18, 2026

All commit authors signed the Contributor License Agreement.

CLA signed

@picnixz
Copy link
Copy Markdown
Member

picnixz commented Apr 20, 2026

  • Please update the docs for this.

Comment thread Lib/uuid.py
@picnixz
Copy link
Copy Markdown
Member

picnixz commented Apr 20, 2026

Please avoid updating your branch if the CI is green. See our devguide. It wastes CI resources

Comment thread Lib/test/test_uuid.py Outdated
Comment thread Lib/test/test_uuid.py Outdated
Comment thread Lib/test/test_uuid.py Outdated
Comment thread Lib/uuid.py Outdated
Comment thread Misc/NEWS.d/next/Library/2026-04-18-17-37-13.gh-issue-148740.sYnFi0.rst Outdated
sumpygump and others added 2 commits April 20, 2026 12:14
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Copy link
Copy Markdown
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that there are some uuid3 tests that are put after the uuid4 tests. Can you move all your uuid3 tests before the existing uuid4 tests and update the comment of the uuid3 test about uuid5 format (it is in the existing test)

@sumpygump
Copy link
Copy Markdown
Author

@picnixz I reordered the tests and fixed the comment you noticed. Thanks.

@picnixz picnixz self-requested a review April 23, 2026 21:07
Comment thread Lib/uuid.py Outdated
Comment thread Lib/uuid.py Outdated
Comment thread Lib/test/test_uuid.py Outdated
Comment thread Lib/test/test_uuid.py Outdated
@picnixz picnixz changed the title gh-148740: Allow setting namespace from command line usage of uuid module gh-148740: Fix uuid --uuid uuid{3,5} --namespace with custom UUIDs Apr 27, 2026
 - Update parser errror to be more succinct
 - Reorder tests so that good inputs come before invalid inputs
@picnixz picnixz self-requested a review April 28, 2026 15:30
@picnixz picnixz changed the title gh-148740: Fix uuid --uuid uuid{3,5} --namespace with custom UUIDs gh-148740: Fix uuid CLI with custom UUIDs for UUIDv3/v5 namespaces Apr 29, 2026
@picnixz picnixz merged commit f1588d4 into python:main Apr 29, 2026
55 checks passed
@picnixz picnixz added the needs backport to 3.14 bugs and security fixes label Apr 29, 2026
@miss-islington-app
Copy link
Copy Markdown

Thanks @sumpygump for the PR, and @picnixz for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

@picnixz picnixz added the needs backport to 3.13 bugs and security fixes label Apr 29, 2026
@miss-islington-app
Copy link
Copy Markdown

Thanks @sumpygump for the PR, and @picnixz for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

@miss-islington-app
Copy link
Copy Markdown

Sorry, @sumpygump and @picnixz, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker f1588d460db97086f5ea28b5797ccdcfffb0307f 3.13

@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented Apr 29, 2026

GH-149152 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Apr 29, 2026
@picnixz picnixz removed the needs backport to 3.13 bugs and security fixes label Apr 29, 2026
@picnixz
Copy link
Copy Markdown
Member

picnixz commented Apr 29, 2026

@sumpygump Can you check if 3.13 is affected please and if so do the backport? TiA!

@sumpygump
Copy link
Copy Markdown
Author

@picnixz I checked python 3.13; this is NOT an issue in 3.13.

picnixz added a commit that referenced this pull request Apr 29, 2026
…spaces (GH-148741) (#149152)

gh-148740: Fix `uuid` CLI with custom UUIDs for UUIDv3/v5 namespaces (GH-148741)
(cherry picked from commit f1588d4)

Co-authored-by: Jansen Price <sumpygump@gmail.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
manoj-km24 pushed a commit to manoj-km24/cpython that referenced this pull request Apr 29, 2026
…paces (python#148741)

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
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.

2 participants