code: def canonicalize_name(
      name: str, *, validate: bool = False
  ) -> NormalizedName:
      if validate and not _validate_regex.match(name):
          raise InvalidName(f"name is invalid: {name!r}")
      # This is taken from PEP 503.
      value = _canonicalize_regex.sub("-", name).lower()
      return cast(NormalizedName, value)
  
file_path: packaging/src/packaging/utils.py
imports: import re
  from typing import FrozenSet, NewType, Tuple, Union, cast
  from .tags import Tag, parse_tag
  from .version import InvalidVersion, Version
label_desc: Use image node_id 1 for calling a global function with example usage: canonicalize_name(name) and returns: cast