Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PythonOperator DAG error when DAG has hyphen in name #42902

Merged

Conversation

jason810496
Copy link
Contributor

closes: #42796

After reproducing the issue, I found that the generated DAG file looks like this:

import types

unusual_prefix_3f65a6a886883f41209c142108053b32fdfbaf99_my-dag  = types.ModuleType("unusual_prefix_3f65a6a886883f41209c142108053b32fdfbaf99_my-dag")

unusual_prefix_3f65a6a886883f41209c142108053b32fdfbaf99_my-dag.callable_virtualenv = callable_virtualenv

sys.modules["unusual_prefix_3f65a6a886883f41209c142108053b32fdfbaf99_my-dag"] = unusual_prefix_3f65a6a886883f41209c142108053b32fdfbaf99_my-dag

# ...

Replace the hyphen with dash in original module name to solve this issue.

Copy link

boring-cyborg bot commented Oct 10, 2024

Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contributors' Guide (https:/apache/airflow/blob/main/contributing-docs/README.rst)
Here are some useful points:

  • Pay attention to the quality of your code (ruff, mypy and type annotations). Our pre-commits will help you with that.
  • In case of a new feature add useful documentation (in docstrings or in docs/ directory). Adding a new operator? Check this short guide Consider adding an example DAG that shows how users should use it.
  • Consider using Breeze environment for testing locally, it's a heavy docker but it ships with a working Airflow and a lot of integrations.
  • Be patient and persistent. It might take some time to get a review or get the final approval from Committers.
  • Please follow ASF Code of Conduct for all communication including (but not limited to) comments on Pull Requests, Mailing list and Slack.
  • Be sure to read the Airflow Coding style.
  • Always keep your Pull Requests rebased, otherwise your build might fail due to changes not related to your commits.
    Apache Airflow is a community-driven project and together we are making it better 🚀.
    In case of doubts contact the developers at:
    Mailing List: [email protected]
    Slack: https://s.apache.org/airflow-slack

airflow/utils/file.py Outdated Show resolved Hide resolved
@josix
Copy link
Contributor

josix commented Oct 11, 2024

question: I'm wondering if only hyphen character could be causing this issue. Do we have a more general way to prevent this bug from happening?

@jason810496
Copy link
Contributor Author

jason810496 commented Oct 11, 2024

If we directly hash the original module name, it will definitely work.
However, I think it's better to convert characters that could be interpreted as Python operators (like . or -) into underscores.The original module name is derived from the DAG file name by using rfind to remove the file extension.
The original developer probably intended to retain some readability.

@jason810496 jason810496 force-pushed the fix/python-operator-dag-error-dag-with-hyphen branch from 04d716a to c1d99a9 Compare October 11, 2024 04:49
@shahar1 shahar1 added this to the Airflow 2.10.3 milestone Oct 11, 2024
@Lee-W Lee-W self-requested a review October 11, 2024 07:05
@jason810496 jason810496 force-pushed the fix/python-operator-dag-error-dag-with-hyphen branch from c1d99a9 to 2364a47 Compare October 11, 2024 07:57
tests/utils/test_file.py Outdated Show resolved Hide resolved
Copy link
Member

@Lee-W Lee-W left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left on nitpick. I think we're really close to merge.

@jason810496 jason810496 force-pushed the fix/python-operator-dag-error-dag-with-hyphen branch from 2364a47 to 8331beb Compare October 11, 2024 08:54
tests/utils/test_file.py Outdated Show resolved Hide resolved
tests/utils/test_file.py Outdated Show resolved Hide resolved
@jason810496 jason810496 force-pushed the fix/python-operator-dag-error-dag-with-hyphen branch from 8331beb to 0cc9dd4 Compare October 11, 2024 11:00
@eladkal eladkal requested a review from Lee-W October 12, 2024 00:42
@Lee-W
Copy link
Member

Lee-W commented Oct 12, 2024

Looks good to me. Will merge it once CI pass

@Lee-W Lee-W force-pushed the fix/python-operator-dag-error-dag-with-hyphen branch from 0cc9dd4 to a7eab44 Compare October 12, 2024 15:19
@Lee-W Lee-W merged commit 8b1c57a into apache:main Oct 13, 2024
81 checks passed
Copy link

boring-cyborg bot commented Oct 13, 2024

Awesome work, congrats on your first merged pull request! You are invited to check our Issue Tracker for additional contributions.

@Lee-W
Copy link
Member

Lee-W commented Oct 13, 2024

@jason810496 congrate on your first PR 🙂 I think this can be back ported to 2.10.x. Could you please open a PR based on branch v2-10-test

pavansharma36 pushed a commit to pavansharma36/airflow that referenced this pull request Oct 14, 2024
R7L208 pushed a commit to R7L208/airflow that referenced this pull request Oct 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PythonVirtualenvOperator - SyntaxError: cannot assign to operator
4 participants