Skip to content

Commit 9be1670

Browse files
Copilotrizzip
andauthored
[ADX-212] Make Pipeline user_id/app_id optional, default from CLI context (#1043)
* Initial plan * Make user_id and app_id optional in Pipeline.__init__ with fallback to CLI context Agent-Logs-Url: https://github.com/Clarifai/clarifai-python/sessions/dab343de-b778-409b-9439-1e5440d29368 Co-authored-by: rizzip <32918283+rizzip@users.noreply.github.com> * fix: remove unnecessary is None checks --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: rizzip <32918283+rizzip@users.noreply.github.com> Co-authored-by: Peter Rizzi <peter.rizzi@clarifai.com>
1 parent ef7a21b commit 9be1670

2 files changed

Lines changed: 59 additions & 1 deletion

File tree

clarifai/runners/pipelines/pipeline.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,19 @@
1919
class Pipeline:
2020
"""Code-first pipeline definition that compiles to the existing config format."""
2121

22-
def __init__(self, id: str, user_id: str, app_id: str, visibility: str = 'PRIVATE'):
22+
def __init__(
23+
self,
24+
id: str,
25+
user_id: Optional[str] = None,
26+
app_id: Optional[str] = None,
27+
visibility: str = 'PRIVATE',
28+
):
29+
user_id, app_id = self._resolve_from_context(user_id, app_id)
30+
if not user_id or not app_id:
31+
raise ValueError(
32+
"Pipeline(...) needs user_id and app_id. Pass them explicitly, "
33+
"or run `clarifai login` to set them in your CLI context."
34+
)
2335
self.id = id
2436
self.user_id = user_id
2537
self.app_id = app_id
@@ -30,6 +42,20 @@ def __init__(self, id: str, user_id: str, app_id: str, visibility: str = 'PRIVAT
3042
self._active = False
3143
self._uploaded_pipeline_version_id: Optional[str] = None
3244

45+
@staticmethod
46+
def _resolve_from_context(user_id: Optional[str], app_id: Optional[str]) -> tuple:
47+
"""Fill missing user_id/app_id from the current CLI context."""
48+
try:
49+
from clarifai.utils.config import Config
50+
51+
config = Config.from_yaml()
52+
ctx = config.current
53+
user_id = user_id or getattr(ctx, 'user_id', None)
54+
app_id = app_id or getattr(ctx, 'app_id', None)
55+
except Exception:
56+
pass
57+
return user_id, app_id
58+
3359
def __enter__(self):
3460
self._active = True
3561
_set_active_pipeline(self)

tests/runners/test_pipeline_dsl.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from pathlib import Path
2+
from types import SimpleNamespace
23
from unittest.mock import Mock, patch
34

45
import pytest
@@ -238,3 +239,34 @@ def test_step_ref_from_url_parses_versioned_pipeline_step_url():
238239
def test_step_ref_from_url_requires_versioned_pipeline_step_path():
239240
with pytest.raises(ValueError, match='versioned pipeline step URL or resource path'):
240241
step_ref.from_url('users/demo-user/apps/shared-app/pipeline_steps/summarize')
242+
243+
244+
def _mock_config_with_context(user_id, app_id):
245+
"""Return a mock Config whose .current gives a context with the given ids."""
246+
ctx = SimpleNamespace(user_id=user_id, app_id=app_id)
247+
config = Mock()
248+
config.current = ctx
249+
return config
250+
251+
252+
def test_pipeline_defaults_user_id_and_app_id_from_context():
253+
config = _mock_config_with_context('ctx-user', 'ctx-app')
254+
with patch('clarifai.utils.config.Config.from_yaml', return_value=config):
255+
pipeline = Pipeline(id='p1')
256+
assert pipeline.user_id == 'ctx-user'
257+
assert pipeline.app_id == 'ctx-app'
258+
259+
260+
def test_pipeline_explicit_args_override_context():
261+
config = _mock_config_with_context('ctx-user', 'ctx-app')
262+
with patch('clarifai.utils.config.Config.from_yaml', return_value=config):
263+
pipeline = Pipeline(id='p2', user_id='explicit-user', app_id='explicit-app')
264+
assert pipeline.user_id == 'explicit-user'
265+
assert pipeline.app_id == 'explicit-app'
266+
267+
268+
def test_pipeline_raises_without_context_or_explicit_args():
269+
config = _mock_config_with_context(None, None)
270+
with patch('clarifai.utils.config.Config.from_yaml', return_value=config):
271+
with pytest.raises(ValueError, match='clarifai login'):
272+
Pipeline(id='p3')

0 commit comments

Comments
 (0)