-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[airflow] Passing positional argument into airflow.lineage.hook.HookLineageCollector.create_asset is not allowed (AIR303)
#22046
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
Merged
ntBre
merged 7 commits into
astral-sh:main
from
sjyangkevin:add-AIR303-function-signature-change
Dec 30, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2b371c5
add new code AIR303 and add rule for checking function signature chan…
sjyangkevin d28ee50
updates according to feedback
sjyangkevin 1b9d9a0
add new enum and remove fix/suggest
sjyangkevin 3c6e759
improve code structure and edge case handling
sjyangkevin 32c9c6a
add a new line to end of AIR303.py
sjyangkevin 5e992fe
clean up
sjyangkevin 59c7f95
small improvements and clean up
sjyangkevin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
crates/ruff_linter/resources/test/fixtures/airflow/AIR303.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from airflow.lineage.hook import HookLineageCollector | ||
|
|
||
| # airflow.lineage.hook | ||
| hlc = HookLineageCollector() | ||
| hlc.create_asset("there") | ||
| hlc.create_asset("should", "be", "no", "posarg") | ||
| hlc.create_asset(name="but", uri="kwargs are ok") | ||
| hlc.create_asset() | ||
|
|
||
| HookLineageCollector().create_asset(name="but", uri="kwargs are ok") | ||
| HookLineageCollector().create_asset("there") | ||
| HookLineageCollector().create_asset("should", "be", "no", "posarg") | ||
|
|
||
| args = ["uri_value"] | ||
| hlc.create_asset(*args) | ||
| HookLineageCollector().create_asset(*args) | ||
|
|
||
| # Literal unpacking | ||
| hlc.create_asset(*["literal_uri"]) | ||
| HookLineageCollector().create_asset(*["literal_uri"]) | ||
|
|
||
| # starred args with keyword args | ||
| hlc.create_asset(*args, extra="value") | ||
| HookLineageCollector().create_asset(*args, extra="value") | ||
|
|
||
| # Double-starred keyword arguments | ||
| kwargs = {"uri": "value", "name": "test"} | ||
| hlc.create_asset(**kwargs) | ||
| HookLineageCollector().create_asset(**kwargs) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
crates/ruff_linter/src/rules/airflow/rules/function_signature_change_in_3.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| use crate::checkers::ast::Checker; | ||
| use crate::{FixAvailability, Violation}; | ||
| use ruff_macros::{ViolationMetadata, derive_message_formats}; | ||
| use ruff_python_ast::name::QualifiedName; | ||
| use ruff_python_ast::{Arguments, Expr, ExprAttribute, ExprCall, Identifier}; | ||
| use ruff_python_semantic::Modules; | ||
| use ruff_python_semantic::analyze::typing; | ||
| use ruff_text_size::Ranged; | ||
|
|
||
| /// ## What it does | ||
| /// Checks for Airflow function calls that will raise a runtime error in Airflow 3.0 | ||
| /// due to function signature changes, such as functions that changed to accept only | ||
| /// keyword arguments, parameter reordering, or parameter type changes. | ||
| /// | ||
| /// ## Why is this bad? | ||
| /// Airflow 3.0 introduces changes to function signatures. Code that | ||
| /// worked in Airflow 2.x will raise a runtime error if not updated in Airflow | ||
| /// 3.0. | ||
| /// | ||
| /// ## Example | ||
| /// ```python | ||
| /// from airflow.lineage.hook import HookLineageCollector | ||
| /// | ||
| /// collector = HookLineageCollector() | ||
| /// # Passing positional arguments will raise a runtime error in Airflow 3.0 | ||
| /// collector.create_asset("s3://bucket/key") | ||
| /// ``` | ||
| /// | ||
| /// Use instead: | ||
| /// ```python | ||
| /// from airflow.lineage.hook import HookLineageCollector | ||
| /// | ||
| /// collector = HookLineageCollector() | ||
| /// # Passing arguments as keyword arguments instead of positional arguments | ||
| /// collector.create_asset(uri="s3://bucket/key") | ||
| /// ``` | ||
| #[derive(ViolationMetadata)] | ||
| #[violation_metadata(preview_since = "0.14.11")] | ||
| pub(crate) struct Airflow3IncompatibleFunctionSignature { | ||
| function_name: String, | ||
| change_type: FunctionSignatureChangeType, | ||
| } | ||
|
|
||
| impl Violation for Airflow3IncompatibleFunctionSignature { | ||
| const FIX_AVAILABILITY: FixAvailability = FixAvailability::None; | ||
|
|
||
| #[derive_message_formats] | ||
| fn message(&self) -> String { | ||
| let Airflow3IncompatibleFunctionSignature { | ||
| function_name, | ||
| change_type, | ||
| } = self; | ||
| match change_type { | ||
| FunctionSignatureChangeType::KeywordOnly { .. } => { | ||
| format!("`{function_name}` signature is changed in Airflow 3.0") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn fix_title(&self) -> Option<String> { | ||
| let Airflow3IncompatibleFunctionSignature { change_type, .. } = self; | ||
| match change_type { | ||
| FunctionSignatureChangeType::KeywordOnly { message } => Some(message.to_string()), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// AIR303 | ||
| pub(crate) fn airflow_3_incompatible_function_signature(checker: &Checker, expr: &Expr) { | ||
| if !checker.semantic().seen_module(Modules::AIRFLOW) { | ||
| return; | ||
| } | ||
|
|
||
| let Expr::Call(ExprCall { | ||
| func, arguments, .. | ||
| }) = expr | ||
| else { | ||
| return; | ||
| }; | ||
|
|
||
| let Expr::Attribute(ExprAttribute { attr, value, .. }) = func.as_ref() else { | ||
| return; | ||
| }; | ||
|
|
||
| // Resolve the qualified name: try variable assignments first, then fall back to direct | ||
| // constructor calls. | ||
| let qualified_name = typing::resolve_assignment(value, checker.semantic()).or_else(|| { | ||
| value | ||
| .as_call_expr() | ||
| .and_then(|call| checker.semantic().resolve_qualified_name(&call.func)) | ||
| }); | ||
|
|
||
| let Some(qualified_name) = qualified_name else { | ||
| return; | ||
| }; | ||
|
|
||
| check_keyword_only_method(checker, &qualified_name, attr, arguments); | ||
| } | ||
|
|
||
| fn check_keyword_only_method( | ||
| checker: &Checker, | ||
| qualified_name: &QualifiedName, | ||
| attr: &Identifier, | ||
| arguments: &Arguments, | ||
| ) { | ||
| let has_positional_args = | ||
| arguments.find_positional(0).is_some() || arguments.args.iter().any(Expr::is_starred_expr); | ||
|
|
||
| if let ["airflow", "lineage", "hook", "HookLineageCollector"] = qualified_name.segments() { | ||
| if attr.as_str() == "create_asset" && has_positional_args { | ||
| checker.report_diagnostic( | ||
| Airflow3IncompatibleFunctionSignature { | ||
| function_name: attr.to_string(), | ||
| change_type: FunctionSignatureChangeType::KeywordOnly { | ||
| message: "Pass positional arguments as keyword arguments (e.g., `create_asset(uri=...)`)", | ||
| }, | ||
| }, | ||
| attr.range(), | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Clone, Debug, Eq, PartialEq)] | ||
| pub(crate) enum FunctionSignatureChangeType { | ||
| /// Function signature changed to only accept keyword arguments. | ||
| KeywordOnly { message: &'static str }, | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
...ter/src/rules/airflow/snapshots/ruff_linter__rules__airflow__tests__AIR303_AIR303.py.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| --- | ||
| source: crates/ruff_linter/src/rules/airflow/mod.rs | ||
| --- | ||
| AIR303 `create_asset` signature is changed in Airflow 3.0 | ||
| --> AIR303.py:7:5 | ||
| | | ||
| 5 | # airflow.lineage.hook | ||
| 6 | hlc = HookLineageCollector() | ||
| 7 | hlc.create_asset("there") | ||
| | ^^^^^^^^^^^^ | ||
| 8 | hlc.create_asset("should", "be", "no", "posarg") | ||
| 9 | hlc.create_asset(name="but", uri="kwargs are ok") | ||
| | | ||
| help: Pass positional arguments as keyword arguments (e.g., `create_asset(uri=...)`) | ||
|
|
||
| AIR303 `create_asset` signature is changed in Airflow 3.0 | ||
| --> AIR303.py:8:5 | ||
| | | ||
| 6 | hlc = HookLineageCollector() | ||
| 7 | hlc.create_asset("there") | ||
| 8 | hlc.create_asset("should", "be", "no", "posarg") | ||
| | ^^^^^^^^^^^^ | ||
| 9 | hlc.create_asset(name="but", uri="kwargs are ok") | ||
| 10 | hlc.create_asset() | ||
| | | ||
| help: Pass positional arguments as keyword arguments (e.g., `create_asset(uri=...)`) | ||
|
|
||
| AIR303 `create_asset` signature is changed in Airflow 3.0 | ||
| --> AIR303.py:13:24 | ||
| | | ||
| 12 | HookLineageCollector().create_asset(name="but", uri="kwargs are ok") | ||
| 13 | HookLineageCollector().create_asset("there") | ||
| | ^^^^^^^^^^^^ | ||
| 14 | HookLineageCollector().create_asset("should", "be", "no", "posarg") | ||
| | | ||
| help: Pass positional arguments as keyword arguments (e.g., `create_asset(uri=...)`) | ||
|
|
||
| AIR303 `create_asset` signature is changed in Airflow 3.0 | ||
| --> AIR303.py:14:24 | ||
| | | ||
| 12 | HookLineageCollector().create_asset(name="but", uri="kwargs are ok") | ||
| 13 | HookLineageCollector().create_asset("there") | ||
| 14 | HookLineageCollector().create_asset("should", "be", "no", "posarg") | ||
| | ^^^^^^^^^^^^ | ||
| 15 | | ||
| 16 | args = ["uri_value"] | ||
| | | ||
| help: Pass positional arguments as keyword arguments (e.g., `create_asset(uri=...)`) | ||
|
|
||
| AIR303 `create_asset` signature is changed in Airflow 3.0 | ||
| --> AIR303.py:17:5 | ||
| | | ||
| 16 | args = ["uri_value"] | ||
| 17 | hlc.create_asset(*args) | ||
| | ^^^^^^^^^^^^ | ||
| 18 | HookLineageCollector().create_asset(*args) | ||
| | | ||
| help: Pass positional arguments as keyword arguments (e.g., `create_asset(uri=...)`) | ||
|
|
||
| AIR303 `create_asset` signature is changed in Airflow 3.0 | ||
| --> AIR303.py:18:24 | ||
| | | ||
| 16 | args = ["uri_value"] | ||
| 17 | hlc.create_asset(*args) | ||
| 18 | HookLineageCollector().create_asset(*args) | ||
| | ^^^^^^^^^^^^ | ||
| 19 | | ||
| 20 | # Literal unpacking | ||
| | | ||
| help: Pass positional arguments as keyword arguments (e.g., `create_asset(uri=...)`) | ||
|
|
||
| AIR303 `create_asset` signature is changed in Airflow 3.0 | ||
| --> AIR303.py:21:5 | ||
| | | ||
| 20 | # Literal unpacking | ||
| 21 | hlc.create_asset(*["literal_uri"]) | ||
| | ^^^^^^^^^^^^ | ||
| 22 | HookLineageCollector().create_asset(*["literal_uri"]) | ||
| | | ||
| help: Pass positional arguments as keyword arguments (e.g., `create_asset(uri=...)`) | ||
|
|
||
| AIR303 `create_asset` signature is changed in Airflow 3.0 | ||
| --> AIR303.py:22:24 | ||
| | | ||
| 20 | # Literal unpacking | ||
| 21 | hlc.create_asset(*["literal_uri"]) | ||
| 22 | HookLineageCollector().create_asset(*["literal_uri"]) | ||
| | ^^^^^^^^^^^^ | ||
| 23 | | ||
| 24 | # starred args with keyword args | ||
| | | ||
| help: Pass positional arguments as keyword arguments (e.g., `create_asset(uri=...)`) | ||
|
|
||
| AIR303 `create_asset` signature is changed in Airflow 3.0 | ||
| --> AIR303.py:25:5 | ||
| | | ||
| 24 | # starred args with keyword args | ||
| 25 | hlc.create_asset(*args, extra="value") | ||
| | ^^^^^^^^^^^^ | ||
| 26 | HookLineageCollector().create_asset(*args, extra="value") | ||
| | | ||
| help: Pass positional arguments as keyword arguments (e.g., `create_asset(uri=...)`) | ||
|
|
||
| AIR303 `create_asset` signature is changed in Airflow 3.0 | ||
| --> AIR303.py:26:24 | ||
| | | ||
| 24 | # starred args with keyword args | ||
| 25 | hlc.create_asset(*args, extra="value") | ||
| 26 | HookLineageCollector().create_asset(*args, extra="value") | ||
| | ^^^^^^^^^^^^ | ||
| 27 | | ||
| 28 | # Double-starred keyword arguments | ||
| | | ||
| help: Pass positional arguments as keyword arguments (e.g., `create_asset(uri=...)`) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.