You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "leoperegrino (via GitHub)" <gi...@apache.org> on 2025/02/12 21:17:11 UTC
[I] Bug: StrEnum values in task function arguments get converted to strings, breaking method access [airflow]
leoperegrino opened a new issue, #46701:
URL: https://github.com/apache/airflow/issues/46701
### Apache Airflow version
Other Airflow 2 version (please specify below)
### If "Other Airflow 2 version" selected, which one?
apache/airflow:2.8.1-python3.11
### What happened?
I'm running `apache/airflow:2.8.1-python3.11` container and working with `Enum` and `StrEnum` to help me with the type system. The main difference between these two enum types is that `StrEnum` when cast to string (`f'{MyStrEnum.A}'` or `str(MyStrEnum.A)`) will auto use the `.value` attribute.
When passing a `StrEnum` to a `tasks` decorated function, `StrEnum` arguments will be cast to string. Calls to `StrEnum` methods then raise `AttributeError`. Normal enums work as usual.
### What you think should happen instead?
The StrEnum argument should maintain its enum type when passed to the task, just like the regular Enum does. The task function signature def test_str_enum(str_enum: MyStrEnum) indicates that the parameter should be a MyStrEnum instance, not a string.
While StrEnum is designed to automatically convert to string during string operations (like str() or f-strings), this auto-conversion should not happen during parameter passing.
### How to reproduce
```python
from enum import Enum
from enum import StrEnum
from airflow.decorators import dag
from airflow.decorators import task
class MyEnum(Enum):
A = 'A'
class MyStrEnum(StrEnum):
A = 'A'
@dag(schedule_interval=None)
def enum_dag():
@task(task_id='test_enum')
def test_enum(enum: MyEnum):
return enum.name
@task(task_id='test_str_enum')
def test_str_enum(str_enum: MyStrEnum):
return str_enum.name
test_enum(enum=MyEnum.A)
test_str_enum(str_enum=MyStrEnum.A)
enum_dag()
```
### Operating System
Amazon Linux 2023
### Versions of Apache Airflow Providers
_No response_
### Deployment
Docker-Compose
### Deployment details
_No response_
### Anything else?
`test_enum`
```log
f063d55d4244
*** Found local files:
*** * /opt/airflow/logs/dag_id=enum_dag/run_id=manual__2025-02-11T16:58:48.370587+00:00/task_id=test_enum/attempt=1.log
[2025-02-11, 16:58:50 UTC] {taskinstance.py:1956} INFO - Dependencies all met for dep_context=non-requeueable deps ti=<TaskInstance: enum_dag.test_enum manual__2025-02-11T16:58:48.370587+00:00 [queued]>
[2025-02-11, 16:58:50 UTC] {taskinstance.py:1956} INFO - Dependencies all met for dep_context=requeueable deps ti=<TaskInstance: enum_dag.test_enum manual__2025-02-11T16:58:48.370587+00:00 [queued]>
[2025-02-11, 16:58:50 UTC] {taskinstance.py:2170} INFO - Starting attempt 1 of 1
[2025-02-11, 16:58:50 UTC] {taskinstance.py:2191} INFO - Executing <Task(_PythonDecoratedOperator): test_enum> on 2025-02-11 16:58:48.370587+00:00
[2025-02-11, 16:58:50 UTC] {standard_task_runner.py:60} INFO - Started process 2879665 to run task
[2025-02-11, 16:58:50 UTC] {standard_task_runner.py:87} INFO - Running: ['***', 'tasks', 'run', 'enum_dag', 'test_enum', 'manual__2025-02-11T16:58:48.370587+00:00', '--job-id', '6650', '--raw', '--subdir', 'DAGS_FOLDER/enum_dag.py', '--cfg-path', '/tmp/tmppaapxvvf']
[2025-02-11, 16:58:50 UTC] {standard_task_runner.py:88} INFO - Job 6650: Subtask test_enum
[2025-02-11, 16:58:50 UTC] {task_command.py:423} INFO - Running <TaskInstance: enum_dag.test_enum manual__2025-02-11T16:58:48.370587+00:00 [running]> on host f063d55d4244
[2025-02-11, 16:58:50 UTC] {taskinstance.py:2480} INFO - Exporting env vars: AIRFLOW_CTX_DAG_OWNER='***' AIRFLOW_CTX_DAG_ID='enum_dag' AIRFLOW_CTX_TASK_ID='test_enum' AIRFLOW_CTX_EXECUTION_DATE='2025-02-11T16:58:48.370587+00:00' AIRFLOW_CTX_TRY_NUMBER='1' AIRFLOW_CTX_DAG_RUN_ID='manual__2025-02-11T16:58:48.370587+00:00'
[2025-02-11, 16:58:50 UTC] {python.py:201} INFO - Done. Returned value was: A
[2025-02-11, 16:58:50 UTC] {taskinstance.py:1138} INFO - Marking task as SUCCESS. dag_id=enum_dag, task_id=test_enum, execution_date=20250211T165848, start_date=20250211T165850, end_date=20250211T165850
[2025-02-11, 16:58:50 UTC] {local_task_job_runner.py:234} INFO - Task exited with return code 0
[2025-02-11, 16:58:50 UTC] {taskinstance.py:3280} INFO - 0 downstream tasks scheduled from follow-on schedule check
```
`test_str_enum`
```log
f063d55d4244
*** Found local files:
*** * /opt/airflow/logs/dag_id=enum_dag/run_id=manual__2025-02-11T16:58:48.370587+00:00/task_id=test_str_enum/attempt=1.log
[2025-02-11, 16:58:50 UTC] {taskinstance.py:1956} INFO - Dependencies all met for dep_context=non-requeueable deps ti=<TaskInstance: enum_dag.test_str_enum manual__2025-02-11T16:58:48.370587+00:00 [queued]>
[2025-02-11, 16:58:50 UTC] {taskinstance.py:1956} INFO - Dependencies all met for dep_context=requeueable deps ti=<TaskInstance: enum_dag.test_str_enum manual__2025-02-11T16:58:48.370587+00:00 [queued]>
[2025-02-11, 16:58:50 UTC] {taskinstance.py:2170} INFO - Starting attempt 1 of 1
[2025-02-11, 16:58:50 UTC] {taskinstance.py:2191} INFO - Executing <Task(_PythonDecoratedOperator): test_str_enum> on 2025-02-11 16:58:48.370587+00:00
[2025-02-11, 16:58:50 UTC] {standard_task_runner.py:60} INFO - Started process 2879664 to run task
[2025-02-11, 16:58:50 UTC] {standard_task_runner.py:87} INFO - Running: ['***', 'tasks', 'run', 'enum_dag', 'test_str_enum', 'manual__2025-02-11T16:58:48.370587+00:00', '--job-id', '6649', '--raw', '--subdir', 'DAGS_FOLDER/enum_dag.py', '--cfg-path', '/tmp/tmp5lou4idb']
[2025-02-11, 16:58:50 UTC] {standard_task_runner.py:88} INFO - Job 6649: Subtask test_str_enum
[2025-02-11, 16:58:50 UTC] {task_command.py:423} INFO - Running <TaskInstance: enum_dag.test_str_enum manual__2025-02-11T16:58:48.370587+00:00 [running]> on host f063d55d4244
[2025-02-11, 16:58:50 UTC] {taskinstance.py:2480} INFO - Exporting env vars: AIRFLOW_CTX_DAG_OWNER='***' AIRFLOW_CTX_DAG_ID='enum_dag' AIRFLOW_CTX_TASK_ID='test_str_enum' AIRFLOW_CTX_EXECUTION_DATE='2025-02-11T16:58:48.370587+00:00' AIRFLOW_CTX_TRY_NUMBER='1' AIRFLOW_CTX_DAG_RUN_ID='manual__2025-02-11T16:58:48.370587+00:00'
[2025-02-11, 16:58:50 UTC] {taskinstance.py:2698} ERROR - Task failed with exception
Traceback (most recent call last):
File "/home/airflow/.local/lib/python3.11/site-packages/airflow/models/taskinstance.py", line 433, in _execute_task
result = execute_callable(context=context, **execute_callable_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/airflow/.local/lib/python3.11/site-packages/airflow/decorators/base.py", line 241, in execute
return_value = super().execute(context)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/airflow/.local/lib/python3.11/site-packages/airflow/operators/python.py", line 199, in execute
return_value = self.execute_callable()
^^^^^^^^^^^^^^^^^^^^^^^
File "/home/airflow/.local/lib/python3.11/site-packages/airflow/operators/python.py", line 216, in execute_callable
return self.python_callable(*self.op_args, **self.op_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/airflow/dags/enum_dag.py", line 27, in test_str_enum
return str_enum.name
^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'name'
[2025-02-11, 16:58:50 UTC] {taskinstance.py:1138} INFO - Marking task as FAILED. dag_id=enum_dag, task_id=test_str_enum, execution_date=20250211T165848, start_date=20250211T165850, end_date=20250211T165850
[2025-02-11, 16:58:50 UTC] {standard_task_runner.py:107} ERROR - Failed to execute job 6649 for task test_str_enum ('str' object has no attribute 'name'; 2879664)
[2025-02-11, 16:58:50 UTC] {local_task_job_runner.py:234} INFO - Task exited with return code 1
[2025-02-11, 16:58:50 UTC] {taskinstance.py:3280} INFO - 0 downstream tasks scheduled from follow-on schedule check
```
### Are you willing to submit PR?
- [ ] Yes I am willing to submit a PR!
### Code of Conduct
- [x] I agree to follow this project's [Code of Conduct](https://github.com/apache/airflow/blob/main/CODE_OF_CONDUCT.md)
--
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-unsubscribe@airflow.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] Bug: StrEnum values in task function arguments get converted to strings, breaking method access [airflow]
Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk commented on issue #46701:
URL: https://github.com/apache/airflow/issues/46701#issuecomment-2660953788
StrEnum is not supported for Python < 3.11. I doubt we want to add conditional code for Python 3.9 and 3.10 to handle StrEnums and Airflow is 3.9+ currently. I will convert it to a discussion - if more discussion is needed.
--
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-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] Bug: StrEnum values in task function arguments get converted to strings, breaking method access [airflow]
Posted by "boring-cyborg[bot] (via GitHub)" <gi...@apache.org>.
boring-cyborg[bot] commented on issue #46701:
URL: https://github.com/apache/airflow/issues/46701#issuecomment-2654863206
Thanks for opening your first issue here! Be sure to follow the issue template! If you are willing to raise PR to address this issue please do so, no need to wait for approval.
--
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-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] Bug: StrEnum values in task function arguments get converted to strings, breaking method access [airflow]
Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk closed issue #46701: Bug: StrEnum values in task function arguments get converted to strings, breaking method access
URL: https://github.com/apache/airflow/issues/46701
--
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-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org