uranusjr commented on code in PR #44639: URL: https://github.com/apache/airflow/pull/44639#discussion_r1879425317
########## airflow/utils/context.py: ########## @@ -161,54 +176,57 @@ class OutletEventAccessor: :meta private: """ - raw_key: str | Asset | AssetAlias + key: BaseAssetUniqueKey extra: dict[str, Any] = attrs.Factory(dict) asset_alias_events: list[AssetAliasEvent] = attrs.field(factory=list) - def add(self, asset: Asset | str, extra: dict[str, Any] | None = None) -> None: + def add(self, asset: Asset, extra: dict[str, Any] | None = None) -> None: """Add an AssetEvent to an existing Asset.""" - if isinstance(asset, str): - asset_uri = asset - elif isinstance(asset, Asset): - asset_uri = asset.uri - else: + if not isinstance(self.key, AssetAliasUniqueKey): return - if isinstance(self.raw_key, str): - asset_alias_name = self.raw_key - elif isinstance(self.raw_key, AssetAlias): - asset_alias_name = self.raw_key.name - else: - return - - event = AssetAliasEvent(asset_alias_name, asset_uri, extra=extra or {}) + asset_alias_name = self.key.name + event = AssetAliasEvent( + source_alias_name=asset_alias_name, + dest_asset_key=AssetUniqueKey.from_asset(asset), + extra=extra or {}, + ) self.asset_alias_events.append(event) -class OutletEventAccessors(Mapping[str, OutletEventAccessor]): +class OutletEventAccessors(Mapping[Union[Asset, AssetAlias], OutletEventAccessor]): """ Lazy mapping of outlet asset event accessors. :meta private: """ def __init__(self) -> None: - self._dict: dict[str, OutletEventAccessor] = {} + self._dict: dict[BaseAssetUniqueKey, OutletEventAccessor] = {} def __str__(self) -> str: return f"OutletEventAccessors(_dict={self._dict})" - def __iter__(self) -> Iterator[str]: - return iter(self._dict) + def __iter__(self) -> Iterator[Asset | AssetAlias]: + return ( + key.to_asset() if isinstance(key, AssetUniqueKey) else key.to_asset_alias() for key in self._dict + ) def __len__(self) -> int: return len(self._dict) - def __getitem__(self, key: str | Asset | AssetAlias) -> OutletEventAccessor: - event_key = extract_event_key(key) - if event_key not in self._dict: - self._dict[event_key] = OutletEventAccessor(extra={}, raw_key=key) - return self._dict[event_key] + def __getitem__(self, key: Asset | AssetAlias) -> OutletEventAccessor: + hashable_key: BaseAssetUniqueKey + if isinstance(key, Asset): + hashable_key = AssetUniqueKey.from_asset(key) + elif isinstance(key, AssetAlias): + hashable_key = AssetAliasUniqueKey.from_asset_alias(key) + else: + raise KeyError("Key should be either an asset or an asset alias") Review Comment: I think Python containers generally raise TypeError for this. ########## airflow/utils/context.py: ########## @@ -161,54 +176,57 @@ class OutletEventAccessor: :meta private: """ - raw_key: str | Asset | AssetAlias + key: BaseAssetUniqueKey extra: dict[str, Any] = attrs.Factory(dict) asset_alias_events: list[AssetAliasEvent] = attrs.field(factory=list) - def add(self, asset: Asset | str, extra: dict[str, Any] | None = None) -> None: + def add(self, asset: Asset, extra: dict[str, Any] | None = None) -> None: """Add an AssetEvent to an existing Asset.""" - if isinstance(asset, str): - asset_uri = asset - elif isinstance(asset, Asset): - asset_uri = asset.uri - else: + if not isinstance(self.key, AssetAliasUniqueKey): return - if isinstance(self.raw_key, str): - asset_alias_name = self.raw_key - elif isinstance(self.raw_key, AssetAlias): - asset_alias_name = self.raw_key.name - else: - return - - event = AssetAliasEvent(asset_alias_name, asset_uri, extra=extra or {}) + asset_alias_name = self.key.name + event = AssetAliasEvent( + source_alias_name=asset_alias_name, + dest_asset_key=AssetUniqueKey.from_asset(asset), + extra=extra or {}, + ) self.asset_alias_events.append(event) -class OutletEventAccessors(Mapping[str, OutletEventAccessor]): +class OutletEventAccessors(Mapping[Union[Asset, AssetAlias], OutletEventAccessor]): """ Lazy mapping of outlet asset event accessors. :meta private: """ def __init__(self) -> None: - self._dict: dict[str, OutletEventAccessor] = {} + self._dict: dict[BaseAssetUniqueKey, OutletEventAccessor] = {} def __str__(self) -> str: return f"OutletEventAccessors(_dict={self._dict})" - def __iter__(self) -> Iterator[str]: - return iter(self._dict) + def __iter__(self) -> Iterator[Asset | AssetAlias]: + return ( + key.to_asset() if isinstance(key, AssetUniqueKey) else key.to_asset_alias() for key in self._dict + ) def __len__(self) -> int: return len(self._dict) - def __getitem__(self, key: str | Asset | AssetAlias) -> OutletEventAccessor: - event_key = extract_event_key(key) - if event_key not in self._dict: - self._dict[event_key] = OutletEventAccessor(extra={}, raw_key=key) - return self._dict[event_key] + def __getitem__(self, key: Asset | AssetAlias) -> OutletEventAccessor: + hashable_key: BaseAssetUniqueKey + if isinstance(key, Asset): + hashable_key = AssetUniqueKey.from_asset(key) + elif isinstance(key, AssetAlias): + hashable_key = AssetAliasUniqueKey.from_asset_alias(key) + else: + raise KeyError("Key should be either an asset or an asset alias") Review Comment: I think Python containers generally raise TypeError for this. ```pycon >>> []['a'] <python-input-0>:1: SyntaxWarning: list indices must be integers or slices, not str; perhaps you missed a comma? []['a'] Traceback (most recent call last): File "<python-input-0>", line 1, in <module> []['a'] ~~^^^^^ TypeError: list indices must be integers or slices, not str ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@airflow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org