UUID

Info

A UUID is a "Universally Unique Identifier".

It's a standard format for identifiers, like passport numbers, but for anything, not just people in countries.

They look like this:

d48edaa6-871a-4082-a196-4daab372d4a1

The way they are generated makes them sufficiently long and random that you could assume that every UUID generated is unique. Even if it was generated by a different application, database, or system.

So, if your system uses UUIDs to identify your data, you could mix it with the data from some other system that also uses UUIDs with some confidence that their IDs (UUIDs) won't clash with yours.

This wouldn't be true if you just used ints as identifiers, as most databases do.

You can declare a CLI parameter as a UUID:

from uuid import UUID

import typer


def main(user_id: UUID):
    print(f"USER_ID is {user_id}")
    print(f"UUID version is: {user_id.version}")


if __name__ == "__main__":
    typer.run(main)

Your Python code will receive a standard Python UUID object with all its attributes and methods, and as you are annotating your function parameter with that type, you will have type checks, autocompletion in your editor, etc.

Check it:

// Pass a valid UUID v4
$ python main.py d48edaa6-871a-4082-a196-4daab372d4a1

USER_ID is d48edaa6-871a-4082-a196-4daab372d4a1
UUID version is: 4

// An invalid value
$ python main.py 7479706572-72756c6573

Usage: main.py [OPTIONS] USER_ID
Try "main.py --help" for help.

Error: Invalid value for 'USER_ID': 7479706572-72756c6573 is not a valid UUID value
You can ask questions about Typer. Try:
How can I terminate a program?
How to launch applications?
How to add help to CLI argument?