Skip to content

Commit 1a156b6

Browse files
committed
[dg] Refactor PackageEntry types
1 parent b5fa109 commit 1a156b6

File tree

11 files changed

+155
-93
lines changed

11 files changed

+155
-93
lines changed

python_modules/dagster/dagster/components/cli/list.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
discover_entry_point_package_entries,
2727
discover_package_entries,
2828
)
29-
from dagster.components.core.snapshot import get_library_object_snap
29+
from dagster.components.core.snapshot import get_package_entry_snap
3030

3131

3232
@click.group(name="list")
@@ -44,7 +44,7 @@ def list_library_command(
4444
"""List registered library objects."""
4545
library_objects = _load_library_objects(entry_points, extra_modules)
4646
serialized_snaps = serialize_value(
47-
[get_library_object_snap(key, obj) for key, obj in library_objects.items()]
47+
[get_package_entry_snap(key, obj) for key, obj in library_objects.items()]
4848
)
4949
click.echo(serialized_snaps)
5050

python_modules/dagster/dagster/components/core/snapshot.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
from dagster_shared.serdes.objects.package_entry import (
55
ComponentTypeData,
6-
LibraryEntryKey,
7-
LibraryEntrySnap,
6+
PackageEntryKey,
7+
PackageEntrySnap,
88
ScaffoldTargetTypeData,
99
)
1010

@@ -43,12 +43,12 @@ def _get_scaffold_target_type_data(scaffolder: Scaffolder) -> ScaffoldTargetType
4343
)
4444

4545

46-
def get_library_object_snap(key: LibraryEntryKey, obj: object) -> LibraryEntrySnap:
46+
def get_package_entry_snap(key: PackageEntryKey, obj: object) -> PackageEntrySnap:
4747
type_data = []
4848
if isinstance(obj, type) and issubclass(obj, Component):
4949
type_data.append(_get_component_type_data(obj))
5050
scaffolder = get_scaffolder(obj) if isinstance(obj, type) else None
5151
if isinstance(scaffolder, Scaffolder):
5252
type_data.append(_get_scaffold_target_type_data(scaffolder))
5353
summary, description = _get_summary_and_description(obj)
54-
return LibraryEntrySnap(key=key, summary=summary, description=description, type_data=type_data)
54+
return PackageEntrySnap(key=key, summary=summary, description=description, type_data=type_data)

python_modules/dagster/dagster_tests/components_tests/cli_tests/test_commands.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
from dagster.components.cli import cli
88
from dagster_shared import check
99
from dagster_shared.serdes.objects import (
10-
ComponentTypeSnap,
10+
ComponentTypeData,
1111
PackageEntryKey,
1212
PackageEntrySnap,
13-
ScaffolderSnap,
13+
ScaffoldTargetTypeData,
1414
)
1515
from dagster_shared.serdes.serdes import deserialize_value
1616
from jsonschema import Draft202012Validator, ValidationError
@@ -50,21 +50,25 @@ def test_list_library_objects_from_module():
5050
"dagster_test.components.SimplePipesScriptComponent",
5151
]
5252

53-
assert result[2] == ComponentTypeSnap(
53+
assert result[2] == PackageEntrySnap(
5454
key=PackageEntryKey(namespace="dagster_test.components", name="SimpleAssetComponent"),
55-
schema={
56-
"additionalProperties": False,
57-
"properties": {
58-
"asset_key": {"title": "Asset Key", "type": "string"},
59-
"value": {"title": "Value", "type": "string"},
60-
},
61-
"required": ["asset_key", "value"],
62-
"title": "SimpleAssetComponentModel",
63-
"type": "object",
64-
},
6555
description="A simple asset that returns a constant string value.",
6656
summary="A simple asset that returns a constant string value.",
67-
scaffolder=ScaffolderSnap(schema=None),
57+
type_data=[
58+
ComponentTypeData(
59+
schema={
60+
"additionalProperties": False,
61+
"properties": {
62+
"asset_key": {"title": "Asset Key", "type": "string"},
63+
"value": {"title": "Value", "type": "string"},
64+
},
65+
"required": ["asset_key", "value"],
66+
"title": "SimpleAssetComponentModel",
67+
"type": "object",
68+
}
69+
),
70+
ScaffoldTargetTypeData(schema=None),
71+
],
6872
)
6973

7074
pipes_script_params_schema = {
@@ -77,12 +81,14 @@ def test_list_library_objects_from_module():
7781
"type": "object",
7882
}
7983

80-
assert result[3] == ComponentTypeSnap(
84+
assert result[3] == PackageEntrySnap(
8185
key=PackageEntryKey(namespace="dagster_test.components", name="SimplePipesScriptComponent"),
82-
schema=pipes_script_params_schema,
8386
description="A simple asset that runs a Python script with the Pipes subprocess client.\n\nBecause it is a pipes asset, no value is returned.",
8487
summary="A simple asset that runs a Python script with the Pipes subprocess client.",
85-
scaffolder=ScaffolderSnap(schema=pipes_script_params_schema),
88+
type_data=[
89+
ComponentTypeData(schema=pipes_script_params_schema),
90+
ScaffoldTargetTypeData(schema=pipes_script_params_schema),
91+
],
8692
)
8793

8894

python_modules/dagster/dagster_tests/components_tests/registry_tests/test_registry.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from dagster._utils import pushd
1212
from dagster.components import Component
1313
from dagster.components.core.package_entry import discover_entry_point_package_entries
14-
from dagster.components.core.snapshot import get_library_object_snap
14+
from dagster.components.core.snapshot import get_package_entry_snap
1515
from dagster_dg.utils import get_venv_executable
1616
from dagster_shared.serdes.objects import PackageEntryKey
1717
from dagster_shared.serdes.serdes import deserialize_value
@@ -129,7 +129,7 @@ def test_all_components_have_defined_summary():
129129
registry = discover_entry_point_package_entries()
130130
for component_name, component_type in registry.items():
131131
if isinstance(component_type, type) and issubclass(component_type, Component):
132-
assert get_library_object_snap(
132+
assert get_package_entry_snap(
133133
PackageEntryKey("a", "a"), component_type
134134
).summary, f"Component {component_name} has no summary defined"
135135

python_modules/libraries/dagster-dg/dagster_dg/check.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def check_yaml(
117117
)
118118
for key, component_doc_tree in component_contents_by_key.items():
119119
try:
120-
json_schema = component_registry.get_component_type(key).schema or {}
120+
json_schema = component_registry.get(key).component_schema or {}
121121

122122
v = Draft202012Validator(json_schema)
123123
for err in v.iter_errors(component_doc_tree.value["attributes"]):

python_modules/libraries/dagster-dg/dagster_dg/cli/list.py

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import json
22
from dataclasses import asdict
33
from pathlib import Path
4-
from typing import Any
4+
from typing import Any, Optional
55

66
import click
7-
from dagster_shared.serdes.objects import ComponentTypeSnap
87
from rich.console import Console
98
from rich.table import Table
109

@@ -68,27 +67,17 @@ def list_component_command(**global_options: object) -> None:
6867
# ########################
6968

7069

71-
@list_group.command(name="component-type", cls=DgClickCommand)
72-
@click.option(
73-
"--json",
74-
"output_json",
75-
is_flag=True,
76-
default=False,
77-
help="Output as JSON instead of a table.",
78-
)
79-
@dg_global_options
80-
@cli_telemetry_wrapper
81-
def list_component_type_command(output_json: bool, **global_options: object) -> None:
82-
"""List registered Dagster components in the current project environment."""
83-
cli_config = normalize_cli_config(global_options, click.get_current_context())
84-
dg_context = DgContext.for_defined_registry_environment(Path.cwd(), cli_config)
85-
registry = RemotePackageRegistry.from_dg_context(dg_context)
86-
70+
def _list_package_entries(
71+
registry: RemotePackageRegistry, output_json: bool, entry_type: Optional[str]
72+
) -> None:
8773
sorted_keys = sorted(
88-
(k for k in registry.keys() if isinstance(registry.get(k), ComponentTypeSnap)),
74+
[
75+
key
76+
for key in registry.keys()
77+
if entry_type is None or entry_type in registry.get(key).types
78+
],
8979
key=lambda k: k.to_typename(),
9080
)
91-
9281
# JSON
9382
if output_json:
9483
output: list[dict[str, object]] = []
@@ -101,18 +90,36 @@ def list_component_type_command(output_json: bool, **global_options: object) ->
10190
}
10291
)
10392
click.echo(json.dumps(output, indent=4))
104-
10593
# TABLE
10694
else:
10795
table = Table(border_style="dim")
108-
table.add_column("Component Type", style="bold cyan", no_wrap=True)
96+
table.add_column("Package Entry", style="bold cyan", no_wrap=True)
10997
table.add_column("Summary")
11098
for key in sorted_keys:
11199
table.add_row(key.to_typename(), registry.get(key).summary)
112100
console = Console()
113101
console.print(table)
114102

115103

104+
@list_group.command(name="component-type", cls=DgClickCommand)
105+
@click.option(
106+
"--json",
107+
"output_json",
108+
is_flag=True,
109+
default=False,
110+
help="Output as JSON instead of a table.",
111+
)
112+
@dg_global_options
113+
@cli_telemetry_wrapper
114+
def list_component_type_command(output_json: bool, **global_options: object) -> None:
115+
"""List registered Dagster components in the current project environment."""
116+
cli_config = normalize_cli_config(global_options, click.get_current_context())
117+
dg_context = DgContext.for_defined_registry_environment(Path.cwd(), cli_config)
118+
registry = RemotePackageRegistry.from_dg_context(dg_context)
119+
120+
_list_package_entries(registry, output_json, "component")
121+
122+
116123
# ########################
117124
# ##### DEFS
118125
# ########################

python_modules/libraries/dagster-dg/dagster_dg/cli/utils.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -91,36 +91,34 @@ def inspect_component_type_command(
9191
"Only one of --description, --scaffold-params-schema, and --component-schema can be specified."
9292
)
9393

94-
component_type_snap = registry.get_component_type(component_key)
95-
94+
entry_snap = registry.get(component_key)
9695
if description:
97-
if component_type_snap.description:
98-
click.echo(component_type_snap.description)
96+
if entry_snap.description:
97+
click.echo(entry_snap.description)
9998
else:
10099
click.echo("No description available.")
101100
elif scaffold_params_schema:
102-
if component_type_snap.scaffolder_schema:
103-
click.echo(_serialize_json_schema(component_type_snap.scaffolder_schema))
101+
if entry_snap.scaffolder_schema:
102+
click.echo(_serialize_json_schema(entry_snap.scaffolder_schema))
104103
else:
105104
click.echo("No scaffold params schema defined.")
106105
elif component_schema:
107-
if component_type_snap.schema:
108-
click.echo(_serialize_json_schema(component_type_snap.schema))
106+
if entry_snap.component_schema:
107+
click.echo(_serialize_json_schema(entry_snap.component_schema))
109108
else:
110109
click.echo("No component schema defined.")
111-
112110
# print all available metadata
113111
else:
114112
click.echo(component_type)
115-
if component_type_snap.description:
113+
if entry_snap.description:
116114
click.echo("\nDescription:\n")
117-
click.echo(component_type_snap.description)
118-
if component_type_snap.scaffolder_schema:
115+
click.echo(entry_snap.description)
116+
if entry_snap.scaffolder_schema:
119117
click.echo("\nScaffold params schema:\n")
120-
click.echo(_serialize_json_schema(component_type_snap.scaffolder_schema))
121-
if component_type_snap.schema:
118+
click.echo(_serialize_json_schema(entry_snap.scaffolder_schema))
119+
if entry_snap.component_schema:
122120
click.echo("\nComponent schema:\n")
123-
click.echo(_serialize_json_schema(component_type_snap.schema))
121+
click.echo(_serialize_json_schema(entry_snap.component_schema))
124122

125123

126124
def _serialize_json_schema(schema: Mapping[str, Any]) -> str:

python_modules/libraries/dagster-dg/dagster_dg/component.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import dagster_shared.check as check
77
from dagster_shared.serdes import deserialize_value, serialize_value
8-
from dagster_shared.serdes.objects import ComponentTypeSnap, PackageEntryKey, PackageEntrySnap
8+
from dagster_shared.serdes.objects import PackageEntryKey, PackageEntrySnap
99

1010
from dagster_dg.utils import is_valid_json
1111

@@ -48,13 +48,6 @@ def get(self, key: PackageEntryKey) -> PackageEntrySnap:
4848
"""Resolves a library object within the scope of a given component directory."""
4949
return self._objects[key]
5050

51-
def get_component_type(self, key: PackageEntryKey) -> ComponentTypeSnap:
52-
"""Resolves a component type within the scope of a given component directory."""
53-
obj = self.get(key)
54-
if not isinstance(obj, ComponentTypeSnap):
55-
raise ValueError(f"Expected component type, got {obj}")
56-
return obj
57-
5851
def has(self, key: PackageEntryKey) -> bool:
5952
return key in self._objects
6053

python_modules/libraries/dagster-dg/dagster_dg/docs.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
from typing import Any, Optional, TypedDict, Union
66

77
import yaml
8-
from dagster_shared.serdes.objects import PackageEntryKey
8+
from dagster_shared.serdes.objects import ComponentTypeData, PackageEntryKey
9+
from dagster_shared.serdes.objects.package_entry import PackageEntrySnap
910
from dagster_shared.yaml_utils import parse_yaml_with_source_positions
1011
from dagster_shared.yaml_utils.source_position import SourcePositionTree
1112

12-
from dagster_dg.component import ComponentTypeSnap, RemotePackageRegistry
13+
from dagster_dg.component import RemotePackageRegistry
1314

1415
REF_BASE = "#/$defs/"
1516
JSON_SCHEMA_EXTRA_REQUIRED_SCOPE_KEY = "dagster_required_scope"
@@ -175,11 +176,16 @@ def json_for_all_components(
175176
registry: RemotePackageRegistry,
176177
) -> list[ComponentTypeNamespaceJson]:
177178
"""Returns a list of JSON representations of all component types in the registry."""
178-
component_json = [
179-
(key.namespace.split(".")[0], json_for_component_type(key, library_obj))
180-
for key, library_obj in registry.items()
181-
if isinstance(library_obj, ComponentTypeSnap) and library_obj.schema is not None
182-
]
179+
component_json = []
180+
for key, entry in registry.items():
181+
component_type_data = entry.get_type_data("component")
182+
if component_type_data and component_type_data.schema:
183+
component_json.append(
184+
(
185+
key.namespace.split(".")[0],
186+
json_for_component_type(key, entry, component_type_data),
187+
)
188+
)
183189
return [
184190
ComponentTypeNamespaceJson(
185191
name=namespace,
@@ -190,15 +196,15 @@ def json_for_all_components(
190196

191197

192198
def json_for_component_type(
193-
key: PackageEntryKey, remote_component_type: ComponentTypeSnap
199+
key: PackageEntryKey, entry: PackageEntrySnap, component_type_data: ComponentTypeData
194200
) -> ComponentTypeJson:
195201
typename = key.to_typename()
196-
sample_yaml = generate_sample_yaml(typename, remote_component_type.schema or {})
202+
sample_yaml = generate_sample_yaml(typename, component_type_data.schema or {})
197203
return ComponentTypeJson(
198204
name=typename,
199205
author="",
200206
tags=[],
201207
example=sample_yaml,
202-
schema=json.dumps(remote_component_type.schema),
203-
description=remote_component_type.description,
208+
schema=json.dumps(component_type_data.schema),
209+
description=entry.description,
204210
)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from dagster_shared.serdes.objects.package_entry import (
2-
ComponentTypeSnap as ComponentTypeSnap,
2+
ComponentTypeData as ComponentTypeData,
33
PackageEntryKey as PackageEntryKey,
44
PackageEntrySnap as PackageEntrySnap,
5-
ScaffolderSnap as ScaffolderSnap,
5+
ScaffoldTargetTypeData as ScaffoldTargetTypeData,
66
)

0 commit comments

Comments
 (0)