Skip to content

Commit

Permalink
fixup source files
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasha10 committed Feb 14, 2022
1 parent 27bd6fc commit 2169c57
Show file tree
Hide file tree
Showing 37 changed files with 67 additions and 62 deletions.
12 changes: 6 additions & 6 deletions build_helpers/build_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from typing import List, Optional

from setuptools import Command
from setuptools.command import build_py, develop, sdist # type: ignore
from setuptools.command import build_py, develop, sdist


def find_version(*file_paths: str) -> str:
Expand Down Expand Up @@ -143,24 +143,24 @@ def run_antlr(cmd: Command) -> None:
raise


class BuildPyCommand(build_py.build_py): # type: ignore
class BuildPyCommand(build_py.build_py):
def run(self) -> None:
if not self.dry_run:
self.run_command("clean")
run_antlr(self)
build_py.build_py.run(self)


class Develop(develop.develop): # type: ignore
def run(self) -> None:
class Develop(develop.develop):
def run(self) -> None: # type: ignore
if not self.dry_run:
run_antlr(self)
develop.develop.run(self)


class SDistCommand(sdist.sdist): # type: ignore
class SDistCommand(sdist.sdist):
def run(self) -> None:
if not self.dry_run:
if not self.dry_run: # type: ignore
self.run_command("clean")
run_antlr(self)
sdist.sdist.run(self)
Expand Down
4 changes: 2 additions & 2 deletions hydra/_internal/config_search_path_impl.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
from typing import List, MutableSequence, Optional
from typing import List, MutableSequence, Optional, Union

from hydra.core.config_search_path import (
ConfigSearchPath,
Expand Down Expand Up @@ -63,7 +63,7 @@ def append(
self.append(provider, path, anchor=None)

def prepend(
self, provider: str, path: str, anchor: Optional[SearchPathQuery] = None
self, provider: str, path: str, anchor: Optional[Union[SearchPathQuery, str]] = None
) -> None:
"""
Prepends to the search path.
Expand Down
20 changes: 9 additions & 11 deletions hydra/_internal/core_plugins/importlib_resources_config_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@
import os
import sys
import zipfile
from typing import Any, List, Optional
from typing import TYPE_CHECKING, Any, List, Optional

from omegaconf import OmegaConf

from hydra.core.object_type import ObjectType
from hydra.plugins.config_source import ConfigLoadError, ConfigResult, ConfigSource

if sys.version_info.major >= 4 or (
sys.version_info.major >= 3 and sys.version_info.minor >= 9
):
from importlib import resources
if TYPE_CHECKING or (sys.version_info < (3, 9)):
import importlib_resources as resources
else:
import importlib_resources as resources # type:ignore
from importlib import resources

# Relevant issue: https://github.com/python/mypy/issues/1153
# Use importlib backport for Python older than 3.9
Expand Down Expand Up @@ -55,22 +53,22 @@ def _read_config(self, res: Any) -> ConfigResult:

def load_config(self, config_path: str) -> ConfigResult:
normalized_config_path = self._normalize_file_name(config_path)
res = resources.files(self.path).joinpath(normalized_config_path) # type:ignore
res = resources.files(self.path).joinpath(normalized_config_path)
if not res.exists():
raise ConfigLoadError(f"Config not found : {normalized_config_path}")

return self._read_config(res)

def available(self) -> bool:
try:
files = resources.files(self.path) # type: ignore
files = resources.files(self.path)
except (ValueError, ModuleNotFoundError, TypeError):
return False
return any(f.name == "__init__.py" and f.is_file() for f in files.iterdir())

def is_group(self, config_path: str) -> bool:
try:
files = resources.files(self.path) # type:ignore
files = resources.files(self.path)
except (ValueError, ModuleNotFoundError, TypeError):
return False

Expand All @@ -82,7 +80,7 @@ def is_group(self, config_path: str) -> bool:
def is_config(self, config_path: str) -> bool:
config_path = self._normalize_file_name(config_path)
try:
files = resources.files(self.path) # type:ignore
files = resources.files(self.path)
except (ValueError, ModuleNotFoundError, TypeError):
return False
res = files.joinpath(config_path)
Expand All @@ -93,7 +91,7 @@ def is_config(self, config_path: str) -> bool:
def list(self, config_path: str, results_filter: Optional[ObjectType]) -> List[str]:
files: List[str] = []
for file in (
resources.files(self.path).joinpath(config_path).iterdir() # type:ignore
resources.files(self.path).joinpath(config_path).iterdir()
):
fname = file.name
fpath = os.path.join(config_path, fname)
Expand Down
4 changes: 2 additions & 2 deletions hydra/core/config_search_path.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import MutableSequence, Optional
from typing import MutableSequence, Optional, Union


class SearchPathElement:
Expand Down Expand Up @@ -49,7 +49,7 @@ def append(

@abstractmethod
def prepend(
self, provider: str, path: str, anchor: Optional[SearchPathQuery] = None
self, provider: str, path: str, anchor: Optional[Union[SearchPathQuery, str]] = None
) -> None:
"""
Prepends to the search path.
Expand Down
3 changes: 2 additions & 1 deletion hydra/core/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ def _scan_all_plugins(
if module_name.startswith("_") and not module_name.startswith("__"):
continue
import_time = timer()
m = importer.find_module(modname)
m = importer.find_module(modname) # type: ignore
assert m is not None
with warnings.catch_warnings(record=True) as recorded_warnings:
loaded_mod = m.load_module(modname)
import_time = timer() - import_time
Expand Down
16 changes: 5 additions & 11 deletions hydra/extra/pytest_plugin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import copy
from pathlib import Path
from typing import Callable, List, Optional
from typing import Callable, Generator, List, Optional

from pytest import fixture

Expand All @@ -10,8 +10,8 @@
from hydra.types import TaskFunction


@fixture(scope="function") # type: ignore
def hydra_restore_singletons() -> None:
@fixture(scope="function")
def hydra_restore_singletons() -> Generator[None, None, None]:
"""
Restore singletons state after the function returns
"""
Expand All @@ -20,7 +20,7 @@ def hydra_restore_singletons() -> None:
Singleton.set_state(state)


@fixture(scope="function") # type: ignore
@fixture(scope="function")
def hydra_sweep_runner() -> Callable[
[
Optional[str],
Expand All @@ -29,7 +29,6 @@ def hydra_sweep_runner() -> Callable[
Optional[str],
Optional[str],
Optional[List[str]],
Optional[bool],
Optional[Path],
bool,
],
Expand All @@ -42,7 +41,6 @@ def _(
config_path: Optional[str],
config_name: Optional[str],
overrides: Optional[List[str]],
strict: Optional[bool] = None,
temp_dir: Optional[Path] = None,
configure_logging: bool = False,
) -> SweepTaskFunction:
Expand All @@ -52,7 +50,6 @@ def _(
sweep.task_function = task_function
sweep.config_path = config_path
sweep.config_name = config_name
sweep.strict = strict
sweep.overrides = overrides or []
sweep.temp_dir = str(temp_dir)
sweep.configure_logging = configure_logging
Expand All @@ -61,15 +58,14 @@ def _(
return _


@fixture(scope="function") # type: ignore
@fixture(scope="function")
def hydra_task_runner() -> Callable[
[
Optional[str],
Optional[str],
Optional[str],
Optional[str],
Optional[List[str]],
Optional[bool],
bool,
],
TaskTestFunction,
Expand All @@ -80,7 +76,6 @@ def _(
config_path: Optional[str],
config_name: Optional[str],
overrides: Optional[List[str]] = None,
strict: Optional[bool] = None,
configure_logging: bool = False,
) -> TaskTestFunction:
task = TaskTestFunction()
Expand All @@ -89,7 +84,6 @@ def _(
task.config_name = config_name
task.calling_module = calling_module
task.config_path = config_path
task.strict = strict
task.configure_logging = configure_logging
return task

Expand Down
2 changes: 1 addition & 1 deletion tests/test_apps/app_change_dir/my_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


@hydra.main(config_path=None)
def main(_: DictConfig):
def main(_: DictConfig) -> None:
subdir = Path(HydraConfig.get().run.dir) / Path("subdir")
subdir.mkdir(exist_ok=True, parents=True)
os.chdir(subdir)
Expand Down
15 changes: 8 additions & 7 deletions tests/test_apps/app_with_callbacks/custom_callback/my_app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved

import logging
from typing import Any

from omegaconf import DictConfig, OmegaConf

Expand All @@ -12,26 +13,26 @@


class CustomCallback(Callback):
def __init__(self, callback_name):
def __init__(self, callback_name: str) -> None:
self.name = callback_name
log.info(f"Init {self.name}")

def on_job_start(self, config: DictConfig, **kwargs) -> None:
def on_job_start(self, config: DictConfig, **kwargs: Any) -> None:
log.info(f"{self.name} on_job_start")

def on_job_end(self, config: DictConfig, job_return: JobReturn, **kwargs) -> None:
def on_job_end(self, config: DictConfig, job_return: JobReturn, **kwargs: Any) -> None:
log.info(f"{self.name} on_job_end")

def on_run_start(self, config: DictConfig, **kwargs) -> None:
def on_run_start(self, config: DictConfig, **kwargs: Any) -> None:
log.info(f"{self.name} on_run_start")

def on_run_end(self, config: DictConfig, **kwargs) -> None:
def on_run_end(self, config: DictConfig, **kwargs: Any) -> None:
log.info(f"{self.name} on_run_end")

def on_multirun_start(self, config: DictConfig, **kwargs) -> None:
def on_multirun_start(self, config: DictConfig, **kwargs: Any) -> None:
log.info(f"{self.name} on_multirun_start")

def on_multirun_end(self, config: DictConfig, **kwargs) -> None:
def on_multirun_end(self, config: DictConfig, **kwargs: Any) -> None:
log.info(f"{self.name} on_multirun_end")


Expand Down
2 changes: 1 addition & 1 deletion tests/test_apps/multirun_structured_conflict/my_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class TestConfig:


@hydra.main(config_path=".", config_name="config")
def run(config: DictConfig):
def run(config: DictConfig) -> None:
print(config.test.param)


Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 3 additions & 1 deletion tests/test_apps/structured_with_none_list/my_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from dataclasses import dataclass
from typing import List, Optional

from omegaconf import DictConfig

import hydra
from hydra.core.config_store import ConfigStore

Expand All @@ -16,7 +18,7 @@ class Config:


@hydra.main(config_path=None, config_name="config")
def main(cfg):
def main(cfg: DictConfig) -> None:
print(cfg)


Expand Down
10 changes: 5 additions & 5 deletions tests/test_hydra.py
Original file line number Diff line number Diff line change
Expand Up @@ -1031,14 +1031,14 @@ def test_hydra_output_dir(
"directory,file,module, error",
[
(
"tests/test_apps/run_as_module/1",
"tests/test_apps/run_as_module_1",
"my_app.py",
"my_app",
"Primary config module is empty",
),
("tests/test_apps/run_as_module/2", "my_app.py", "my_app", None),
("tests/test_apps/run_as_module/3", "module/my_app.py", "module.my_app", None),
("tests/test_apps/run_as_module/4", "module/my_app.py", "module.my_app", None),
("tests/test_apps/run_as_module_2", "my_app.py", "my_app", None),
("tests/test_apps/run_as_module_3", "module/my_app.py", "module.my_app", None),
("tests/test_apps/run_as_module_4", "module/my_app.py", "module.my_app", None),
],
)
def test_module_run(
Expand Down Expand Up @@ -1285,7 +1285,7 @@ def test_config_dir_argument(


def test_schema_overrides_hydra(monkeypatch: Any, tmpdir: Path) -> None:
monkeypatch.chdir("tests/test_apps/schema-overrides-hydra")
monkeypatch.chdir("tests/test_apps/schema_overrides_hydra")
cmd = [
"my_app.py",
"hydra.run.dir=" + str(tmpdir),
Expand Down
7 changes: 4 additions & 3 deletions tools/configen/configen/configen.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def init_config(conf_dir: str) -> None:
sys.exit(1)

sample_config = pkgutil.get_data(__name__, "templates/sample_config.yaml")
assert sample_config is not None
file.write_bytes(sample_config)


Expand Down Expand Up @@ -147,7 +148,7 @@ def get_default_flags(module: ModuleConf) -> List[Parameter]:
Parameter(
name="_recursive_",
type_str="bool",
default=module.default_flags._recursive_,
default=str(module.default_flags._recursive_),
)
)

Expand All @@ -156,7 +157,7 @@ def get_default_flags(module: ModuleConf) -> List[Parameter]:

def generate_module(cfg: ConfigenConf, module: ModuleConf) -> str:
classes_map: Dict[str, ClassInfo] = {}
imports = set()
imports: Set[Any] = set()
string_imports: Set[str] = set()

default_flags = get_default_flags(module)
Expand All @@ -165,7 +166,7 @@ def generate_module(cfg: ConfigenConf, module: ModuleConf) -> str:
full_name = f"{module.name}.{class_name}"
cls = hydra.utils.get_class(full_name)
sig = inspect.signature(cls)
resolved_hints = get_type_hints(cls.__init__)
resolved_hints = get_type_hints(cls.__init__) # type: ignore
params: List[Parameter] = []
params = params + default_flags

Expand Down
Loading

0 comments on commit 2169c57

Please sign in to comment.