Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions lerna/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,22 @@
SweeperManager = _rust.SweeperManager

__all__ = [
"__version__",
"MissingConfigException",
"main",
"utils",
"TaskFunction",
"compose",
"initialize",
"initialize_config_module",
"initialize_config_dir",
"CallbackManager",
"JobReturn",
"ConfigResult",
"RustFileConfigSource",
"ConfigSourceManager",
"RustBasicLauncher",
"JobReturn",
"LauncherManager",
"MissingConfigException",
"RustBasicLauncher",
"RustBasicSweeper",
"RustFileConfigSource",
"SweeperManager",
"TaskFunction",
"__version__",
"compose",
"initialize",
"initialize_config_dir",
"initialize_config_module",
"main",
"utils",
]
14 changes: 7 additions & 7 deletions lerna/_internal/callbacks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import warnings
from typing import TYPE_CHECKING, Any, Dict, List, Optional
from typing import TYPE_CHECKING, Any

from omegaconf import DictConfig, OmegaConf

Expand All @@ -21,16 +21,16 @@ class CallbacksCache(metaclass=Singleton):
def instance() -> "CallbacksCache":
return Singleton.instance(CallbacksCache) # type: ignore

cache: Dict[int, "Callbacks"]
cache: dict[int, "Callbacks"]

def __init__(self) -> None:
self.cache = {}


class Callbacks:
callbacks: List[Any]
callbacks: list[Any]

def __init__(self, config: Optional[DictConfig] = None, check_cache: bool = True) -> None:
def __init__(self, config: DictConfig | None = None, check_cache: bool = True) -> None:
if config is None:
return
cache = CallbacksCache.instance().cache
Expand All @@ -53,7 +53,7 @@ def _notify(self, function_name: str, reverse: bool = False, **kwargs: Any) -> N
for c in callbacks:
try:
getattr(c, function_name)(**kwargs)
except Exception as e:
except Exception as e: # noqa: BLE001
warnings.warn(f"Callback {type(c).__name__}.{function_name} raised {type(e).__name__}: {e}")

def on_run_start(self, config: DictConfig, **kwargs: Any) -> None:
Expand Down Expand Up @@ -88,8 +88,8 @@ def on_job_end(self, config: DictConfig, job_return: "JobReturn", **kwargs: Any)
def on_compose_config(
self,
config: DictConfig,
config_name: Optional[str],
overrides: List[str],
config_name: str | None,
overrides: list[str],
) -> None:
self._notify(
function_name="on_compose_config",
Expand Down
74 changes: 37 additions & 37 deletions lerna/_internal/config_loader_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import re
import sys
import warnings
from collections.abc import MutableSequence
from textwrap import dedent
from typing import Any, List, MutableSequence, Optional, Tuple, Union
from typing import Any

from omegaconf import Container, DictConfig, ListConfig, OmegaConf, flag_override, open_dict
from omegaconf.errors import (
Expand Down Expand Up @@ -60,7 +61,7 @@ def __init__(

@staticmethod
def validate_sweep_overrides_legal(
overrides: List[Override],
overrides: list[Override],
run_mode: RunMode,
from_shell: bool,
) -> None:
Expand Down Expand Up @@ -90,12 +91,12 @@ def validate_sweep_overrides_legal(
else:
assert False

def _missing_config_error(self, config_name: Optional[str], msg: str, with_search_path: bool) -> None:
def _missing_config_error(self, config_name: str | None, msg: str, with_search_path: bool) -> None:
def add_search_path() -> str:
descs = []
for src in self.repository.get_sources():
if src.provider != "schema":
descs.append(f"\t{repr(src)}")
descs.append(f"\t{src!r}")
lines = "\n".join(descs)

if with_search_path:
Expand All @@ -108,22 +109,21 @@ def add_search_path() -> str:
def ensure_main_config_source_available(self) -> None:
for source in self.get_sources():
# if specified, make sure main config search path exists
if source.provider == "main":
if not source.available():
if source.scheme() == "pkg":
if source.path == "":
msg = "Primary config module is empty.\nPython requires resources to be in a module with an __init__.py file"
else:
msg = f"Primary config module '{source.path}' not found.\nCheck that it's correct and contains an __init__.py file"
if source.provider == "main" and not source.available():
if source.scheme() == "pkg":
if source.path == "":
msg = "Primary config module is empty.\nPython requires resources to be in a module with an __init__.py file"
else:
msg = f"Primary config directory not found.\nCheck that the config directory '{source.path}' exists and readable"
msg = f"Primary config module '{source.path}' not found.\nCheck that it's correct and contains an __init__.py file"
else:
msg = f"Primary config directory not found.\nCheck that the config directory '{source.path}' exists and readable"

self._missing_config_error(config_name=None, msg=msg, with_search_path=False)
self._missing_config_error(config_name=None, msg=msg, with_search_path=False)

def load_configuration(
self,
config_name: Optional[str],
overrides: List[str],
config_name: str | None,
overrides: list[str],
run_mode: RunMode,
from_shell: bool = True,
validate_sweep_overrides: bool = True,
Expand All @@ -141,8 +141,8 @@ def load_configuration(

def _process_config_searchpath(
self,
config_name: Optional[str],
parsed_overrides: List[Override],
config_name: str | None,
parsed_overrides: list[Override],
repo: CachingConfigRepository,
) -> None:
if config_name is not None:
Expand Down Expand Up @@ -204,8 +204,8 @@ def _err() -> None:
)

def _parse_overrides_and_create_caching_repo(
self, config_name: Optional[str], overrides: List[str]
) -> Tuple[List[Override], CachingConfigRepository]:
self, config_name: str | None, overrides: list[str]
) -> tuple[list[Override], CachingConfigRepository]:
parser = OverridesParser.create()
parsed_overrides = parser.parse_overrides(overrides=overrides)
caching_repo = CachingConfigRepository(self.repository)
Expand All @@ -214,8 +214,8 @@ def _parse_overrides_and_create_caching_repo(

def _load_configuration_impl(
self,
config_name: Optional[str],
overrides: List[str],
config_name: str | None,
overrides: list[str],
run_mode: RunMode,
from_shell: bool = True,
validate_sweep_overrides: bool = True,
Expand Down Expand Up @@ -289,7 +289,7 @@ def _load_configuration_impl(

return cfg

def load_sweep_config(self, master_config: DictConfig, sweep_overrides: List[str]) -> DictConfig:
def load_sweep_config(self, master_config: DictConfig, sweep_overrides: list[str]) -> DictConfig:
# Recreate the config for this sweep instance with the appropriate overrides
overrides = OmegaConf.to_container(master_config.hydra.overrides.hydra)
assert isinstance(overrides, list)
Expand All @@ -311,7 +311,7 @@ def get_search_path(self) -> ConfigSearchPath:
return self.config_search_path

@staticmethod
def _apply_overrides_to_config(overrides: List[Override], cfg: DictConfig) -> None:
def _apply_overrides_to_config(overrides: list[Override], cfg: DictConfig) -> None:
for override in overrides:
if override.package is not None:
raise ConfigCompositionException(
Expand All @@ -336,7 +336,7 @@ def _apply_overrides_to_config(overrides: List[Override], cfg: DictConfig) -> No
del cfg[key]
else:
node = OmegaConf.select(cfg, key[0:last_dot])
node_key: Union[str, int] = key[last_dot + 1 :]
node_key: str | int = key[last_dot + 1 :]
if isinstance(node, ListConfig):
node_key = int(node_key)
del node[node_key]
Expand Down Expand Up @@ -498,7 +498,7 @@ def _load_single_config(self, default: ResultDefault, repo: IConfigRepository) -
return res

@staticmethod
def _embed_result_config(ret: ConfigResult, package_override: Optional[str]) -> ConfigResult:
def _embed_result_config(ret: ConfigResult, package_override: str | None) -> ConfigResult:
package = ret.header["package"]
if package_override is not None:
package = package_override
Expand All @@ -511,26 +511,26 @@ def _embed_result_config(ret: ConfigResult, package_override: Optional[str]) ->

return ret

def list_groups(self, parent_name: str) -> List[str]:
def list_groups(self, parent_name: str) -> list[str]:
return self.get_group_options(group_name=parent_name, results_filter=ObjectType.GROUP)

def get_group_options(
self,
group_name: str,
results_filter: Optional[ObjectType] = ObjectType.CONFIG,
config_name: Optional[str] = None,
overrides: Optional[List[str]] = None,
) -> List[str]:
results_filter: ObjectType | None = ObjectType.CONFIG,
config_name: str | None = None,
overrides: list[str] | None = None,
) -> list[str]:
if overrides is None:
overrides = []
_, caching_repo = self._parse_overrides_and_create_caching_repo(config_name, overrides)
return caching_repo.get_group_options(group_name, results_filter)

def _try_rust_compose(
self,
defaults: List[ResultDefault],
defaults: list[ResultDefault],
repo: IConfigRepository,
) -> Optional[DictConfig]:
) -> DictConfig | None:
"""
Compose config using Rust for performance.

Expand Down Expand Up @@ -680,7 +680,7 @@ def _strip_defaults(cfg: Any) -> None:

def _compose_config_from_defaults_list(
self,
defaults: List[ResultDefault],
defaults: list[ResultDefault],
repo: IConfigRepository,
) -> DictConfig:
# Try Rust-accelerated compose first
Expand All @@ -705,13 +705,13 @@ def _compose_config_from_defaults_list(

return cfg

def get_sources(self) -> List[ConfigSource]:
def get_sources(self) -> list[ConfigSource]:
return self.repository.get_sources()

def compute_defaults_list(
self,
config_name: Optional[str],
overrides: List[str],
config_name: str | None,
overrides: list[str],
run_mode: RunMode,
) -> DefaultsList:
parsed_overrides, caching_repo = self._parse_overrides_and_create_caching_repo(config_name, overrides)
Expand All @@ -725,7 +725,7 @@ def compute_defaults_list(
return defaults_list


def get_overrides_dirname(overrides: List[Override], exclude_keys: List[str], item_sep: str, kv_sep: str) -> str:
def get_overrides_dirname(overrides: list[Override], exclude_keys: list[str], item_sep: str, kv_sep: str) -> str:
lines = []
for override in overrides:
if override.key_or_group not in exclude_keys:
Expand Down
Loading
Loading