Use Literal now that we have typing_extensions in Spack. (#48172)

Improve our typing by updating some todo locations in the code to use
`Literal` instead of a simple `str`.

Signed-off-by: Todd Gamblin <tgamblin@llnl.gov>
This commit is contained in:
Todd Gamblin 2024-12-18 05:10:14 -08:00 committed by GitHub
parent 7ee1e518b0
commit 30e2b15eea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 16 additions and 9 deletions

View File

@ -6,6 +6,8 @@
from typing import Iterable, List, Tuple, Union
from typing_extensions import Literal
#: Type hint for the low-level dependency input (enum.Flag is too slow)
DepFlag = int
@ -13,7 +15,7 @@
DepTypes = Union[str, List[str], Tuple[str, ...]]
#: Individual dependency types
DepType = str # Python 3.8: Literal["build", "link", "run", "test"]
DepType = Literal["build", "link", "run", "test"]
# Flag values. NOTE: these values are not arbitrary, since hash computation imposes
# the order (link, run, build, test) when depending on the same package multiple times,

View File

@ -12,6 +12,8 @@
import sys
from typing import Callable, Dict, Optional
from typing_extensions import Literal
from llnl.string import comma_or
from llnl.util import tty
from llnl.util.filesystem import (
@ -109,6 +111,9 @@ def view_copy(
tty.debug(f"Can't change the permissions for {dst}")
#: Type alias for link types
LinkType = Literal["hardlink", "hard", "copy", "relocate", "add", "symlink", "soft"]
#: supported string values for `link_type` in an env, mapped to canonical values
_LINK_TYPES = {
"hardlink": "hardlink",
@ -123,7 +128,7 @@ def view_copy(
_VALID_LINK_TYPES = sorted(set(_LINK_TYPES.values()))
def canonicalize_link_type(link_type: str) -> str:
def canonicalize_link_type(link_type: LinkType) -> str:
"""Return canonical"""
canonical = _LINK_TYPES.get(link_type)
if not canonical:
@ -133,7 +138,7 @@ def canonicalize_link_type(link_type: str) -> str:
return canonical
def function_for_link_type(link_type: str) -> LinkCallbackType:
def function_for_link_type(link_type: LinkType) -> LinkCallbackType:
link_type = canonicalize_link_type(link_type)
if link_type == "hardlink":
return view_hardlink
@ -142,7 +147,7 @@ def function_for_link_type(link_type: str) -> LinkCallbackType:
elif link_type == "copy":
return view_copy
assert False, "invalid link type" # need mypy Literal values
assert False, "invalid link type"
class FilesystemView:
@ -166,7 +171,7 @@ def __init__(
projections: Optional[Dict] = None,
ignore_conflicts: bool = False,
verbose: bool = False,
link_type: str = "symlink",
link_type: LinkType = "symlink",
):
"""
Initialize a filesystem view under the given `root` directory with
@ -292,7 +297,7 @@ def __init__(
projections: Optional[Dict] = None,
ignore_conflicts: bool = False,
verbose: bool = False,
link_type: str = "symlink",
link_type: LinkType = "symlink",
):
super().__init__(
root,

View File

@ -26,6 +26,8 @@
import typing
from typing import Any, Callable, Dict, Iterable, List, Optional, Set, Tuple, Type, TypeVar, Union
from typing_extensions import Literal
import llnl.util.filesystem as fsys
import llnl.util.tty as tty
from llnl.util.lang import classproperty, memoized
@ -1009,10 +1011,8 @@ def redistribute_binary(self):
return False
return True
# NOTE: return type should be Optional[Literal['all', 'specific', 'none']] in
# Python 3.8+, but we still support 3.6.
@property
def keep_werror(self) -> Optional[str]:
def keep_werror(self) -> Optional[Literal["all", "specific", "none"]]:
"""Keep ``-Werror`` flags, matches ``config:flags:keep_werror`` to override config.
Valid return values are: