Skip to content

Add support to create connections using uri in SDK#62211

Merged
amoghrajesh merged 8 commits into
apache:mainfrom
astronomer:allow-uri-in-connections
Feb 27, 2026
Merged

Add support to create connections using uri in SDK#62211
amoghrajesh merged 8 commits into
apache:mainfrom
astronomer:allow-uri-in-connections

Conversation

@amoghrajesh

Copy link
Copy Markdown
Contributor

Was generative AI tooling used to co-author this PR?
  • No

Why?

Happened to notice in https://github.com/apache/airflow/blob/main/providers/apache/livy/tests/unit/apache/livy/hooks/test_livy.py#L91 for example that it was allowed to create a connection in airflow 2.x with uri field. Although we have support for it now using from_uri this could mean that some DAGs from legacy base could fail due to this.

So I am intending to support constructing Connection from a URI now.

I had to use __attrs_post_init__ (to avoid attrs.define(init=False) and a custom __init__ to keep the class simple) to parse uri when provided and populate other fields: conn_type, host,
login, password, port, schema, and extra. This is because: uri is init-only and excluded from serialization (to_dict, from_json) matching the behaviour of models.connection.


  • Read the Pull Request Guidelines for more information. Note: commit author/co-author name and email in commits become permanently public when merged.
  • For fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
  • When adding dependency, check compliance with the ASF 3rd Party License Policy.
  • For significant user-facing changes create newsfragment: {pr_number}.significant.rst or {issue_number}.significant.rst, in airflow-core/newsfragments.

Comment thread task-sdk/src/airflow/sdk/definitions/connection.py Outdated
@amoghrajesh

Copy link
Copy Markdown
Contributor Author

Some small adjustments needed to fix the CI

_______________________________________________________________________ TestConnectionFromUri.test_connection_constructor_with_uri _______________________________________________________________________
[gw1] linux -- Python 3.10.19 /usr/python/bin/python
task-sdk/tests/task_sdk/definitions/test_connection.py:382: in test_connection_constructor_with_uri
    conn = Connection(conn_id="test_conn", uri="**host:5432/db")
task-sdk/src/airflow/sdk/definitions/connection.py:153: in __init__
    self.__dict__.update(self.from_uri(uri, conn_id=conn_id).to_dict(validate=False))
E   AttributeError: 'Connection' object has no attribute '__dict__'. Did you mean: '__dir__'?

Looks like I need to set slots=False on the attrs constructor

@amoghrajesh amoghrajesh requested a review from uranusjr February 26, 2026 11:35
@amoghrajesh

Copy link
Copy Markdown
Contributor Author

LGTM but I cannot approve my own PR haha!

Comment thread task-sdk/src/airflow/sdk/definitions/connection.py Outdated

@uranusjr uranusjr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

One line to remove

@amoghrajesh amoghrajesh merged commit 8a3a522 into apache:main Feb 27, 2026
181 of 182 checks passed
@amoghrajesh amoghrajesh deleted the allow-uri-in-connections branch February 27, 2026 13:33
AkshayArali pushed a commit to AkshayArali/airflow_630 that referenced this pull request Feb 28, 2026
* Add support to create connections using uri in SDK

* Add support to create connections using uri in SDK

* Add support to create connections using uri in SDK

* fixing unit tests

* Allow URI without adding it as a field

* fixing failing tests and mypy

* remove unwanted line

---------

Co-authored-by: Tzu-ping Chung <uranusjr@gmail.com>
dominikhei pushed a commit to dominikhei/airflow that referenced this pull request Mar 11, 2026
* Add support to create connections using uri in SDK

* Add support to create connections using uri in SDK

* Add support to create connections using uri in SDK

* fixing unit tests

* Allow URI without adding it as a field

* fixing failing tests and mypy

* remove unwanted line

---------

Co-authored-by: Tzu-ping Chung <uranusjr@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants