KubernetesException defined twice (kubernetes.py and kube_utils.py) causes except/isinstance mismatch across import paths
Description
KubernetesException is defined as two separate classes:
metaflow/plugins/kubernetes/kubernetes.py—class KubernetesException(MetaflowException)metaflow/plugins/kubernetes/kube_utils.py—class KubernetesException(MetaflowException)(with a comment: "avoid circular import by having the exception class contained here")
Different modules import different ones:
kubernetes_decorator.py:from .kubernetes import KubernetesExceptionkubernetes_cli.py:from metaflow.plugins.kubernetes.kube_utils import KubernetesException
Because these are two distinct class objects, an except KubernetesException
(or isinstance / is) using one import path will not match an exception
raised as the other. A raise of the kube_utils variant won't be caught by a
handler importing the kubernetes.py variant, and vice versa.
Mechanism
class MetaflowException(Exception): ...
def make():
class KubernetesException(MetaflowException): ...
return KubernetesException
A = make(); B = make() # two same-named but distinct classes
try:
raise B("boom")
except A: # does NOT catch B
...
# -> B propagates uncaught
Expected vs. actual
- Expected: one canonical
KubernetesException;exceptcatches it regardless of import path. - Actual: two classes; a handler importing one path misses raises from the other.
Root cause
Duplicate class definitions. The kube_utils.py copy exists to avoid a circular
import — kube_utils is a low-level module and cannot import from kubernetes.py.
Proposed fix
Keep the canonical definition in the low-level module (kube_utils.py) and
re-export it from kubernetes.py (from .kube_utils import KubernetesException).
kube_utils only imports metaflow.exception and metaflow.util, so
kubernetes.py importing it introduces no cycle. Both existing import paths keep
working and now resolve to the same object. A PR follows with a regression test.
Environment
- Metaflow version: 2.19.35 (also present on current
master) - Files:
metaflow/plugins/kubernetes/kubernetes.py,metaflow/plugins/kubernetes/kube_utils.py