amoghrajesh commented on code in PR #60776:
URL: https://github.com/apache/airflow/pull/60776#discussion_r2719712214
##########
providers/standard/src/airflow/providers/standard/operators/trigger_dagrun.py:
##########
@@ -89,8 +88,17 @@ def get_link(self, operator: BaseOperator, *, ti_key:
TaskInstanceKey) -> str:
trigger_dag_id = operator.trigger_dag_id
if not AIRFLOW_V_3_0_PLUS:
from airflow.models.renderedtifields import
RenderedTaskInstanceFields
+ from airflow.models.taskinstancekey import TaskInstanceKey as
CoreTaskInstanceKey
+
+ core_ti_key = CoreTaskInstanceKey(
+ dag_id=ti_key.dag_id,
+ task_id=ti_key.task_id,
+ run_id=ti_key.run_id,
+ try_number=ti_key.try_number,
+ map_index=ti_key.map_index,
+ )
Review Comment:
I looked it up a bit and mypy by default seems to do nominal typing unless
there is a protocol in play, hence with a simple example:
```python
from typing import NamedTuple
class SDKKey(NamedTuple):
dag_id: str
task_id: str
class CoreKey(NamedTuple):
dag_id: str
task_id: str
def core_function(key: CoreKey) -> str:
return f"{key.dag_id}/{key.task_id}"
sdk_key = SDKKey("my_dag", "my_task")
core_key = CoreKey("my_dag", "my_task")
core_function(sdk_key)
core_function(core_key)
```
```shell
(apache-airflow) ➜ airflow git:(ti-key-to-sdk) ✗ mypy minimal.py
minimal.py:21: error: Argument 1 to "core_function" has incompatible type
"SDKKey"; expected
"CoreKey" [arg-type]
core_function(sdk_key)
^~~~~~~
Found 1 error in 1 file (checked 1 source file)
```
But on the other hand if I do this:
```python
from typing import NamedTuple, Protocol
class KeyProtocol(Protocol):
@property
def dag_id(self) -> str: ...
@property
def task_id(self) -> str: ...
class SDKKey(NamedTuple):
dag_id: str
task_id: str
class CoreKey(NamedTuple):
dag_id: str
task_id: str
def protocol_function(key: KeyProtocol) -> str:
return f"{key.dag_id}/{key.task_id}"
def nominal_function(key: CoreKey) -> str:
return f"{key.dag_id}/{key.task_id}"
sdk_key = SDKKey("my_dag", "my_task")
core_key = CoreKey("my_dag", "my_task")
protocol_function(sdk_key)
protocol_function(core_key)
nominal_function(sdk_key)
nominal_function(core_key)
```
```shell
(apache-airflow) ➜ airflow git:(ti-key-to-sdk) ✗ mypy minimal.py
minimal.py:36: error: Argument 1 to "nominal_function" has incompatible type
"SDKKey";
expected "CoreKey" [arg-type]
nominal_function(sdk_key)
^~~~~~~
Found 1 error in 1 file (checked 1 source file)
```
--
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]