Skip to content

Commit

Permalink
Use AntlrAssetSelectionParser in AssetSelection.from_string
Browse files Browse the repository at this point in the history
  • Loading branch information
briantu committed Nov 14, 2024
1 parent 197977a commit 0615b89
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,5 @@ def tree_str(self) -> str:
return self._tree_str

@property
def asset_selection(self) -> AssetSelection:
def asset_selection(self) -> "AssetSelection":
return self._asset_selection
48 changes: 27 additions & 21 deletions python_modules/dagster/dagster/_core/definitions/asset_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

import dagster._check as check
from dagster._annotations import deprecated, experimental, experimental_param, public
from dagster._core.definitions.antlr_asset_selection.antlr_asset_selection import (
AntlrAssetSelectionParser,
)
from dagster._core.definitions.asset_check_spec import AssetCheckKey
from dagster._core.definitions.asset_graph import AssetGraph
from dagster._core.definitions.asset_key import (
Expand Down Expand Up @@ -495,27 +498,30 @@ def resolve_checks_inner(

@classmethod
def from_string(cls, string: str) -> "AssetSelection":
if string == "*":
return cls.all()

parts = parse_clause(string)
if parts is not None:
key_selection = cls.assets(parts.item_name)
if parts.up_depth and parts.down_depth:
selection = key_selection.upstream(parts.up_depth) | key_selection.downstream(
parts.down_depth
)
elif parts.up_depth:
selection = key_selection.upstream(parts.up_depth)
elif parts.down_depth:
selection = key_selection.downstream(parts.down_depth)
else:
selection = key_selection
return selection

elif string.startswith("tag:"):
tag_str = string[len("tag:") :]
return cls.tag_string(tag_str)
try:
return AntlrAssetSelectionParser(string).asset_selection
except:
if string == "*":
return cls.all()

parts = parse_clause(string)
if parts is not None:
key_selection = cls.assets(parts.item_name)
if parts.up_depth and parts.down_depth:
selection = key_selection.upstream(parts.up_depth) | key_selection.downstream(
parts.down_depth
)
elif parts.up_depth:
selection = key_selection.upstream(parts.up_depth)
elif parts.down_depth:
selection = key_selection.downstream(parts.down_depth)
else:
selection = key_selection
return selection

elif string.startswith("tag:"):
tag_str = string[len("tag:") :]
return cls.tag_string(tag_str)

check.failed(f"Invalid selection string: {string}")

Expand Down

0 comments on commit 0615b89

Please sign in to comment.