-
Notifications
You must be signed in to change notification settings - Fork 1.3k
refactor(client): consolidate three identical pathspec identifier patterns into one #3263
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
Open
dheerenmohta
wants to merge
2
commits into
Netflix:master
Choose a base branch
from
dheerenmohta:refactor/client-consolidate-pathspec-regex-patterns
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+326
−16
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,225 @@ | ||
| """ | ||
| Unit tests for pathspec validation in MetaflowObject. | ||
|
|
||
| Tests the validation added in issue #948 to ensure pathspecs follow the expected format: | ||
| - Flow: FlowName | ||
| - Run: FlowName/RunID | ||
| - Step: FlowName/RunID/StepName | ||
| - Task: FlowName/RunID/StepName/TaskID | ||
| - DataArtifact: FlowName/RunID/StepName/TaskID/ArtifactName | ||
| """ | ||
|
|
||
| import unittest | ||
| from metaflow.exception import MetaflowInvalidPathspec | ||
| from metaflow.client.core import MetaflowObject | ||
|
|
||
|
|
||
| class TestPathspecValidation(unittest.TestCase): | ||
| """Test pathspec format validation.""" | ||
|
|
||
| def test_flow_valid_pathspecs(self): | ||
| """Test that valid flow pathspecs are accepted.""" | ||
| valid_pathspecs = [ | ||
| "MyFlow", | ||
| "my_flow", | ||
| "MyFlow123", | ||
| "_private_flow", | ||
| "Flow_With_Underscores", | ||
| ] | ||
| for pathspec in valid_pathspecs: | ||
| with self.subTest(pathspec=pathspec): | ||
| result = MetaflowObject._validate_pathspec_format(pathspec, "flow") | ||
| self.assertEqual(result, [pathspec]) | ||
|
|
||
| def test_flow_invalid_pathspecs(self): | ||
| """Test that invalid flow pathspecs are rejected.""" | ||
| invalid_cases = [ | ||
| ("", "empty"), | ||
| ("/MyFlow", "leading slash"), | ||
| ("MyFlow/", "trailing slash"), | ||
| ("123Flow", "starts with number"), | ||
| ("My-Flow", "contains dash"), | ||
| ("My Flow", "contains space"), | ||
| ("My/Flow", "contains slash"), | ||
| ] | ||
| for pathspec, description in invalid_cases: | ||
| with self.subTest(pathspec=pathspec, reason=description): | ||
| with self.assertRaises(MetaflowInvalidPathspec): | ||
| MetaflowObject._validate_pathspec_format(pathspec, "flow") | ||
|
|
||
| def test_run_valid_pathspecs(self): | ||
| """Test that valid run pathspecs are accepted.""" | ||
| valid_pathspecs = [ | ||
| "MyFlow/123", | ||
| "my_flow/456", | ||
| "MyFlow123/789", | ||
| "_private_flow/1", | ||
| ] | ||
| for pathspec in valid_pathspecs: | ||
| with self.subTest(pathspec=pathspec): | ||
| result = MetaflowObject._validate_pathspec_format(pathspec, "run") | ||
| self.assertEqual(len(result), 2) | ||
|
|
||
| def test_run_invalid_pathspecs(self): | ||
| """Test that invalid run pathspecs are rejected.""" | ||
| invalid_cases = [ | ||
| ("MyFlow", "too few components"), | ||
| ("MyFlow/123/extra", "too many components"), | ||
| ("MyFlow/abc", "non-numeric run ID"), | ||
| ("MyFlow//123", "empty component"), | ||
| ("MyFlow/123/", "trailing slash"), | ||
| ("/MyFlow/123", "leading slash"), | ||
| ("123Flow/123", "invalid flow name"), | ||
| ("MyFlow/12.3", "decimal run ID"), | ||
| ("MyFlow/-123", "negative run ID"), | ||
| ] | ||
| for pathspec, description in invalid_cases: | ||
| with self.subTest(pathspec=pathspec, reason=description): | ||
| with self.assertRaises(MetaflowInvalidPathspec): | ||
| MetaflowObject._validate_pathspec_format(pathspec, "run") | ||
|
|
||
| def test_step_valid_pathspecs(self): | ||
| """Test that valid step pathspecs are accepted.""" | ||
| valid_pathspecs = [ | ||
| "MyFlow/123/start", | ||
| "my_flow/456/end", | ||
| "MyFlow/789/my_step", | ||
| "_private_flow/1/_private_step", | ||
| ] | ||
| for pathspec in valid_pathspecs: | ||
| with self.subTest(pathspec=pathspec): | ||
| result = MetaflowObject._validate_pathspec_format(pathspec, "step") | ||
| self.assertEqual(len(result), 3) | ||
|
|
||
| def test_step_invalid_pathspecs(self): | ||
| """Test that invalid step pathspecs are rejected.""" | ||
| invalid_cases = [ | ||
| ("MyFlow/123", "too few components"), | ||
| ("MyFlow/123/start/extra", "too many components"), | ||
| ("MyFlow/abc/start", "non-numeric run ID"), | ||
| ("MyFlow/123/123step", "step name starts with number"), | ||
| ("MyFlow/123/my-step", "step name contains dash"), | ||
| ("MyFlow//start", "empty run ID"), | ||
| ("MyFlow/123//", "empty step name"), | ||
| ] | ||
| for pathspec, description in invalid_cases: | ||
| with self.subTest(pathspec=pathspec, reason=description): | ||
| with self.assertRaises(MetaflowInvalidPathspec): | ||
| MetaflowObject._validate_pathspec_format(pathspec, "step") | ||
|
|
||
| def test_task_valid_pathspecs(self): | ||
| """Test that valid task pathspecs are accepted.""" | ||
| valid_pathspecs = [ | ||
| "MyFlow/123/start/1", | ||
| "my_flow/456/end/999", | ||
| "MyFlow/789/my_step/42", | ||
| "_private_flow/1/_private_step/0", | ||
| ] | ||
| for pathspec in valid_pathspecs: | ||
| with self.subTest(pathspec=pathspec): | ||
| result = MetaflowObject._validate_pathspec_format(pathspec, "task") | ||
| self.assertEqual(len(result), 4) | ||
|
|
||
| def test_task_invalid_pathspecs(self): | ||
| """Test that invalid task pathspecs are rejected.""" | ||
| invalid_cases = [ | ||
| ("MyFlow/123/start", "too few components"), | ||
| ("MyFlow/123/start/1/extra", "too many components"), | ||
| ("MyFlow/abc/start/1", "non-numeric run ID"), | ||
| ("MyFlow/123/start/abc", "non-numeric task ID"), | ||
| ("MyFlow/123/start/1.5", "decimal task ID"), | ||
| ("MyFlow/123///1", "empty step name"), | ||
| ] | ||
| for pathspec, description in invalid_cases: | ||
| with self.subTest(pathspec=pathspec, reason=description): | ||
| with self.assertRaises(MetaflowInvalidPathspec): | ||
| MetaflowObject._validate_pathspec_format(pathspec, "task") | ||
|
|
||
| def test_artifact_valid_pathspecs(self): | ||
| """Test that valid artifact pathspecs are accepted.""" | ||
| valid_pathspecs = [ | ||
| "MyFlow/123/start/1/my_artifact", | ||
| "my_flow/456/end/999/result", | ||
| "MyFlow/789/my_step/42/_private_var", | ||
| "_private_flow/1/_private_step/0/data", | ||
| ] | ||
| for pathspec in valid_pathspecs: | ||
| with self.subTest(pathspec=pathspec): | ||
| result = MetaflowObject._validate_pathspec_format( | ||
| pathspec, "artifact" | ||
| ) | ||
| self.assertEqual(len(result), 5) | ||
|
|
||
| def test_artifact_invalid_pathspecs(self): | ||
| """Test that invalid artifact pathspecs are rejected.""" | ||
| invalid_cases = [ | ||
| ("MyFlow/123/start/1", "too few components"), | ||
| ("MyFlow/123/start/1/artifact/extra", "too many components"), | ||
| ("MyFlow/abc/start/1/artifact", "non-numeric run ID"), | ||
| ("MyFlow/123/start/abc/artifact", "non-numeric task ID"), | ||
| ("MyFlow/123/start/1/123artifact", "artifact name starts with number"), | ||
| ("MyFlow/123/start/1/my-artifact", "artifact name contains dash"), | ||
| ("MyFlow/123/start//artifact", "empty task ID"), | ||
| ] | ||
| for pathspec, description in invalid_cases: | ||
| with self.subTest(pathspec=pathspec, reason=description): | ||
| with self.assertRaises(MetaflowInvalidPathspec): | ||
| MetaflowObject._validate_pathspec_format(pathspec, "artifact") | ||
|
|
||
| def test_empty_components(self): | ||
| """Test that pathspecs with empty components are rejected.""" | ||
| invalid_pathspecs = [ | ||
| ("//", "flow"), | ||
| ("Flow//123", "run"), | ||
| ("Flow/123//", "step"), | ||
| ("Flow//step/1", "run"), | ||
| ("Flow/123/step//", "task"), | ||
| ] | ||
| for pathspec, object_type in invalid_pathspecs: | ||
| with self.subTest(pathspec=pathspec, object_type=object_type): | ||
| with self.assertRaises(MetaflowInvalidPathspec) as cm: | ||
| MetaflowObject._validate_pathspec_format(pathspec, object_type) | ||
| self.assertIn("empty", str(cm.exception).lower()) | ||
|
|
||
| def test_leading_trailing_slashes(self): | ||
| """Test that pathspecs with leading or trailing slashes are rejected.""" | ||
| invalid_pathspecs = [ | ||
| ("/MyFlow", "flow"), | ||
| ("MyFlow/", "flow"), | ||
| ("/MyFlow/123", "run"), | ||
| ("MyFlow/123/", "run"), | ||
| ("/MyFlow/123/start", "step"), | ||
| ("MyFlow/123/start/", "step"), | ||
| ] | ||
| for pathspec, object_type in invalid_pathspecs: | ||
| with self.subTest(pathspec=pathspec, object_type=object_type): | ||
| with self.assertRaises(MetaflowInvalidPathspec) as cm: | ||
| MetaflowObject._validate_pathspec_format(pathspec, object_type) | ||
| self.assertIn("cannot start or end", str(cm.exception).lower()) | ||
|
|
||
| def test_error_messages_are_helpful(self): | ||
| """Test that error messages provide helpful information.""" | ||
| # Test invalid flow name | ||
| with self.assertRaises(MetaflowInvalidPathspec) as cm: | ||
| MetaflowObject._validate_pathspec_format("123Flow", "flow") | ||
| error_msg = str(cm.exception) | ||
| self.assertIn("123Flow", error_msg) | ||
| self.assertIn("flow name", error_msg.lower()) | ||
|
|
||
| # Test invalid run ID | ||
| with self.assertRaises(MetaflowInvalidPathspec) as cm: | ||
| MetaflowObject._validate_pathspec_format("MyFlow/abc", "run") | ||
| error_msg = str(cm.exception) | ||
| self.assertIn("abc", error_msg) | ||
| self.assertIn("run ID", error_msg) | ||
| self.assertIn("numeric", error_msg.lower()) | ||
|
|
||
| # Test wrong number of components | ||
| with self.assertRaises(MetaflowInvalidPathspec) as cm: | ||
| MetaflowObject._validate_pathspec_format("MyFlow", "run") | ||
| error_msg = str(cm.exception) | ||
| self.assertIn("Run('FlowName/RunID')", error_msg) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_NUMERIC_ID_PATTERNbreaks Argo Workflows and Step Functions run IDsMetaflow has two production-grade backends that assign non-numeric run IDs: Argo Workflows uses
argo-<workflow-name>(e.g."argo-myflow-1a2b3c") and AWS Step Functions usessfn-<execution-id>(e.g."sfn-abc123"). After this change, any call toRun("MyFlow/argo-myflow-1a2b3c")will immediately raiseMetaflowInvalidPathspec("Invalid run ID … Run IDs must be numeric")before even hitting the metadata service, completely preventing access to runs produced by those schedulers.Both
local.py'sregister_run_idandregister_task_idexplicitly handle non-integer IDs via atry: int(…) except ValueErrorbranch, which documents that arbitrary-string IDs are a first-class, intended format. The_NUMERIC_ID_PATTERNvalidation contradicts that contract. Either the run-ID and task-ID positions should be left unvalidated (only length/format checks), or the pattern must be broadened to cover any non-empty, non-whitespace token.