Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unpin mypy #1878

Merged
merged 4 commits into from
Feb 14, 2022
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
4 changes: 4 additions & 0 deletions .mypy.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
[mypy]
python_version = 3.6
mypy_path=.stubs
exclude = (?x)(
build/
| ^hydra/grammar/gen/
)

[mypy-antlr4.*]
ignore_missing_imports = True
Expand Down
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
7 changes: 5 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,10 @@ 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
22 changes: 9 additions & 13 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 @@ -92,9 +90,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
):
for file in resources.files(self.path).joinpath(config_path).iterdir():
fname = file.name
fpath = os.path.join(config_path, fname)
self._list_add_result(
Expand Down
7 changes: 5 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,10 @@ 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
46 changes: 41 additions & 5 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,23 +233,55 @@ def lint(session):

session.run(*isort, silent=SILENT)

session.run("mypy", ".", "--strict", silent=SILENT)
session.run(
"mypy",
".",
"--strict",
"--install-types",
"--non-interactive",
"--exclude=^examples/",
"--exclude=^tests/standalone_apps/",
"--exclude=^tests/test_apps/",
"--exclude=^tools/",
"--exclude=^plugins/",
silent=SILENT,
)
session.run("flake8", "--config", ".flake8")
session.run("yamllint", "--strict", ".")

example_dirs = [
mypy_check_subdirs = [
"examples/advanced",
"examples/configure_hydra",
"examples/patterns",
"examples/instantiate",
"examples/tutorials/basic/your_first_hydra_app",
"examples/tutorials/basic/running_your_hydra_app",
"examples/tutorials/structured_configs",
"tests/standalone_apps",
"tests/test_apps",
]
for edir in example_dirs:
dirs = find_dirs(path=edir)
for sdir in mypy_check_subdirs:
dirs = find_dirs(path=sdir)
for d in dirs:
session.run("mypy", d, "--strict", silent=SILENT)
session.run(
"mypy",
d,
"--strict",
"--install-types",
"--non-interactive",
silent=SILENT,
)

for sdir in ["tools"]: # no --strict flag for tools
dirs = find_dirs(path=sdir)
for d in dirs:
session.run(
"mypy",
d,
"--install-types",
"--non-interactive",
silent=SILENT,
)

# lint example plugins
lint_plugins_in_dir(session=session, directory="examples/plugins")
Expand Down Expand Up @@ -295,6 +327,8 @@ def lint_plugins_in_dir(session, directory: str) -> None:
session.run(
"mypy",
"--strict",
"--install-types",
"--non-interactive",
f"{path}/hydra_plugins",
"--config-file",
f"{BASE}/.mypy.ini",
Expand All @@ -303,6 +337,8 @@ def lint_plugins_in_dir(session, directory: str) -> None:
session.run(
"mypy",
"--strict",
"--install-types",
"--non-interactive",
"--namespace-packages",
"--config-file",
f"{BASE}/.mypy.ini",
Expand Down
2 changes: 1 addition & 1 deletion requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ coverage
flake8
flake8-copyright
isort==5.5.2
mypy==0.790
mypy
nox
packaging
pre-commit
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
17 changes: 10 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,28 @@


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
Loading