ferruzzi commented on code in PR #34784: URL: https://github.com/apache/airflow/pull/34784#discussion_r1347884136
########## airflow/providers/amazon/aws/operators/base_aws.py: ########## @@ -0,0 +1,150 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from __future__ import annotations + +import warnings +from functools import cached_property +from typing import Any, Generic, Sequence, TypeVar + +from typing_extensions import final + +from airflow.exceptions import AirflowException, AirflowProviderDeprecationWarning +from airflow.models import BaseOperator +from airflow.providers.amazon.aws.hooks.base_aws import AwsGenericHook + +_AwsHook = TypeVar("_AwsHook", bound=AwsGenericHook) +_REGION_MSG = "`region` is deprecated and will be removed in the future. Please use `region_name` instead." + + +class AwsBaseOperator(BaseOperator, Generic[_AwsHook]): + """Base AWS (Amazon) Operator Class for build operators in top of AWS Hooks. + + Examples: + .. code-block:: python + + from airflow.providers.amazon.aws.hooks.foo_bar import FooBarThinHook, FooBarThickHook + + + class AwsFooBarOperator(AwsBaseOperator[FooBarThinHook]): + aws_hook_class = FooBarThinHook + + def execute(self, context): + pass + + + class AwsFooBarOperator2(AwsBaseOperator[FooBarThickHook]): + aws_hook_class = FooBarThickHook + + def __init__(self, *, spam: str, **kwargs): + super().__init__(**kwargs) + self.spam = spam + + @property + def _hook_parameters(self): + return {**super()._hook_parameters, "spam": self.spam} + + def execute(self, context): + pass + + :param aws_conn_id: The Airflow connection used for AWS credentials. + If this is None or empty then the default boto3 behaviour is used. If + running Airflow in a distributed manner and aws_conn_id is None or + empty, then default boto3 configuration would be used (and must be + maintained on each worker node). Review Comment: This repeating itself, but maybe I'm missing something. ########## tests/providers/amazon/aws/operators/test_base_aws.py: ########## @@ -0,0 +1,132 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +import pytest + +from airflow.exceptions import AirflowException, AirflowProviderDeprecationWarning +from airflow.hooks.base import BaseHook +from airflow.providers.amazon.aws.hooks.base_aws import AwsBaseHook +from airflow.providers.amazon.aws.operators.base_aws import AwsBaseOperator + +TEST_CONN = "aws_test_conn" + + +class FakeS3Hook(AwsBaseHook): + """Hook for tests, implements thin-wrapper around s3 client.""" + + def __init__(self, **kwargs): + kwargs.update({"client_type": "s3", "resource_type": None}) + super().__init__(**kwargs) + + +class FakeS3Operator(AwsBaseOperator): + aws_hook_class = FakeS3Hook + + [email protected](autouse=True) +def fake_conn(monkeypatch): + monkeypatch.setenv(f"AWS_CONN_{TEST_CONN.upper()}", '{"conn_type": "aws"}') + + +class TestAwsBaseOperator: + def test_default_parameters(self): + op = FakeS3Operator(task_id="fake_task_id") + msg = "Attention! Changes in default parameters might produce breaking changes in multiple operators" Review Comment: I like this one! Nice addition. ########## airflow/providers/amazon/aws/operators/base_aws.py: ########## @@ -0,0 +1,150 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from __future__ import annotations + +import warnings +from functools import cached_property +from typing import Any, Generic, Sequence, TypeVar + +from typing_extensions import final + +from airflow.exceptions import AirflowException, AirflowProviderDeprecationWarning +from airflow.models import BaseOperator +from airflow.providers.amazon.aws.hooks.base_aws import AwsGenericHook + +_AwsHook = TypeVar("_AwsHook", bound=AwsGenericHook) +_REGION_MSG = "`region` is deprecated and will be removed in the future. Please use `region_name` instead." + + +class AwsBaseOperator(BaseOperator, Generic[_AwsHook]): + """Base AWS (Amazon) Operator Class for build operators in top of AWS Hooks. + + Examples: + .. code-block:: python + + from airflow.providers.amazon.aws.hooks.foo_bar import FooBarThinHook, FooBarThickHook + + + class AwsFooBarOperator(AwsBaseOperator[FooBarThinHook]): + aws_hook_class = FooBarThinHook + + def execute(self, context): + pass + + + class AwsFooBarOperator2(AwsBaseOperator[FooBarThickHook]): + aws_hook_class = FooBarThickHook + + def __init__(self, *, spam: str, **kwargs): + super().__init__(**kwargs) + self.spam = spam + + @property + def _hook_parameters(self): + return {**super()._hook_parameters, "spam": self.spam} + + def execute(self, context): + pass + + :param aws_conn_id: The Airflow connection used for AWS credentials. + If this is None or empty then the default boto3 behaviour is used. If + running Airflow in a distributed manner and aws_conn_id is None or + empty, then default boto3 configuration would be used (and must be + maintained on each worker node). + :param region_name: AWS region_name. If not specified then the default boto3 behaviour is used. + :param verify: Whether or not to verify SSL certificates. See: + https://boto3.amazonaws.com/v1/documentation/api/latest/reference/core/session.html + :param botocore_config: Configuration dictionary (key-values) for botocore client. See: + https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html + """ + + aws_hook_class: type[_AwsHook] + template_fields: Sequence[str] = ( + "aws_conn_id", + "region_name", + "botocore_config", + ) + + def __init__( + self, + *, + aws_conn_id: str | None = "aws_default", + region_name: str | None = None, + verify: bool | str | None = None, + botocore_config: dict | None = None, + **kwargs, + ): + # Validate if ``aws_hook_class`` properly set with + if hasattr(self, "aws_hook_class"): + try: + if not issubclass(self.aws_hook_class, AwsGenericHook): + raise TypeError + except TypeError: + # Raise if ``aws_hook_class`` is not a class or not a subclass of Generic/Base AWS Hook + raise AirflowException( + f"Class attribute '{type(self).__name__}.aws_hook_class' " + f"is not a subclass of AwsGenericHook." + ) from None + else: + raise AirflowException(f"Class attribute '{type(self).__name__}.aws_hook_class' should be set.") + + if region := kwargs.pop("region", None): + warnings.warn(_REGION_MSG, AirflowProviderDeprecationWarning, stacklevel=3) + if region_name and region_name != region: + raise AirflowException( + f"Ambiguous `region_name` provided, region_name={region_name!r}, region={region!r}." + ) + region_name = region + + super().__init__(**kwargs) + self.aws_conn_id = aws_conn_id + self.region_name = region_name + self.verify = verify + self.botocore_config = botocore_config + + @property + def _hook_parameters(self) -> dict[str, Any]: + """Mapping parameters for build boto3-related hook. + + Only required to be overwritten for thick-wrapped Hooks. + """ + return { + "aws_conn_id": self.aws_conn_id, + "region_name": self.region_name, + "verify": self.verify, + "config": self.botocore_config, + } + + @cached_property + @final + def hook(self) -> _AwsHook: + """ + Return AWS Provider's hook based on ``aws_hook_class``. + + This method implementation should be taken as a final, which a good for + thin-wrapped Hooks around boto3, for thick-wrapped Hooks developer + should consider to overwrite ``_hook_parameters`` method instead. + """ + return self.aws_hook_class(**self._hook_parameters) + + @property + @final + def region(self) -> str | None: + """Alias for ``region_name``, uses for compatibility (deprecated).""" Review Comment: ```suggestion """Alias for ``region_name``, used for compatibility (deprecated).""" ``` ########## airflow/providers/amazon/aws/operators/base_aws.py: ########## @@ -0,0 +1,150 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from __future__ import annotations + +import warnings +from functools import cached_property +from typing import Any, Generic, Sequence, TypeVar + +from typing_extensions import final + +from airflow.exceptions import AirflowException, AirflowProviderDeprecationWarning +from airflow.models import BaseOperator +from airflow.providers.amazon.aws.hooks.base_aws import AwsGenericHook + +_AwsHook = TypeVar("_AwsHook", bound=AwsGenericHook) +_REGION_MSG = "`region` is deprecated and will be removed in the future. Please use `region_name` instead." + + +class AwsBaseOperator(BaseOperator, Generic[_AwsHook]): + """Base AWS (Amazon) Operator Class for build operators in top of AWS Hooks. Review Comment: ```suggestion """ Base AWS (Amazon) Operator Class to build operators on top of AWS Hooks. ``` ########## airflow/providers/amazon/aws/operators/base_aws.py: ########## @@ -0,0 +1,150 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from __future__ import annotations + +import warnings +from functools import cached_property +from typing import Any, Generic, Sequence, TypeVar + +from typing_extensions import final + +from airflow.exceptions import AirflowException, AirflowProviderDeprecationWarning +from airflow.models import BaseOperator +from airflow.providers.amazon.aws.hooks.base_aws import AwsGenericHook + +_AwsHook = TypeVar("_AwsHook", bound=AwsGenericHook) +_REGION_MSG = "`region` is deprecated and will be removed in the future. Please use `region_name` instead." + + +class AwsBaseOperator(BaseOperator, Generic[_AwsHook]): + """Base AWS (Amazon) Operator Class for build operators in top of AWS Hooks. + + Examples: + .. code-block:: python + + from airflow.providers.amazon.aws.hooks.foo_bar import FooBarThinHook, FooBarThickHook + + + class AwsFooBarOperator(AwsBaseOperator[FooBarThinHook]): + aws_hook_class = FooBarThinHook + + def execute(self, context): + pass + + + class AwsFooBarOperator2(AwsBaseOperator[FooBarThickHook]): + aws_hook_class = FooBarThickHook + + def __init__(self, *, spam: str, **kwargs): + super().__init__(**kwargs) + self.spam = spam + + @property + def _hook_parameters(self): + return {**super()._hook_parameters, "spam": self.spam} + + def execute(self, context): + pass + + :param aws_conn_id: The Airflow connection used for AWS credentials. + If this is None or empty then the default boto3 behaviour is used. If + running Airflow in a distributed manner and aws_conn_id is None or + empty, then default boto3 configuration would be used (and must be + maintained on each worker node). + :param region_name: AWS region_name. If not specified then the default boto3 behaviour is used. + :param verify: Whether or not to verify SSL certificates. See: + https://boto3.amazonaws.com/v1/documentation/api/latest/reference/core/session.html + :param botocore_config: Configuration dictionary (key-values) for botocore client. See: + https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html + """ + + aws_hook_class: type[_AwsHook] + template_fields: Sequence[str] = ( + "aws_conn_id", + "region_name", + "botocore_config", + ) + + def __init__( + self, + *, + aws_conn_id: str | None = "aws_default", + region_name: str | None = None, + verify: bool | str | None = None, + botocore_config: dict | None = None, + **kwargs, + ): + # Validate if ``aws_hook_class`` properly set with Review Comment: ```suggestion # Validate if ``aws_hook_class`` is properly set. ``` ########## airflow/providers/amazon/aws/operators/base_aws.py: ########## @@ -0,0 +1,150 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from __future__ import annotations + +import warnings +from functools import cached_property +from typing import Any, Generic, Sequence, TypeVar + +from typing_extensions import final + +from airflow.exceptions import AirflowException, AirflowProviderDeprecationWarning +from airflow.models import BaseOperator +from airflow.providers.amazon.aws.hooks.base_aws import AwsGenericHook + +_AwsHook = TypeVar("_AwsHook", bound=AwsGenericHook) +_REGION_MSG = "`region` is deprecated and will be removed in the future. Please use `region_name` instead." + + +class AwsBaseOperator(BaseOperator, Generic[_AwsHook]): + """Base AWS (Amazon) Operator Class for build operators in top of AWS Hooks. + + Examples: + .. code-block:: python + + from airflow.providers.amazon.aws.hooks.foo_bar import FooBarThinHook, FooBarThickHook + + + class AwsFooBarOperator(AwsBaseOperator[FooBarThinHook]): + aws_hook_class = FooBarThinHook + + def execute(self, context): + pass + + + class AwsFooBarOperator2(AwsBaseOperator[FooBarThickHook]): + aws_hook_class = FooBarThickHook + + def __init__(self, *, spam: str, **kwargs): + super().__init__(**kwargs) + self.spam = spam + + @property + def _hook_parameters(self): + return {**super()._hook_parameters, "spam": self.spam} + + def execute(self, context): + pass + + :param aws_conn_id: The Airflow connection used for AWS credentials. + If this is None or empty then the default boto3 behaviour is used. If + running Airflow in a distributed manner and aws_conn_id is None or + empty, then default boto3 configuration would be used (and must be + maintained on each worker node). + :param region_name: AWS region_name. If not specified then the default boto3 behaviour is used. + :param verify: Whether or not to verify SSL certificates. See: + https://boto3.amazonaws.com/v1/documentation/api/latest/reference/core/session.html + :param botocore_config: Configuration dictionary (key-values) for botocore client. See: + https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html + """ + + aws_hook_class: type[_AwsHook] + template_fields: Sequence[str] = ( + "aws_conn_id", + "region_name", + "botocore_config", + ) + + def __init__( + self, + *, + aws_conn_id: str | None = "aws_default", + region_name: str | None = None, + verify: bool | str | None = None, + botocore_config: dict | None = None, + **kwargs, + ): + # Validate if ``aws_hook_class`` properly set with + if hasattr(self, "aws_hook_class"): + try: + if not issubclass(self.aws_hook_class, AwsGenericHook): + raise TypeError + except TypeError: + # Raise if ``aws_hook_class`` is not a class or not a subclass of Generic/Base AWS Hook + raise AirflowException( + f"Class attribute '{type(self).__name__}.aws_hook_class' " + f"is not a subclass of AwsGenericHook." + ) from None + else: + raise AirflowException(f"Class attribute '{type(self).__name__}.aws_hook_class' should be set.") + + if region := kwargs.pop("region", None): + warnings.warn(_REGION_MSG, AirflowProviderDeprecationWarning, stacklevel=3) + if region_name and region_name != region: + raise AirflowException( + f"Ambiguous `region_name` provided, region_name={region_name!r}, region={region!r}." + ) + region_name = region + + super().__init__(**kwargs) + self.aws_conn_id = aws_conn_id + self.region_name = region_name + self.verify = verify + self.botocore_config = botocore_config + + @property + def _hook_parameters(self) -> dict[str, Any]: + """Mapping parameters for build boto3-related hook. Review Comment: ```suggestion """ Mapping parameters to build boto3-related hooks. ``` ########## airflow/providers/amazon/aws/operators/base_aws.py: ########## @@ -0,0 +1,150 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from __future__ import annotations + +import warnings +from functools import cached_property +from typing import Any, Generic, Sequence, TypeVar + +from typing_extensions import final + +from airflow.exceptions import AirflowException, AirflowProviderDeprecationWarning +from airflow.models import BaseOperator +from airflow.providers.amazon.aws.hooks.base_aws import AwsGenericHook + +_AwsHook = TypeVar("_AwsHook", bound=AwsGenericHook) +_REGION_MSG = "`region` is deprecated and will be removed in the future. Please use `region_name` instead." + + +class AwsBaseOperator(BaseOperator, Generic[_AwsHook]): + """Base AWS (Amazon) Operator Class for build operators in top of AWS Hooks. + + Examples: + .. code-block:: python + + from airflow.providers.amazon.aws.hooks.foo_bar import FooBarThinHook, FooBarThickHook + + + class AwsFooBarOperator(AwsBaseOperator[FooBarThinHook]): + aws_hook_class = FooBarThinHook + + def execute(self, context): + pass + + + class AwsFooBarOperator2(AwsBaseOperator[FooBarThickHook]): + aws_hook_class = FooBarThickHook + + def __init__(self, *, spam: str, **kwargs): + super().__init__(**kwargs) + self.spam = spam + + @property + def _hook_parameters(self): + return {**super()._hook_parameters, "spam": self.spam} + + def execute(self, context): + pass + + :param aws_conn_id: The Airflow connection used for AWS credentials. + If this is None or empty then the default boto3 behaviour is used. If + running Airflow in a distributed manner and aws_conn_id is None or + empty, then default boto3 configuration would be used (and must be + maintained on each worker node). + :param region_name: AWS region_name. If not specified then the default boto3 behaviour is used. + :param verify: Whether or not to verify SSL certificates. See: + https://boto3.amazonaws.com/v1/documentation/api/latest/reference/core/session.html + :param botocore_config: Configuration dictionary (key-values) for botocore client. See: + https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html + """ + + aws_hook_class: type[_AwsHook] + template_fields: Sequence[str] = ( + "aws_conn_id", + "region_name", + "botocore_config", + ) + + def __init__( + self, + *, + aws_conn_id: str | None = "aws_default", + region_name: str | None = None, + verify: bool | str | None = None, + botocore_config: dict | None = None, + **kwargs, + ): + # Validate if ``aws_hook_class`` properly set with + if hasattr(self, "aws_hook_class"): + try: + if not issubclass(self.aws_hook_class, AwsGenericHook): + raise TypeError + except TypeError: + # Raise if ``aws_hook_class`` is not a class or not a subclass of Generic/Base AWS Hook + raise AirflowException( + f"Class attribute '{type(self).__name__}.aws_hook_class' " + f"is not a subclass of AwsGenericHook." + ) from None + else: + raise AirflowException(f"Class attribute '{type(self).__name__}.aws_hook_class' should be set.") + + if region := kwargs.pop("region", None): + warnings.warn(_REGION_MSG, AirflowProviderDeprecationWarning, stacklevel=3) + if region_name and region_name != region: + raise AirflowException( + f"Ambiguous `region_name` provided, region_name={region_name!r}, region={region!r}." Review Comment: ```suggestion f"Conflicting `region_name` provided, region_name={region_name!r}, region={region!r}." ``` ########## tests/providers/amazon/aws/operators/test_base_aws.py: ########## @@ -0,0 +1,132 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +import pytest + +from airflow.exceptions import AirflowException, AirflowProviderDeprecationWarning +from airflow.hooks.base import BaseHook +from airflow.providers.amazon.aws.hooks.base_aws import AwsBaseHook +from airflow.providers.amazon.aws.operators.base_aws import AwsBaseOperator + +TEST_CONN = "aws_test_conn" + + +class FakeS3Hook(AwsBaseHook): + """Hook for tests, implements thin-wrapper around s3 client.""" + + def __init__(self, **kwargs): + kwargs.update({"client_type": "s3", "resource_type": None}) + super().__init__(**kwargs) + + +class FakeS3Operator(AwsBaseOperator): + aws_hook_class = FakeS3Hook + + [email protected](autouse=True) +def fake_conn(monkeypatch): + monkeypatch.setenv(f"AWS_CONN_{TEST_CONN.upper()}", '{"conn_type": "aws"}') + + +class TestAwsBaseOperator: + def test_default_parameters(self): + op = FakeS3Operator(task_id="fake_task_id") + msg = "Attention! Changes in default parameters might produce breaking changes in multiple operators" + assert op.aws_conn_id == "aws_default", msg + assert op.region_name is None, msg + assert op.verify is None, msg + assert op.botocore_config is None, msg + + def test_parameters(self): + op = FakeS3Operator( + task_id="fake-task-id", + aws_conn_id=TEST_CONN, + region_name="eu-central-1", + verify=False, + botocore_config={"read_timeout": 777, "connect_timeout": 42}, + ) + + assert op.aws_conn_id == TEST_CONN + assert op.region_name == "eu-central-1" + assert op.verify is False + assert op.botocore_config == {"read_timeout": 777, "connect_timeout": 42} + + hook = op.hook + assert isinstance(hook, FakeS3Hook) + assert hook.aws_conn_id == op.aws_conn_id + assert hook._region_name == op.region_name + assert hook._verify == op.verify + assert hook._config.read_timeout == 777 + assert hook._config.connect_timeout == 42 + + @pytest.mark.parametrize( + "region, region_name", + [ + pytest.param("eu-west-1", None, id="region-only"), + pytest.param("us-east-1", "us-east-1", id="non-ambiguous-params"), + ], + ) + def test_deprecated_region_name(self, region, region_name): + warning_match = r"`region` is deprecated and will be removed" + with pytest.warns(AirflowProviderDeprecationWarning, match=warning_match): + op = FakeS3Operator( + task_id="fake-task-id", + aws_conn_id=TEST_CONN, + region=region, + region_name=region_name, + ) + assert op.region_name == region + + with pytest.warns(AirflowProviderDeprecationWarning, match=warning_match): + assert op.region == region + + def test_ambiguous_region_name(self): Review Comment: If you accept the suggestion to reword the warning, you'll need to change this as well. ########## airflow/providers/amazon/aws/sensors/base_aws.py: ########## @@ -0,0 +1,145 @@ +# Licensed to the Apache Software Foundation (ASF) under one Review Comment: All of he above suggestions apply down here as well. ########## airflow/providers/amazon/aws/operators/base_aws.py: ########## @@ -0,0 +1,150 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from __future__ import annotations + +import warnings +from functools import cached_property +from typing import Any, Generic, Sequence, TypeVar + +from typing_extensions import final + +from airflow.exceptions import AirflowException, AirflowProviderDeprecationWarning +from airflow.models import BaseOperator +from airflow.providers.amazon.aws.hooks.base_aws import AwsGenericHook + +_AwsHook = TypeVar("_AwsHook", bound=AwsGenericHook) +_REGION_MSG = "`region` is deprecated and will be removed in the future. Please use `region_name` instead." + + +class AwsBaseOperator(BaseOperator, Generic[_AwsHook]): + """Base AWS (Amazon) Operator Class for build operators in top of AWS Hooks. + + Examples: + .. code-block:: python + + from airflow.providers.amazon.aws.hooks.foo_bar import FooBarThinHook, FooBarThickHook + + + class AwsFooBarOperator(AwsBaseOperator[FooBarThinHook]): + aws_hook_class = FooBarThinHook + + def execute(self, context): + pass + + + class AwsFooBarOperator2(AwsBaseOperator[FooBarThickHook]): + aws_hook_class = FooBarThickHook + + def __init__(self, *, spam: str, **kwargs): + super().__init__(**kwargs) + self.spam = spam + + @property + def _hook_parameters(self): + return {**super()._hook_parameters, "spam": self.spam} + + def execute(self, context): + pass + + :param aws_conn_id: The Airflow connection used for AWS credentials. + If this is None or empty then the default boto3 behaviour is used. If + running Airflow in a distributed manner and aws_conn_id is None or + empty, then default boto3 configuration would be used (and must be + maintained on each worker node). + :param region_name: AWS region_name. If not specified then the default boto3 behaviour is used. + :param verify: Whether or not to verify SSL certificates. See: + https://boto3.amazonaws.com/v1/documentation/api/latest/reference/core/session.html + :param botocore_config: Configuration dictionary (key-values) for botocore client. See: + https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html + """ + + aws_hook_class: type[_AwsHook] + template_fields: Sequence[str] = ( + "aws_conn_id", + "region_name", + "botocore_config", + ) + + def __init__( + self, + *, + aws_conn_id: str | None = "aws_default", + region_name: str | None = None, + verify: bool | str | None = None, + botocore_config: dict | None = None, + **kwargs, + ): + # Validate if ``aws_hook_class`` properly set with + if hasattr(self, "aws_hook_class"): + try: + if not issubclass(self.aws_hook_class, AwsGenericHook): + raise TypeError + except TypeError: + # Raise if ``aws_hook_class`` is not a class or not a subclass of Generic/Base AWS Hook + raise AirflowException( + f"Class attribute '{type(self).__name__}.aws_hook_class' " + f"is not a subclass of AwsGenericHook." + ) from None + else: + raise AirflowException(f"Class attribute '{type(self).__name__}.aws_hook_class' should be set.") + + if region := kwargs.pop("region", None): + warnings.warn(_REGION_MSG, AirflowProviderDeprecationWarning, stacklevel=3) + if region_name and region_name != region: + raise AirflowException( + f"Ambiguous `region_name` provided, region_name={region_name!r}, region={region!r}." + ) + region_name = region + + super().__init__(**kwargs) + self.aws_conn_id = aws_conn_id + self.region_name = region_name + self.verify = verify + self.botocore_config = botocore_config + + @property + def _hook_parameters(self) -> dict[str, Any]: + """Mapping parameters for build boto3-related hook. + + Only required to be overwritten for thick-wrapped Hooks. + """ + return { + "aws_conn_id": self.aws_conn_id, + "region_name": self.region_name, + "verify": self.verify, + "config": self.botocore_config, + } + + @cached_property + @final + def hook(self) -> _AwsHook: + """ + Return AWS Provider's hook based on ``aws_hook_class``. + + This method implementation should be taken as a final, which a good for + thin-wrapped Hooks around boto3, for thick-wrapped Hooks developer + should consider to overwrite ``_hook_parameters`` method instead. + """ Review Comment: ```suggestion This method implementation should be taken as a final for thin-wrapped Hooks around boto3. For thick-wrapped Hooks developer should consider to overwrite ``_hook_parameters`` method instead. """ ``` May also need to adjust line breaks after this change. -- 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]
