Make some imports lazy for plots (#5219)

This commit is contained in:
Saugat Pachhai
2021-01-06 17:41:30 +02:00
committed by GitHub
parent 1cc646614f
commit 2646c0d6bc
3 changed files with 26 additions and 18 deletions
+12 -9
View File
@@ -1,23 +1,24 @@
import logging
import os
from typing import Callable, Iterable, Set, Tuple
from typing import TYPE_CHECKING, Callable, Iterable, Set, Tuple
from dvc.output.base import BaseOutput
from dvc.path_info import PathInfo
from dvc.repo import Repo
from dvc.tree.repo import RepoTree
from dvc.types import DvcPath
if TYPE_CHECKING:
from dvc.output.base import BaseOutput
from dvc.repo import Repo
logger = logging.getLogger(__name__)
FilterFn = Callable[[BaseOutput], bool]
Outputs = Set[BaseOutput]
FilterFn = Callable[["BaseOutput"], bool]
Outputs = Set["BaseOutput"]
DvcPaths = Set[DvcPath]
def _collect_outs(
repo: Repo, output_filter: FilterFn = None, deps: bool = False,
repo: "Repo", output_filter: FilterFn = None, deps: bool = False,
) -> Outputs:
outs = {
out
@@ -28,11 +29,13 @@ def _collect_outs(
def _collect_paths(
repo: Repo,
repo: "Repo",
targets: Iterable[str],
recursive: bool = False,
rev: str = None,
):
from dvc.tree.repo import RepoTree
path_infos = {PathInfo(os.path.abspath(target)) for target in targets}
tree = RepoTree(repo)
@@ -67,7 +70,7 @@ def _filter_duplicates(
def collect(
repo: Repo,
repo: "Repo",
deps: bool = False,
targets: Iterable[str] = None,
output_filter: FilterFn = None,
+12 -8
View File
@@ -1,5 +1,5 @@
import logging
from typing import Dict, List
from typing import TYPE_CHECKING, Dict, List
from funcy import cached_property, first, project
@@ -9,14 +9,14 @@ from dvc.exceptions import (
NoMetricsFoundError,
NoMetricsParsedError,
)
from dvc.output import BaseOutput
from dvc.repo import Repo
from dvc.repo.collect import collect
from dvc.repo.plots.data import PlotParsingError
from dvc.schema import PLOT_PROPS
from dvc.tree.repo import RepoTree
from dvc.utils import relpath
if TYPE_CHECKING:
from dvc.output import BaseOutput
from dvc.repo import Repo
logger = logging.getLogger(__name__)
@@ -51,6 +51,8 @@ class Plots:
}}}
Data parsing is postponed, since it's affected by props.
"""
from dvc.tree.repo import RepoTree
targets = [targets] if isinstance(targets, str) else targets or []
data = {}
for rev in self.repo.brancher(revs=revs):
@@ -201,12 +203,12 @@ class Plots:
return PlotTemplates(self.repo.dvc_dir)
def _is_plot(out: BaseOutput) -> bool:
def _is_plot(out: "BaseOutput") -> bool:
return bool(out.plot) or bool(out.live)
def _collect_plots(
repo: Repo,
repo: "Repo",
targets: List[str] = None,
rev: str = None,
recursive: bool = False,
@@ -223,7 +225,9 @@ def _collect_plots(
return result
def _plot_props(out: BaseOutput) -> Dict:
def _plot_props(out: "BaseOutput") -> Dict:
from dvc.schema import PLOT_PROPS
if not (out.plot or out.live):
raise NotAPlotError(out)
if isinstance(out.plot, list):
+2 -1
View File
@@ -5,7 +5,6 @@ from typing import Any, Dict, Optional
from funcy import cached_property
from dvc.exceptions import DvcException
from dvc.utils.fs import makedirs
class TemplateNotFoundError(DvcException):
@@ -572,6 +571,8 @@ class PlotTemplates:
self.dvc_dir = dvc_dir
def init(self):
from dvc.utils.fs import makedirs
makedirs(self.templates_dir, exist_ok=True)
for t in self.TEMPLATES:
self._dump(t())