Skip to content

Commit

Permalink
Add docstring detailing factory use case for ConfigurableResource (#1…
Browse files Browse the repository at this point in the history
…7613)

## Summary & Motivation

Per #17551 adding docblock description of using `ConfigurableResource` to produce different object type at runtime.

## How I Tested These Changes

![Screenshot 2023-11-01 at 4 52 02 PM](https://github.com/dagster-io/dagster/assets/28738937/06d1b84e-00cc-42b3-8c87-daaeae89b758)

Read
  • Loading branch information
schrockn authored Nov 1, 2023
1 parent be07285 commit cc09249
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/content/api/modules.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/content/api/searchindex.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/content/api/sections.json

Large diffs are not rendered by default.

Binary file modified docs/next/public/objects.inv
Binary file not shown.
28 changes: 28 additions & 0 deletions python_modules/dagster/dagster/_config/pythonic_config/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,34 @@ def asset_that_uses_writer(writer: WriterResource):
resources={"writer": WriterResource(prefix="a_prefix")},
)
You can optionally use this class to model configuration only and vend an object
of a different type for use at runtime. This is useful for those who wish to
have a separate object that manages configuration and a separate object at runtime. Or
where you want to directly use a third-party class that you do not control.
To do this you override the `create_resource` methods to return a different object.
.. code-block:: python
class WriterResource(ConfigurableResource):
str: prefix
def create_resource(self, context: InitResourceContext) -> Writer:
# Writer is pre-existing class defined else
return Writer(self.prefix)
Example usage:
.. code-block:: python
@asset
def use_preexisting_writer_as_resource(writer: ResourceParam[Writer]):
writer.output("text")
defs = Definitions(
assets=[use_preexisting_writer_as_resource],
resources={"writer": WriterResource(prefix="a_prefix")},
)
"""

def create_resource(self, context: InitResourceContext) -> TResValue:
Expand Down

1 comment on commit cc09249

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for dagster-docs ready!

✅ Preview
https://dagster-docs-460naodya-elementl.vercel.app
https://master.dagster.dagster-docs.io

Built with commit cc09249.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.