Skip to content

Commit a44bc93

Browse files
committed
[dg] Fix lib/__init__.py addition for component type scaffolding (BUILD-925) (#29097)
## Summary & Motivation Fix line added to `<project>/lib/__init__.py`. Formerly: ``` # extra line from my_project.lib.shell_command import ShellCommand ``` Now: ``` from my_project.lib.shell_command import ShellCommand as ShellCommand ``` ## How I Tested These Changes Modified a test. ## Changelog Fix formatting of line added to `project/lib/__init__.py` when scaffolding a component type.
1 parent 705bfd1 commit a44bc93

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,13 @@ def scaffold_component_type(
246246
model=model,
247247
)
248248

249-
with open(root_path / "__init__.py", "a") as f:
250-
f.write(
251-
f"from {dg_context.default_component_library_module_name}.{module_name} import {class_name}\n"
252-
)
249+
with open(root_path / "__init__.py") as f:
250+
lines = f.readlines()
251+
lines.append(
252+
f"from {dg_context.default_component_library_module_name}.{module_name} import {class_name} as {class_name}\n"
253+
)
254+
with open(root_path / "__init__.py", "w") as f:
255+
f.writelines(lines)
253256

254257
click.echo(f"Scaffolded files for Dagster component type at {root_path}/{module_name}.py.")
255258

python_modules/libraries/dagster-dg/dagster_dg_tests/cli_tests/test_scaffold_commands.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22
import shutil
33
import subprocess
4+
import textwrap
45
from pathlib import Path
56
from typing import Literal, get_args
67

@@ -690,6 +691,9 @@ def test_scaffold_component_type_success() -> None:
690691
dg_context = DgContext.from_file_discovery_and_command_line_config(Path.cwd(), {})
691692
registry = RemotePluginRegistry.from_dg_context(dg_context)
692693
assert registry.has(PluginObjectKey(name="Baz", namespace="foo_bar.lib"))
694+
assert Path("src/foo_bar/lib/__init__.py").read_text() == textwrap.dedent("""
695+
from foo_bar.lib.baz import Baz as Baz
696+
""")
693697

694698

695699
def test_scaffold_component_type_already_exists_fails() -> None:

python_modules/libraries/dagster-dg/dagster_dg_tests/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,9 @@ def isolated_example_component_library_foo_bar(
295295
("project", "entry-points", "dagster_dg.library", "foo_bar"),
296296
lib_module_name,
297297
)
298-
Path("src", *lib_module_name.split(".")).mkdir(exist_ok=True)
298+
lib_dir = Path("src", *lib_module_name.split("."))
299+
lib_dir.mkdir(exist_ok=True)
300+
(lib_dir / "__init__.py").touch()
299301

300302
# Install the component library into our venv
301303
if not skip_venv:

0 commit comments

Comments
 (0)