Skip to content

Command line usage of uuid module doesn't allow to supply custom namespace UUID #148740

@sumpygump

Description

@sumpygump

Bug report

Bug description:

The ability to generate a uuid version 3 or 5 that requires a namespace and name
argument is not fully implemented from command line (e.g. python -m uuid)

Using one of the built in namespaces works fine:

python -m uuid --uuid uuid5 --namespace @oid --name some.user
a9711298-a42a-5ae8-8bbc-7c1e99aced12

If you try to supply a UUID for the namespace you get an error about invalid usage of
the command:

python -m uuid --uuid uuid5 --namespace '0d6a16cc-34a7-47d8-b660-214d0ae184d2' --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: argument -n/--namespace: invalid choice: '0d6a16cc-34a7-47d8-b660-214d0ae184d2' (choose from any UUID, @dns, @url, @oid, @x500)

It is possible to complete this task with python, so the functionality is correct, it is
just not possible from the command line invocation of the module. Here is an example
of working python code to achieve generating a version 5 UUID with custom namespace UUID:

>>> import uuid
>>> uuid.uuid5(uuid.UUID('0d6a16cc-34a7-47d8-b660-214d0ae184d2'), 'some.user')
UUID('a32f4562-2058-5b20-8055-5f38b28285b6')

Incidentally, based on the choices provided by --help it allows the user to use the
string literal "any UUID" as a namespace argument, but doing so fails in a worse way:

python -m uuid --uuid uuid5 --namespace 'any UUID' --name some.user
Traceback (most recent call last):
  File "/cpython/Lib/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
                     "__main__", mod_spec)
  File "/cpython/Lib/runpy.py", line 87, in _run_code
    exec(code, run_globals)
    ~~~~^^^^^^^^^^^^^^^^^^^
  File "/cpython/Lib/uuid.py", line 1008, in <module>
    main()
    ~~~~^^
  File "/cpython/Lib/uuid.py", line 987, in main
    namespace = namespaces[namespace] if namespace in namespaces else UUID(namespace)
                                                                      ~~~~^^^^^^^^^^^
  File "/cpython/Lib/uuid.py", line 219, in __init__
    raise ValueError('badly formed hexadecimal UUID string')
ValueError: badly formed hexadecimal UUID string

It is apparent the argparse configuration is "gating" the ability for the user to supply
their own UUID because it fails on any value that is not in the list of choices.

Suggested corrections:

  1. Allow the user to enter a UUID as a value for the --namespace argument to satisfy
    the 'any UUID' option (i.e. provide a UUID that is not in the list of standard
    NAMESPACE UUIDs).
  2. Fail more gracefully if the --namespace argument is not a valid UUID or
    namespace key.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions