diff --git a/dvc/cache/base.py b/dvc/cache/base.py index 4454dff51..a5f6b0b13 100644 --- a/dvc/cache/base.py +++ b/dvc/cache/base.py @@ -3,6 +3,7 @@ import json import logging from concurrent.futures import ThreadPoolExecutor from copy import copy +from typing import Optional from funcy import decorator from shortuuid import uuid @@ -16,7 +17,9 @@ from dvc.exceptions import ( DvcException, ) from dvc.progress import Tqdm -from dvc.remote.slow_link_detection import slow_link_guard +from dvc.remote.slow_link_detection import ( # type: ignore[attr-defined] + slow_link_guard, +) from ..tree.base import RemoteActionNotImplemented @@ -41,7 +44,7 @@ class CloudCache: """Cloud cache class.""" DEFAULT_CACHE_TYPES = ["copy"] - CACHE_MODE = None + CACHE_MODE: Optional[int] = None def __init__(self, tree): self.tree = tree diff --git a/dvc/external_repo.py b/dvc/external_repo.py index c3cb563a7..7debd028c 100644 --- a/dvc/external_repo.py +++ b/dvc/external_repo.py @@ -4,7 +4,7 @@ import tempfile import threading from contextlib import contextmanager from distutils.dir_util import copy_tree -from typing import Iterable +from typing import Dict, Iterable from funcy import cached_property, reraise, retry, wrap_with @@ -73,8 +73,8 @@ def external_repo(url, rev=None, for_write=False, **kwargs): _remove(path) -CLONES = {} -CACHE_DIRS = {} +CLONES: Dict[str, str] = {} +CACHE_DIRS: Dict[str, str] = {} def clean_repos(): diff --git a/dvc/hash_info.py b/dvc/hash_info.py index a4bcbd1fe..cfba288ca 100644 --- a/dvc/hash_info.py +++ b/dvc/hash_info.py @@ -1,19 +1,22 @@ from collections import OrderedDict from dataclasses import dataclass, field +from typing import Dict, Optional HASH_DIR_SUFFIX = ".dir" +DirInfo = Dict[str, str] + @dataclass class HashInfo: PARAM_SIZE = "size" PARAM_NFILES = "nfiles" - name: str - value: str - dir_info: dict = field(default=None, compare=False) - size: int = field(default=None, compare=False) - nfiles: int = field(default=None, compare=False) + name: Optional[str] + value: Optional[str] + dir_info: Optional[DirInfo] = field(default=None, compare=False) + size: Optional[int] = field(default=None, compare=False) + nfiles: Optional[int] = field(default=None, compare=False) def __bool__(self): return bool(self.value) diff --git a/dvc/path_info.py b/dvc/path_info.py index 3f35eba90..6f6aa57d8 100644 --- a/dvc/path_info.py +++ b/dvc/path_info.py @@ -2,6 +2,7 @@ import os import pathlib import posixpath +from typing import Callable from urllib.parse import urlparse from funcy import cached_property @@ -253,7 +254,7 @@ class CloudURLInfo(URLInfo): class HTTPURLInfo(URLInfo): - __hash__ = URLInfo.__hash__ + __hash__: Callable[["HTTPURLInfo"], int] = URLInfo.__hash__ def __init__(self, url): p = urlparse(url) diff --git a/dvc/scm/base.py b/dvc/scm/base.py index 699c602e6..d37abbcd9 100644 --- a/dvc/scm/base.py +++ b/dvc/scm/base.py @@ -41,7 +41,7 @@ class Base: self._root_dir = os.path.realpath(root_dir) @property - def root_dir(self): + def root_dir(self) -> str: return self._root_dir def __repr__(self): diff --git a/dvc/scm/git.py b/dvc/scm/git.py index 5bfd84c77..aa93d715b 100644 --- a/dvc/scm/git.py +++ b/dvc/scm/git.py @@ -90,7 +90,7 @@ class Git(Base): self.files_to_track = set() @property - def root_dir(self): + def root_dir(self) -> str: return self.repo.working_tree_dir @staticmethod diff --git a/dvc/stage/__init__.py b/dvc/stage/__init__.py index ed040c4cd..839ba69f2 100644 --- a/dvc/stage/__init__.py +++ b/dvc/stage/__init__.py @@ -133,11 +133,11 @@ class Stage(params.StageParams): self.desc = desc @property - def path(self): + def path(self) -> str: return self._path @path.setter - def path(self, path): + def path(self, path: str): self._path = path self.__dict__.pop("path_in_repo", None) self.__dict__.pop("relpath", None) @@ -169,7 +169,7 @@ class Stage(params.StageParams): return f"stage: '{self.addressing}'" @property - def addressing(self): + def addressing(self) -> str: """ Useful for alternative presentations where we don't need `Stage:` prefix. diff --git a/dvc/stage/serialize.py b/dvc/stage/serialize.py index 1c6ffe8e0..1a1e179f5 100644 --- a/dvc/stage/serialize.py +++ b/dvc/stage/serialize.py @@ -1,7 +1,7 @@ from collections import OrderedDict from functools import partial from operator import attrgetter -from typing import TYPE_CHECKING, List +from typing import TYPE_CHECKING, List, no_type_check from funcy import post_processing @@ -57,6 +57,7 @@ def _serialize_out(out): return out.def_path if not flags else {out.def_path: flags} +@no_type_check def _serialize_outs(outputs: List[BaseOutput]): outs, metrics, plots = [], [], [] for out in sort_by_path(outputs): @@ -95,6 +96,7 @@ def _serialize_params_keys(params): return keys +@no_type_check def _serialize_params_values(params: List[ParamsDependency]): """Returns output of following format, used for lockfile: {'params.yaml': {'lr': '1', 'train': 2}, {'params2.yaml': {'lr': '1'}}