From 3435585886cac69c8798583f878e00e7c96825ed Mon Sep 17 00:00:00 2001 From: Darin Yu Date: Thu, 14 Mar 2024 22:08:15 +0000 Subject: [PATCH 1/3] fix bug where client can not access foreach stack --- metaflow/__init__.py | 3 +++ metaflow/plugins/datatools/s3/s3.py | 2 +- metaflow/task.py | 13 +------------ metaflow/util.py | 15 --------------- 4 files changed, 5 insertions(+), 28 deletions(-) diff --git a/metaflow/__init__.py b/metaflow/__init__.py index 619f17d4ec2..9c298a0d431 100644 --- a/metaflow/__init__.py +++ b/metaflow/__init__.py @@ -143,6 +143,9 @@ class and related decorators. DataArtifact, ) +# Data class needed for Client. +from .tuple_util import ForeachFrame + __version_addl__ = [] _ext_debug("Loading top-level modules") for m in _tl_modules: diff --git a/metaflow/plugins/datatools/s3/s3.py b/metaflow/plugins/datatools/s3/s3.py index d2f125ca4c4..aeffbabcd11 100644 --- a/metaflow/plugins/datatools/s3/s3.py +++ b/metaflow/plugins/datatools/s3/s3.py @@ -21,7 +21,6 @@ TEMPDIR, ) from metaflow.util import ( - namedtuple_with_defaults, is_stringish, to_bytes, to_unicode, @@ -29,6 +28,7 @@ url_quote, url_unquote, ) +from metaflow.tuple_util import namedtuple_with_defaults from metaflow.exception import MetaflowException from metaflow.debug import debug import metaflow.tracing as tracing diff --git a/metaflow/task.py b/metaflow/task.py index 0afbda78425..b9e7274f8ff 100644 --- a/metaflow/task.py +++ b/metaflow/task.py @@ -23,18 +23,7 @@ from .clone_util import clone_task_helper from .metaflow_current import current from metaflow.tracing import get_trace_id -from metaflow.util import namedtuple_with_defaults - -foreach_frame_field_list = [ - ("step", str), - ("var", str), - ("num_splits", int), - ("index", int), - ("value", str), -] -ForeachFrame = namedtuple_with_defaults( - "ForeachFrame", foreach_frame_field_list, (None,) * (len(foreach_frame_field_list)) -) +from metaflow.tuple_util import ForeachFrame # Maximum number of characters of the foreach path that we store in the metadata. MAX_FOREACH_PATH_LENGTH = 256 diff --git a/metaflow/util.py b/metaflow/util.py index 0f022d94ce8..78d281f932b 100644 --- a/metaflow/util.py +++ b/metaflow/util.py @@ -51,21 +51,6 @@ def unquote_bytes(x): from shlex import quote as _quote -from typing import NamedTuple - - -def namedtuple_with_defaults(typename, field_descr, defaults=()): - T = NamedTuple(typename, field_descr) - T.__new__.__defaults__ = tuple(defaults) - - # Adding the following to ensure the named tuple can be (un)pickled correctly. - import __main__ - - setattr(__main__, T.__name__, T) - T.__module__ = "__main__" - return T - - class TempDir(object): # Provide a temporary directory since Python 2.7 does not have it inbuilt def __enter__(self): From f8d5c491aeda7b8bd933e5f4cce8df9167be2304 Mon Sep 17 00:00:00 2001 From: Darin Yu Date: Thu, 14 Mar 2024 22:13:45 +0000 Subject: [PATCH 2/3] include the file --- metaflow/tuple_util.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 metaflow/tuple_util.py diff --git a/metaflow/tuple_util.py b/metaflow/tuple_util.py new file mode 100644 index 00000000000..de6f1f245d5 --- /dev/null +++ b/metaflow/tuple_util.py @@ -0,0 +1,27 @@ +# Keep this file minimum dependency as this will be imported by metaflow at bootup. +def namedtuple_with_defaults(typename, field_descr, defaults=()): + from typing import NamedTuple + + T = NamedTuple(typename, field_descr) + T.__new__.__defaults__ = tuple(defaults) + + # Adding the following to ensure the named tuple can be (un)pickled correctly. + import __main__ + + setattr(__main__, T.__name__, T) + T.__module__ = "__main__" + return T + + +# Define the namedtuple with default here if they need to be accessible in client +# (and w/o a real flow). +foreach_frame_field_list = [ + ("step", str), + ("var", str), + ("num_splits", int), + ("index", int), + ("value", str), +] +ForeachFrame = namedtuple_with_defaults( + "ForeachFrame", foreach_frame_field_list, (None,) * (len(foreach_frame_field_list)) +) From 36eb274998dda57bfe0ded9935aad05c5d229bf2 Mon Sep 17 00:00:00 2001 From: Darin Yu Date: Thu, 14 Mar 2024 22:20:27 +0000 Subject: [PATCH 3/3] fix comments --- metaflow/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/metaflow/__init__.py b/metaflow/__init__.py index 9c298a0d431..4c7fa88725e 100644 --- a/metaflow/__init__.py +++ b/metaflow/__init__.py @@ -143,8 +143,8 @@ class and related decorators. DataArtifact, ) -# Data class needed for Client. -from .tuple_util import ForeachFrame +# Import data class within tuple_util but not introduce new symbols. +from . import tuple_util __version_addl__ = [] _ext_debug("Loading top-level modules")