forked from dagster-io/dagster
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract InOutMapper to begin refactoring AssetsDefinition constructio…
…n process (dagster-io#22222) ## Summary & Motivation `multi_asset` is a beast, as well as the entire `AssetsDefinition` creation machinery. This class introduces a class `InOutMapper` (renamed to `AssetsDefinitionFactory` upstack once its mandate increased) that begins to tease apart `multi_asset` which is nearly 300 LoC, has 37 local variables, and a huge dynamically scoped inner function. To see where this is going I have created a digest [PR](dagster-io#22238) that demonstrate the before after. This also sets us up to consolidate the `AssetsDefinition` creation code paths, as it contains tons of duplicated code strewn about. Instead we will be able to invoke the decomposed code in the new factory functions. This was motivated by the discussion in dagster-io#22221 that suggested we move a propose class to be within the inheritance hierarchy of `AssetsDefinition`. The complexity of logic surrounding the construction of said object is completely out of control, and I found it effectively intractable to do an inheritance scheme cleanly. ## How I Tested These Changes BK
- Loading branch information
1 parent
bfad7c4
commit e98ae1e
Showing
2 changed files
with
64 additions
and
11 deletions.
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
52 changes: 52 additions & 0 deletions
52
python_modules/dagster/dagster/_core/definitions/decorators/assets_definition_factory.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,52 @@ | ||
from functools import cached_property | ||
from typing import Mapping, NamedTuple, Tuple | ||
|
||
from dagster._core.definitions.asset_key import AssetKey | ||
from dagster._core.definitions.input import In | ||
from dagster._core.definitions.output import Out | ||
|
||
|
||
class InMapping(NamedTuple): | ||
input_name: str | ||
input: In | ||
|
||
|
||
class OutMapping(NamedTuple): | ||
output_name: str | ||
output: Out | ||
|
||
|
||
class InOutMapper: | ||
def __init__( | ||
self, | ||
in_mappings: Mapping[AssetKey, InMapping], | ||
out_mappings: Mapping[AssetKey, OutMapping], | ||
) -> None: | ||
self.in_mappings = in_mappings | ||
self.out_mappings = out_mappings | ||
|
||
@staticmethod | ||
def from_asset_ins_and_asset_outs( | ||
asset_ins: Mapping[AssetKey, Tuple[str, In]], | ||
asset_outs: Mapping[AssetKey, Tuple[str, Out]], | ||
): | ||
in_mappings = { | ||
asset_key: InMapping(input_name, in_) | ||
for asset_key, (input_name, in_) in asset_ins.items() | ||
} | ||
out_mappings = { | ||
asset_key: OutMapping(output_name, out_) | ||
for asset_key, (output_name, out_) in asset_outs.items() | ||
} | ||
return InOutMapper(in_mappings, out_mappings) | ||
|
||
@cached_property | ||
def asset_outs_by_output_name(self) -> Mapping[str, Out]: | ||
return dict(self.out_mappings.values()) | ||
|
||
@cached_property | ||
def keys_by_output_name(self) -> Mapping[str, AssetKey]: | ||
return { | ||
out_mapping.output_name: asset_key | ||
for asset_key, out_mapping in self.out_mappings.items() | ||
} |