Skip to content

Commit

Permalink
types: remove singleton union in globals (spack#47282)
Browse files Browse the repository at this point in the history
  • Loading branch information
haampie authored Oct 30, 2024
1 parent cbf4d39 commit 8892c87
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 29 deletions.
11 changes: 2 additions & 9 deletions lib/spack/spack/caches.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

"""Caches used by Spack to store data"""
import os
from typing import Union

import llnl.util.lang
from llnl.util.filesystem import mkdirp
Expand All @@ -32,12 +31,8 @@ def _misc_cache():
return spack.util.file_cache.FileCache(path)


FileCacheType = Union[spack.util.file_cache.FileCache, llnl.util.lang.Singleton]

#: Spack's cache for small data
MISC_CACHE: Union[spack.util.file_cache.FileCache, llnl.util.lang.Singleton] = (
llnl.util.lang.Singleton(_misc_cache)
)
MISC_CACHE: spack.util.file_cache.FileCache = llnl.util.lang.Singleton(_misc_cache) # type: ignore


def fetch_cache_location():
Expand Down Expand Up @@ -74,6 +69,4 @@ def store(self, fetcher, relative_dest):


#: Spack's local cache for downloaded source archives
FETCH_CACHE: Union[spack.fetch_strategy.FsCache, llnl.util.lang.Singleton] = (
llnl.util.lang.Singleton(_fetch_cache)
)
FETCH_CACHE: spack.fetch_strategy.FsCache = llnl.util.lang.Singleton(_fetch_cache) # type: ignore
12 changes: 4 additions & 8 deletions lib/spack/spack/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ def print_section(self, section: str, blame: bool = False, *, scope=None) -> Non
@contextlib.contextmanager
def override(
path_or_scope: Union[ConfigScope, str], value: Optional[Any] = None
) -> Generator[Union[lang.Singleton, Configuration], None, None]:
) -> Generator[Configuration, None, None]:
"""Simple way to override config settings within a context.
Arguments:
Expand Down Expand Up @@ -756,9 +756,7 @@ def override(
COMMAND_LINE_SCOPES: List[str] = []


def _add_platform_scope(
cfg: Union[Configuration, lang.Singleton], name: str, path: str, writable: bool = True
) -> None:
def _add_platform_scope(cfg: Configuration, name: str, path: str, writable: bool = True) -> None:
"""Add a platform-specific subdirectory for the current platform."""
platform = spack.platforms.host().name
scope = DirectoryConfigScope(
Expand Down Expand Up @@ -792,9 +790,7 @@ def config_paths_from_entry_points() -> List[Tuple[str, str]]:
return config_paths


def _add_command_line_scopes(
cfg: Union[Configuration, lang.Singleton], command_line_scopes: List[str]
) -> None:
def _add_command_line_scopes(cfg: Configuration, command_line_scopes: List[str]) -> None:
"""Add additional scopes from the --config-scope argument, either envs or dirs."""
import spack.environment.environment as env # circular import

Expand Down Expand Up @@ -875,7 +871,7 @@ def create() -> Configuration:


#: This is the singleton configuration instance for Spack.
CONFIG: Union[Configuration, lang.Singleton] = lang.Singleton(create)
CONFIG: Configuration = lang.Singleton(create) # type: ignore


def add_from_file(filename: str, scope: Optional[str] = None) -> None:
Expand Down
13 changes: 6 additions & 7 deletions lib/spack/spack/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import spack.provider_index
import spack.spec
import spack.tag
import spack.util.file_cache
import spack.util.git
import spack.util.naming as nm
import spack.util.path
Expand Down Expand Up @@ -589,7 +590,7 @@ def __init__(
self,
package_checker: FastPackageChecker,
namespace: str,
cache: "spack.caches.FileCacheType",
cache: spack.util.file_cache.FileCache,
):
self.checker = package_checker
self.packages_path = self.checker.packages_path
Expand Down Expand Up @@ -682,7 +683,7 @@ class RepoPath:
def __init__(
self,
*repos: Union[str, "Repo"],
cache: Optional["spack.caches.FileCacheType"],
cache: Optional[spack.util.file_cache.FileCache],
overrides: Optional[Dict[str, Any]] = None,
) -> None:
self.repos: List[Repo] = []
Expand Down Expand Up @@ -964,7 +965,7 @@ def __init__(
self,
root: str,
*,
cache: "spack.caches.FileCacheType",
cache: spack.util.file_cache.FileCache,
overrides: Optional[Dict[str, Any]] = None,
) -> None:
"""Instantiate a package repository from a filesystem path.
Expand Down Expand Up @@ -1439,9 +1440,7 @@ def _path(configuration=None):
return create(configuration=configuration)


def create(
configuration: Union["spack.config.Configuration", llnl.util.lang.Singleton]
) -> RepoPath:
def create(configuration: spack.config.Configuration) -> RepoPath:
"""Create a RepoPath from a configuration object.
Args:
Expand All @@ -1464,7 +1463,7 @@ def create(


#: Singleton repo path instance
PATH: Union[RepoPath, llnl.util.lang.Singleton] = llnl.util.lang.Singleton(_path)
PATH: RepoPath = llnl.util.lang.Singleton(_path) # type: ignore

# Add the finder to sys.meta_path
REPOS_FINDER = ReposFinder()
Expand Down
7 changes: 2 additions & 5 deletions lib/spack/spack/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@
DEFAULT_INSTALL_TREE_ROOT = os.path.join(spack.paths.opt_path, "spack")


ConfigurationType = Union["spack.config.Configuration", "llnl.util.lang.Singleton"]


def parse_install_tree(config_dict):
"""Parse config settings and return values relevant to the store object.
Expand Down Expand Up @@ -207,7 +204,7 @@ def __reduce__(self):
)


def create(configuration: ConfigurationType) -> Store:
def create(configuration: spack.config.Configuration) -> Store:
"""Create a store from the configuration passed as input.
Args:
Expand Down Expand Up @@ -240,7 +237,7 @@ def _create_global() -> Store:


#: Singleton store instance
STORE: Union[Store, llnl.util.lang.Singleton] = llnl.util.lang.Singleton(_create_global)
STORE: Store = llnl.util.lang.Singleton(_create_global) # type: ignore


def reinitialize():
Expand Down

0 comments on commit 8892c87

Please sign in to comment.