diff --git a/latch/__init__.py b/latch/__init__.py index f01f732a..2c81218d 100644 --- a/latch/__init__.py +++ b/latch/__init__.py @@ -4,26 +4,66 @@ Latch platform. """ -from latch.functions.messages import message -from latch.functions.operators import ( - combine, - group_tuple, - inner_join, - latch_filter, - left_join, - outer_join, - right_join, -) -from latch.resources.conditional import create_conditional_section -from latch.resources.map_tasks import map_task -from latch.resources.reference_workflow import workflow_reference -from latch.resources.tasks import ( - custom_task, - custom_memory_optimized_task, - large_gpu_task, - large_task, - medium_task, - small_gpu_task, - small_task, -) -from latch.resources.workflow import workflow +import importlib +import sys +import warnings +from textwrap import dedent +from types import ModuleType +from typing import Callable + +_imports = { + "latch.functions.operators": [ + "combine", + "group_tuple", + "inner_join", + "latch_filter", + "left_join", + "outer_join", + "right_join", + ], + "latch.resources.conditional": ["create_conditional_section"], + "latch.resources.map_tasks": ["map_task"], + "latch.resources.reference_workflow": ["workflow_reference"], + "latch.resources.tasks": [ + "custom_memory_optimized_task", + "custom_task", + "large_gpu_task", + "large_task", + "medium_task", + "small_gpu_task", + "small_task", + ], + "latch.resources.workflow": ["workflow"], +} + + +def deprecated(module: ModuleType, fn_name: str) -> Callable: + fn = getattr(module, fn_name) + + def new_fn(*args, **kwargs): + warnings.warn( + dedent(f""" + + Importing `{fn_name}` directly from `latch` is deprecated. Please use the full import + + from {module.__name__} import {fn_name} + + This will be removed in version 3.0.0. + """), + DeprecationWarning, + ) + + return fn(*args, **kwargs) + + return new_fn + + +module = sys.modules[__name__] +for module_name, fn_names in _imports.items(): + imported = importlib.import_module(module_name) + + for fn_name in fn_names: + setattr(module, fn_name, deprecated(imported, fn_name)) + + +__slots__ = sum(_imports.values(), start=[]) diff --git a/latch/_deprecation.py b/latch/_deprecation.py new file mode 100644 index 00000000..0c79f139 --- /dev/null +++ b/latch/_deprecation.py @@ -0,0 +1,44 @@ +from typing import Callable, TypeVar +from warnings import warn + +from typing_extensions import ParamSpec + +T = TypeVar("T") +P = ParamSpec("P") + + +_DEPRECATION_VERSION = "3.0.0" + + +def _deprecated_import( + name: str, + new_import_source: str, +) -> Callable[[Callable[P, T]], Callable[P, T]]: + def decorator(f: Callable[P, T]) -> Callable[P, T]: + warn( + ( + f"Importing `{name}` directly from `latch` is deprecated, and will" + f" be removed in version {_DEPRECATION_VERSION}.\n\n Please use" + f" the full import `from {new_import_source} import {name}`\n" + ), + DeprecationWarning, + ) + + return f + + return decorator + + +def _deprecated() -> Callable[[Callable[P, T]], Callable[P, T]]: + def decorator(f: Callable[P, T]) -> Callable[P, T]: + warn( + ( + f"{f.__name__} is deprecated, and will be removed in version" + f" {_DEPRECATION_VERSION}." + ), + DeprecationWarning, + ) + + return f + + return decorator diff --git a/latch/types/__init__.py b/latch/types/__init__.py index fca24155..a0a44091 100644 --- a/latch/types/__init__.py +++ b/latch/types/__init__.py @@ -1,3 +1,4 @@ +from latch._deprecation import _deprecated_import from latch.types.directory import LatchDir, LatchOutputDir from latch.types.file import LatchFile, LatchOutputFile from latch.types.glob import file_glob @@ -14,3 +15,33 @@ Spoiler, Text, ) + +# LatchDir = _deprecated_import("LatchDir", "latch.types.directory")(LatchDir) +# LatchOutputDir = _deprecated_import("LatchOutputDir", "latch.types.directory")( +# LatchOutputDir +# ) + +# LatchFile = _deprecated_import("LatchFile", "latch.types.file")(LatchFile) +# LatchOutputFile = _deprecated_import("LatchOutputFile", "latch.types.file")( +# LatchOutputFile +# ) + +# file_glob = _deprecated_import("file_glob", "latch.types.glob")(file_glob) + +# Fork = _deprecated_import("Fork", "latch.types.metadata")(Fork) +# ForkBranch = _deprecated_import("ForkBranch", "latch.types.metadata")(ForkBranch) +# LatchAppearanceType = _deprecated_import("LatchAppearanceType", "latch.types.metadata")( +# LatchAppearanceType +# ) +# LatchAuthor = _deprecated_import("LatchAuthor", "latch.types.metadata")(LatchAuthor) +# LatchMetadata = _deprecated_import("LatchMetadata", "latch.types.metadata")( +# LatchMetadata +# ) +# LatchParameter = _deprecated_import("LatchParameter", "latch.types.metadata")( +# LatchParameter +# ) +# LatchRule = _deprecated_import("LatchRule", "latch.types.metadata")(LatchRule) +# Params = _deprecated_import("Params", "latch.types.metadata")(Params) +# Section = _deprecated_import("Section", "latch.types.metadata")(Section) +# Spoiler = _deprecated_import("Spoiler", "latch.types.metadata")(Spoiler) +# Text = _deprecated_import("Text", "latch.types.metadata")(Text)