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

add deprecation warnings #278

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
86 changes: 63 additions & 23 deletions latch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=[])
44 changes: 44 additions & 0 deletions latch/_deprecation.py
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions latch/types/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Loading