jason810496 opened a new pull request, #77759:
URL: https://github.com/apache/airflow/pull/77759

   - related: 
https://github.com/apache/airflow/pull/77582#discussion_r4706885169
   
   ## Why
   
   Airflow never records an outlet event for an asset or alias the task did not 
declare, and `add()` on an asset accessor does nothing, so these mistakes only 
show up when downstream Dags never run.
   
   ## What
   
   `outlet_events` now raises an error for an asset or alias that is not in the 
task's `outlets`, instead of dropping the event. 
   
   ### Dynamic asset events through `AssetAlias` still work
   
   Both ways of emitting an event for an asset only known at runtime behave as 
before:
   
   ```python
   @task(outlets=[AssetAlias("dynamic-outputs")])
   def emit_with_context(partition_date: str, *, outlet_events):
       asset = Asset(f"s3://my-bucket/data/{partition_date}.parquet")
       outlet_events[AssetAlias("dynamic-outputs")].add(asset, 
extra={"partition_date": partition_date})
   
   
   @task(outlets=[AssetAlias("dynamic-outputs")])
   def emit_with_metadata(bucket_name: str):
       yield Metadata(
           asset=Asset(f"s3://{bucket_name}/latest.json"),
           extra={"source": "runtime"},
           alias=AssetAlias("dynamic-outputs"),
       )
   ```
   
   The concrete asset does not need to be in `outlets`. The restrictions:
   
   - The alias must be in `outlets`, otherwise `outlet_events[alias]` or the 
`yield` raises `UndeclaredOutletError`. Airflow dropped these events before.
   - Call `add()` on the alias accessor. Calling it on an asset accessor raises 
`TypeError`.
   - `Metadata` without `alias` still needs its asset in `outlets`.
   
   ### Undeclared asset or alias
   
   ```python
   @task(outlets=[Asset("declared"), AssetAlias("declared-alias")])
   def produce(*, outlet_events):
       outlet_events[Asset("other")].extra = {"rows": 1}
       outlet_events[AssetAlias("other-alias")].add(Asset("x"))
   ```
   
   - Before: both events are dropped and the task succeeds.
   - After: the first line raises `UndeclaredOutletError`.
   
   ### `add()` on an asset accessor
   
   ```python
   @task(outlets=[Asset("declared")])
   def produce(*, outlet_events):
       outlet_events[Asset("declared")].add(Asset("x"))
   ```
   
   - Before: does nothing.
   - After: raises `TypeError`. `add()` only works on an asset alias accessor.
   
   ### Yielded `Metadata`
   
   ```python
   @task(outlets=[Asset("declared"), AssetAlias("my-alias")])
   def produce():
       yield Metadata(Asset("other"), extra={"rows": 1})
       yield Metadata(Asset("declared"), alias=AssetAlias("other-alias"))
   ```
   
   - Before: both events are dropped and the task succeeds.
   - After: the first `yield` raises `UndeclaredOutletError`.
   
   ### Asset ref outlet with a different asset
   
   ```python
   @task(outlets=[Asset.ref(name="a")])  # "a" is stored with uri 
"s3://bucket/a"
   def produce(*, outlet_events):
       outlet_events[Asset(name="a", uri="s3://bucket/wrong")].extra = {"rows": 
1}
   ```
   
   - Before: Airflow records an event for `a` without the extra.
   - After: raises `ConflictingOutletError`: `Asset(name='a', 
uri='s3://bucket/wrong') is not an outlet of this task but has the same name as 
outlet Asset(name='a', uri='s3://bucket/a').`
   
   ### `get()` and `in`
   
   ```python
   outlet_events.get(Asset("other"))
   Asset("other") in outlet_events
   ```
   
   - Before: an empty accessor and `True`.
   - After: `None` and `False`.
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   - [x] Yes, with help of Claude Code (Opus 5.5) following [the 
guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
   


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to