-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Prototyping secrets requirements and demonstrating loading behavior #26783
Draft
schrockn
wants to merge
5
commits into
master
Choose a base branch
from
schrockn/test-env-requirement
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.
+206
−2
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 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 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 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
Empty file.
Empty file.
32 changes: 32 additions & 0 deletions
32
...es/dagster-components/dagster_components_tests/scope_tests/footran_component/component.py
This file contains 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,32 @@ | ||
from typing import AbstractSet | ||
|
||
from dagster import Definitions, asset | ||
from dagster_components import Component, ComponentLoadContext, component_type | ||
from pydantic import BaseModel | ||
|
||
|
||
class FootranParams(BaseModel): | ||
target_address: str | ||
|
||
|
||
@component_type(name="footran") | ||
class FootranComponent(Component): | ||
def __init__(self, target_address: str): | ||
self.target_address = target_address | ||
|
||
params_schema = FootranParams | ||
|
||
@classmethod | ||
def load(cls, context: ComponentLoadContext): | ||
loaded_params = context.load_params(cls.params_schema) | ||
return cls(target_address=loaded_params.target_address) | ||
|
||
def build_defs(self, context: ComponentLoadContext): | ||
@asset | ||
def an_asset() -> None: ... | ||
|
||
return Definitions() | ||
|
||
def required_env_vars(self, context: ComponentLoadContext) -> AbstractSet[str]: | ||
requires = context.component_file_model.requires | ||
return set(requires.env) if requires and requires.env else set() |
9 changes: 9 additions & 0 deletions
9
.../dagster-components/dagster_components_tests/scope_tests/footran_component/component.yaml
This file contains 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,9 @@ | ||
type: .footran | ||
|
||
requires: | ||
env: | ||
- FOOTRAN_API_KEY | ||
- FOOTRAN_API_SECRET | ||
|
||
params: | ||
target_address: some_address |
111 changes: 111 additions & 0 deletions
111
...ies/dagster-components/dagster_components_tests/scope_tests/test_basic_env_requirement.py
This file contains 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,111 @@ | ||
from pathlib import Path | ||
from typing import List, Tuple, cast | ||
|
||
from dagster_components.core.component import ComponentTypeRegistry | ||
from dagster_components.core.component_defs_builder import ( | ||
build_components_from_component_path, | ||
loading_context_for_component_path, | ||
) | ||
|
||
from dagster_components_tests.scope_tests.footran_component.component import FootranComponent | ||
|
||
|
||
def test_custom_scope() -> None: | ||
components = build_components_from_component_path( | ||
path=Path(__file__).parent / "footran_component", | ||
registry=ComponentTypeRegistry.empty(), | ||
resources={}, | ||
) | ||
|
||
assert len(components) == 1 | ||
component = components[0] | ||
|
||
# This fails due to lack of proper module caching with the right package name | ||
# assert isinstance(component, FootranComponent) | ||
assert type(component).__name__ == "FootranComponent" | ||
component = cast(FootranComponent, component) | ||
|
||
assert component.target_address == "some_address" | ||
load_context = loading_context_for_component_path( | ||
path=Path(__file__).parent / "footran_component", | ||
registry=ComponentTypeRegistry.empty(), | ||
resources={}, | ||
) | ||
assert component.required_env_vars(load_context) == {"FOOTRAN_API_KEY", "FOOTRAN_API_SECRET"} | ||
|
||
|
||
import os | ||
|
||
|
||
def find_package_root(file_path: str) -> Tuple[str, List[str]]: | ||
"""Starting from the directory containing 'file_path', walk upward | ||
until we find a directory that is NOT a Python package | ||
(i.e., no __init__.py). | ||
|
||
Returns the absolute path of the 'package root' directory (which | ||
itself may have an __init__.py if it's still part of a package), | ||
plus the list of path components under that root. | ||
|
||
Example: | ||
If file_path = "/home/user/project/my_pkg/sub_pkg/module.py" | ||
and /home/user/project/my_pkg/__init__.py exists, | ||
and /home/user/project/__init__.py does NOT exist, | ||
then the package root is "/home/user/project" | ||
and the sub-path is ["my_pkg", "sub_pkg", "module.py"]. | ||
""" | ||
file_path = os.path.abspath(file_path) | ||
if not os.path.isfile(file_path): | ||
raise ValueError(f"Path {file_path} is not a file.") | ||
|
||
dir_path = os.path.dirname(file_path) | ||
filename = os.path.basename(file_path) | ||
|
||
# We will accumulate path components in reverse, then reverse them at the end | ||
components = [Path(filename).stem] | ||
|
||
# Traverse upward while the directory contains an __init__.py, | ||
# meaning it is part of a package. | ||
while True: | ||
init_py = os.path.join(dir_path, "__init__.py") | ||
parent_dir = os.path.dirname(dir_path) | ||
|
||
if not os.path.isfile(init_py): | ||
# The current dir_path is not a python package directory | ||
# so break: we've found the package "root" (which might | ||
# itself still have an __init__.py, or might be outside any package). | ||
break | ||
|
||
# If we do have an __init__.py, then the directory is part of the package, | ||
# so add that directory name to our components and go one level up. | ||
components.append(os.path.basename(dir_path)) | ||
|
||
# Move upward in the filesystem | ||
dir_path = parent_dir | ||
|
||
# If we reach the filesystem root, stop | ||
if dir_path == "/" or not dir_path: | ||
break | ||
|
||
components.reverse() # Now it's from top-level package -> ... -> filename | ||
return dir_path, components | ||
|
||
|
||
def package_name(file_path: str) -> str: | ||
_, components = find_package_root(file_path) | ||
return ".".join(components) | ||
|
||
|
||
def test_get_full_module_name() -> None: | ||
comp_path = Path(__file__).parent / "footran_component" / "component.py" | ||
assert comp_path.exists() | ||
|
||
dir_path, components = find_package_root(str(comp_path)) | ||
|
||
# import code | ||
|
||
# code.interact(local=locals()) | ||
|
||
assert ( | ||
package_name(str(comp_path)) | ||
== "dagster_components_tests.scope_tests.footran_component.component" | ||
) |
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.
@smackesey i want to discuss this at tmrw's standup