Skip to content

gh-148740: Allow setting namespace from command line usage of uuid module#148741

Open
sumpygump wants to merge 4 commits intopython:mainfrom
sumpygump:fix/148740-uuid-cli-usage
Open

gh-148740: Allow setting namespace from command line usage of uuid module#148741
sumpygump wants to merge 4 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
default="uuid4",
help="function to generate the UUID")
parser.add_argument("-n", "--namespace",
choices=["any UUID", *namespaces.keys()],
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I see that it was actually a real bug here.

@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
self.uuid.main()
# Check that exception code is the same as argparse.ArgumentParser.error
self.assertEqual(cm.exception.code, 2)
self.assertIn("error: badly formed hexadecimal UUID string", mock_err.getvalue())
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Make sure that your line does not exceed 80 chars otherwise wrap the call.

Comment thread Lib/test/test_uuid.py

@mock.patch.object(sys, "argv",
["", "-u", "uuid3", "-n", "0d6a16cc-34a7-47d8-b660-214d0ae184d2", "-N", "some.user"])
def test_cli_uuid3_ouputted_with_user_provided_namespace_and_name(self):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Change "user_provided" to "custom" to make it a bit shorter.

Comment thread Lib/test/test_uuid.py
output = stdout.getvalue().strip()
uuid_output = self.uuid.UUID(output)

# Output should be in the form of uuid5
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

uuid3 here

Comment thread Lib/uuid.py
help="function to generate the UUID")
parser.add_argument("-n", "--namespace",
choices=["any UUID", *namespaces.keys()],
metavar=f"{{any UUID,{','.join(namespaces.keys())}}}",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
metavar=f"{{any UUID,{','.join(namespaces.keys())}}}",
metavar=f"{{any UUID,{','.join(namespaces)}}}",

@@ -0,0 +1,2 @@
Fix usage for :mod:`uuid` command line interface to support a custom namespace be
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
Fix usage for :mod:`uuid` command line interface to support a custom namespace be
Fix usage for :mod:`uuid` command-line interface to support a custom namespace be

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants