amoghrajesh commented on code in PR #62211:
URL: https://github.com/apache/airflow/pull/62211#discussion_r2845046645
##########
task-sdk/src/airflow/sdk/definitions/connection.py:
##########
@@ -119,9 +120,26 @@ class Connection:
password: str | None = None
port: int | None = None
extra: str | None = None
+ uri: str | None = None
EXTRA_KEY = "__extra__"
+ _URI_FORBIDDEN_FIELDS = ("conn_type", "host", "login", "password",
"schema", "port", "extra")
+
+ def __attrs_post_init__(self) -> None:
+ if self.uri is not None:
+ if any(getattr(self, f) for f in self._URI_FORBIDDEN_FIELDS):
+ raise AirflowException(
+ "You must create an object using the URI or individual
values "
+ "(conn_type, host, login, password, schema, port or
extra). "
+ "You can't mix these two ways to create this object."
+ )
+ conn_from_uri = self.from_uri(self.uri, conn_id=self.conn_id)
+ for attr in attrs.fields(type(conn_from_uri)):
+ if attr.name != "uri":
+ object.__setattr__(self, attr.name, getattr(conn_from_uri,
attr.name))
+ object.__setattr__(self, "uri", None)
Review Comment:
@uranusjr I tried it out and seems there is a problem there. (trying to use
`__new__` without an `__init__`
This is what seems to happen:
1. Lets say i did this: `obj = Connection(conn_id="test",
uri="postgres://host")`
2. It would call `__new__` first and eventually it would return a
`Connection` class leading to call `__init__` since a new object of Connection
is returned
3. In short I saw this `If __new__() returns an instance of cls,
then __init__(instance, ...) will be called with the same arguments.`
So essentially this happens:
```python
from airflow.sdk.definitions.connection import Connection
Connection(conn_id="test_conn", uri="postgres://user:pass@host:5432/db")
Out[4]: Connection(conn_id='test_conn', conn_type=None, description=None,
host=None, schema=None, login=None, password=None, port=None, extra=None,
uri='postgres://user:pass@host:5432/db')
```
`__new__` called from_uri() which returned a Connection with parsed values
and called `__init__` again which reset everything. Any suggestions?
--
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]