Skip to content

Commit

Permalink
Lint passing
Browse files Browse the repository at this point in the history
  • Loading branch information
omry committed Dec 1, 2020
1 parent 50dd795 commit 8a24c63
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 148 deletions.
125 changes: 64 additions & 61 deletions hydra/_internal/hydra.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
from hydra.types import RunMode, TaskFunction

from .config_loader_impl import ConfigLoaderImpl
from .defaults_list import expand_defaults_list
from .utils import create_automatic_config_search_path

log: Optional[logging.Logger] = None
Expand Down Expand Up @@ -487,66 +486,70 @@ def _print_plugins_profiling_info(self, top_n: int) -> None:

self._log_footer(header=header, filler="-")

def _print_defaults_list(self, config_name: Optional[str], overrides: List[str]):
run_mode = RunMode.RUN
ret = self.config_loader.compute_input_defaults_list(
config_name=config_name,
overrides=overrides,
run_mode=RunMode.RUN,
from_shell=True,
)

defaults = expand_defaults_list(
defaults=ret.input_defaults,
skip_missing=run_mode == RunMode.RUN,
ignore_config_load_failures=True,
repo=self.config_loader.repository, # type: ignore
)

box: List[List[str]] = [
[
"Config group",
"Config name",
"Package",
"Parent",
"Skip reason",
]
]
for d in defaults:
row = [
d.config_group,
d.config_name,
d.package,
d.parent,
d.skip_load_reason if d.skip_load else "",
]
row = [x if x is not None else "" for x in row]
box.append(row)
padding = get_column_widths(box)
del box[0]
log.debug("")
self._log_header("Defaults List", filler="*")
header = "| {} | {} | {} | {} | {} | ".format(
"Config group".ljust(padding[0]),
"Config name".ljust(padding[1]),
"Package".ljust(padding[2]),
"Parent".ljust(padding[3]),
"Skip reason".ljust(padding[4]),
)
self._log_header(header=header, filler="-")

for row in box:
log.debug(
"| {} | {} | {} | {} | {} |".format(
row[0].ljust(padding[0]),
row[1].ljust(padding[1]),
row[2].ljust(padding[2]),
row[3].ljust(padding[3]),
row[4].ljust(padding[4]),
)
)

self._log_footer(header=header, filler="-")
def _print_defaults_list(
self, config_name: Optional[str], overrides: List[str]
) -> None:
# TODO
assert False, "Not implemented"
# run_mode = RunMode.RUN
# ret = self.config_loader.compute_input_defaults_list(
# config_name=config_name,
# overrides=overrides,
# run_mode=RunMode.RUN,
# from_shell=True,
# )
#
# defaults = expand_defaults_list(
# defaults=ret.input_defaults,
# skip_missing=run_mode == RunMode.RUN,
# ignore_config_load_failures=True,
# repo=self.config_loader.repository, # type: ignore
# )
#
# box: List[List[str]] = [
# [
# "Config group",
# "Config name",
# "Package",
# "Parent",
# "Skip reason",
# ]
# ]
# for d in defaults:
# row = [
# d.config_group,
# d.config_name,
# d.package,
# d.parent,
# d.skip_load_reason if d.skip_load else "",
# ]
# row = [x if x is not None else "" for x in row]
# box.append(row)
# padding = get_column_widths(box)
# del box[0]
# log.debug("")
# self._log_header("Defaults List", filler="*")
# header = "| {} | {} | {} | {} | {} | ".format(
# "Config group".ljust(padding[0]),
# "Config name".ljust(padding[1]),
# "Package".ljust(padding[2]),
# "Parent".ljust(padding[3]),
# "Skip reason".ljust(padding[4]),
# )
# self._log_header(header=header, filler="-")
#
# for row in box:
# log.debug(
# "| {} | {} | {} | {} | {} |".format(
# row[0].ljust(padding[0]),
# row[1].ljust(padding[1]),
# row[2].ljust(padding[2]),
# row[3].ljust(padding[3]),
# row[4].ljust(padding[4]),
# )
# )
#
# self._log_footer(header=header, filler="-")

def _print_debug_info(self, cfg: DictConfig) -> None:
assert log is not None
Expand Down
35 changes: 22 additions & 13 deletions hydra/_internal/new_defaults_list.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved

import copy
import warnings
from dataclasses import dataclass
from textwrap import dedent
from typing import Dict, List
from typing import Dict, List, Optional, Union

from hydra import MissingConfigException
from hydra._internal.config_repository import IConfigRepository
Expand Down Expand Up @@ -77,7 +79,7 @@ def override_default_option(self, default: GroupDefault) -> None:
default.config_name_overridden = True
self.override_used[key] = True

def ensure_overrides_used(self):
def ensure_overrides_used(self) -> None:
for key, used in self.override_used.items():
if not used:
msg = dedent(
Expand Down Expand Up @@ -110,8 +112,10 @@ def _validate_self(containing_node: InputDefault, defaults: List[InputDefault])


def update_package_header(
repo: IConfigRepository, node: InputDefault, is_primary_config: bool
):
repo: IConfigRepository,
node: InputDefault,
is_primary_config: bool,
) -> None:
# This loads the same config loaded in _create_defaults_tree
# To avoid loading it twice, the repo implementation is expected to cache
# loaded configs
Expand All @@ -127,14 +131,14 @@ def _expand_virtual_root(
root: DefaultsTreeNode,
overrides: Overrides,
) -> DefaultsTreeNode:
children = []
children: List[Union[DefaultsTreeNode, InputDefault]] = []
assert root.children is not None
for d in reversed(root.children):
assert isinstance(d, ConfigDefault)
new_root = DefaultsTreeNode(node=d, parent=root)
d.parent_base_dir = ""
d.parent_package = ""

new_root.parent_base_dir = d.get_group_path()
subtree = _create_defaults_tree(
repo=repo,
root=new_root,
Expand All @@ -159,7 +163,7 @@ def _create_defaults_tree(
overrides: Overrides,
) -> DefaultsTreeNode:
parent = root.node
children = []
children: List[Union[InputDefault, DefaultsTreeNode]] = []
if parent.is_virtual():
return _expand_virtual_root(repo, root, overrides)
else:
Expand All @@ -172,6 +176,7 @@ def _create_defaults_tree(
)

if overrides.is_overridden(parent):
assert isinstance(parent, GroupDefault)
overrides.override_default_option(parent)
# clear package header and obtain updated one from overridden config
# (for the rare case it has changed)
Expand All @@ -187,11 +192,12 @@ def _create_defaults_tree(
if loaded is None:
missing_config_error(repo, root.node)

assert loaded is not None
defaults_list = copy.deepcopy(loaded.new_defaults_list)

if is_primary_config:
for d in overrides.append_group_defaults:
defaults_list.append(d)
for gd in overrides.append_group_defaults:
defaults_list.append(gd)

if len(defaults_list) > 0:
_validate_self(containing_node=parent, defaults=defaults_list)
Expand All @@ -203,6 +209,7 @@ def _create_defaults_tree(
d.parent_package = parent.get_final_package()

if isinstance(d, GroupDefault):
assert d.group is not None
is_legacy_hydra_override = not d.override and d.group.startswith(
"hydra/"
)
Expand Down Expand Up @@ -231,7 +238,6 @@ def _create_defaults_tree(
d.parent_base_dir = parent.get_group_path()
d.parent_package = parent.get_final_package()

new_root.parent_base_dir = d.get_group_path()
subtree = _create_defaults_tree(
repo=repo,
root=new_root,
Expand All @@ -249,9 +255,12 @@ def _create_defaults_tree(
return root


def _create_result_default(tree: DefaultsTreeNode, node: InputDefault) -> ResultDefault:
def _create_result_default(
tree: Optional[DefaultsTreeNode], node: InputDefault
) -> ResultDefault:
res = ResultDefault()
if node.is_self():
assert tree is not None
res.config_path = tree.node.get_config_path()
res.is_self = True
pn = tree.parent_node()
Expand All @@ -272,7 +281,7 @@ def _create_result_default(tree: DefaultsTreeNode, node: InputDefault) -> Result
def _tree_to_list(
tree: DefaultsTreeNode,
output: List[ResultDefault],
):
) -> None:
node = tree.node

if tree.children is None or len(tree.children) == 0:
Expand Down Expand Up @@ -318,7 +327,7 @@ def _create_defaults_list(
is_primary_config=True,
)

output = []
output: List[ResultDefault] = []
_tree_to_list(tree=defaults_tree, output=output)
# TODO: fail if duplicate items exists
return output
Expand Down
Loading

0 comments on commit 8a24c63

Please sign in to comment.