From fd33071b1b7cd506fcac5dfb1b75fa4a152fe955 Mon Sep 17 00:00:00 2001 From: Ayush Kamat Date: Wed, 31 May 2023 12:48:37 -0700 Subject: [PATCH 1/3] add deprecation warnings Signed-off-by: Ayush Kamat --- latch/__init__.py | 34 +++++++++++++++++++++++++++++++ latch/_deprecation.py | 44 +++++++++++++++++++++++++++++++++++++++++ latch/types/__init__.py | 31 +++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 latch/_deprecation.py diff --git a/latch/__init__.py b/latch/__init__.py index b1d11553..078566d2 100644 --- a/latch/__init__.py +++ b/latch/__init__.py @@ -4,6 +4,7 @@ Latch platform. """ +from latch._deprecation import _deprecated, _deprecated_import from latch.functions.messages import message from latch.functions.operators import ( combine, @@ -26,3 +27,36 @@ small_task, ) from latch.resources.workflow import workflow + +message = _deprecated_import("message", "latch.functions.messages")(message) + +combine = _deprecated()(combine) +group_tuple = _deprecated()(group_tuple) +inner_join = _deprecated()(inner_join) +latch_filter = _deprecated()(latch_filter) +left_join = _deprecated()(left_join) +outer_join = _deprecated()(outer_join) +right_join = _deprecated()(right_join) + +create_conditional_section = _deprecated_import( + "create_conditional_section", "latch.resources.conditional" +)(create_conditional_section) + +map_task = _deprecated_import("map_task", "latch.resources.map_tasks")(map_task) + +workflow_reference = _deprecated_import( + "workflow_reference", "latch.resources.reference_workflow" +)(workflow_reference) + +custom_task = _deprecated_import("custom_task", "latch.resources.tasks")(custom_task) +large_gpu_task = _deprecated_import("large_gpu_task", "latch.resources.tasks")( + large_gpu_task +) +large_task = _deprecated_import("large_task", "latch.resources.tasks")(large_task) +medium_task = _deprecated_import("medium_task", "latch.resources.tasks")(medium_task) +small_gpu_task = _deprecated_import("small_gpu_task", "latch.resources.tasks")( + small_gpu_task +) +small_task = _deprecated_import("small_task", "latch.resources.tasks")(small_task) + +workflow = _deprecated_import("workflow", "latch.resources.workflow")(workflow) 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..4f3b7ef7 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) From 4ca04e7d2acca49ed17c2729dad2344eec6b871b Mon Sep 17 00:00:00 2001 From: Ayush Kamat Date: Thu, 26 Oct 2023 16:18:23 -0700 Subject: [PATCH 2/3] save state Signed-off-by: Ayush Kamat --- latch/__init__.py | 45 +++++++++++++---------------------- latch/types/__init__.py | 52 ++++++++++++++++++++--------------------- 2 files changed, 42 insertions(+), 55 deletions(-) diff --git a/latch/__init__.py b/latch/__init__.py index 8ea04f5c..f3a4c76f 100644 --- a/latch/__init__.py +++ b/latch/__init__.py @@ -4,8 +4,9 @@ Latch platform. """ -from latch._deprecation import _deprecated, _deprecated_import -from latch.functions.messages import message +import warnings +from typing import Any, Dict + from latch.functions.operators import ( combine, group_tuple, @@ -19,8 +20,8 @@ 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, + custom_task, large_gpu_task, large_task, medium_task, @@ -29,35 +30,21 @@ ) from latch.resources.workflow import workflow -message = _deprecated_import("message", "latch.functions.messages")(message) +_deprecation_version = "3.0.0" -combine = _deprecated()(combine) -group_tuple = _deprecated()(group_tuple) -inner_join = _deprecated()(inner_join) -latch_filter = _deprecated()(latch_filter) -left_join = _deprecated()(left_join) -outer_join = _deprecated()(outer_join) -right_join = _deprecated()(right_join) -create_conditional_section = _deprecated_import( - "create_conditional_section", "latch.resources.conditional" -)(create_conditional_section) +def _warn(name: str, import_path: str): + warnings.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 {import_path} import {name}`\n", + DeprecationWarning, + ) -map_task = _deprecated_import("map_task", "latch.resources.map_tasks")(map_task) -workflow_reference = _deprecated_import( - "workflow_reference", "latch.resources.reference_workflow" -)(workflow_reference) +def message(typ: str, data: Dict[str, Any]): + from latch.functions.messages import message as _message -custom_task = _deprecated_import("custom_task", "latch.resources.tasks")(custom_task) -large_gpu_task = _deprecated_import("large_gpu_task", "latch.resources.tasks")( - large_gpu_task -) -large_task = _deprecated_import("large_task", "latch.resources.tasks")(large_task) -medium_task = _deprecated_import("medium_task", "latch.resources.tasks")(medium_task) -small_gpu_task = _deprecated_import("small_gpu_task", "latch.resources.tasks")( - small_gpu_task -) -small_task = _deprecated_import("small_task", "latch.resources.tasks")(small_task) + _warn("message", "latch.functions.messages") -workflow = _deprecated_import("workflow", "latch.resources.workflow")(workflow) + return _message(typ, data) diff --git a/latch/types/__init__.py b/latch/types/__init__.py index 4f3b7ef7..a0a44091 100644 --- a/latch/types/__init__.py +++ b/latch/types/__init__.py @@ -16,32 +16,32 @@ Text, ) -LatchDir = _deprecated_import("LatchDir", "latch.types.directory")(LatchDir) -LatchOutputDir = _deprecated_import("LatchOutputDir", "latch.types.directory")( - LatchOutputDir -) +# 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 -) +# 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) +# 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) +# 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) From 262ce7c8f030ece76475ad249487f439400841f7 Mon Sep 17 00:00:00 2001 From: Ayush Kamat Date: Tue, 28 Nov 2023 11:22:05 -0800 Subject: [PATCH 3/3] redo Signed-off-by: Ayush Kamat --- latch/__init__.py | 105 +++++++++++++++++++++++++++------------------- 1 file changed, 62 insertions(+), 43 deletions(-) diff --git a/latch/__init__.py b/latch/__init__.py index f3a4c76f..2c81218d 100644 --- a/latch/__init__.py +++ b/latch/__init__.py @@ -4,47 +4,66 @@ Latch platform. """ +import importlib +import sys import warnings -from typing import Any, Dict - -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_memory_optimized_task, - custom_task, - large_gpu_task, - large_task, - medium_task, - small_gpu_task, - small_task, -) -from latch.resources.workflow import workflow - -_deprecation_version = "3.0.0" - - -def _warn(name: str, import_path: str): - warnings.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 {import_path} import {name}`\n", - DeprecationWarning, - ) - - -def message(typ: str, data: Dict[str, Any]): - from latch.functions.messages import message as _message - - _warn("message", "latch.functions.messages") - - return _message(typ, data) +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=[])