uranusjr commented on code in PR #62211:
URL: https://github.com/apache/airflow/pull/62211#discussion_r2844807340


##########
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:
   Actually, maybe we can use `__new__`?
   
   ```python
   # Add @overload signatures...
   
   def __new__(
       cls,
       conn_id: str,
       conn_type: str | None = None,
       description: str | None = None,
       host: str | None = None,
       schema: str | None = None,
       login: str | None = None,
       password: str | None = None,
       port: int | None = None,
       extra: str | None = None,
       *,
       uri: str | None = None,
   ):
       if uri is not None:
           if any((conn_type ...)):
               raise TypeError("uri cannot be used with...")
           return cls.from_uri(uri=uri, conn_id=conn_id)
       return cls(conn_id, conn_type, ...)
   ```



-- 
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]

Reply via email to