mik-laj commented on a change in pull request #7197: URL: https://github.com/apache/airflow/pull/7197#discussion_r419220670
########## File path: airflow/sensors/decorators/poke_mode_only.py ########## @@ -0,0 +1,80 @@ +# +# 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. +# +import inspect +import os +from functools import wraps + +from airflow.sensors.base_sensor_operator import BaseSensorOperator + + +def poke_mode_only(cls): + """ + Class Decorator for child classes of BaseSensorOperator to indicate + that instances of this class are only safe to use poke mode. + + Will decorate all methods in the class to assert they did not change + the mode from 'poke'. + + :param cls: BaseSensor class to enforce methods only use 'poke' mode. + :type cls: type + """ + def decorate(cls): + if not issubclass(cls, BaseSensorOperator): + raise ValueError("poke_mode_only decorator should only be " + + "applied to subclasses of BaseSensorOperator," + + f" got:{cls}.") + for method in inspect.getmembers(cls, inspect.isfunction): Review comment: or as a decorator ```python def poke_only(cls_type): def mode_getter(_): return "poke" cls_type.mode = property(mode_getter) def mode_setter(_, value): if value != "value": raise AirflowException cls_type.mode.setter(mode_setter) ``` Both examples are not tested but should work. ########## File path: airflow/sensors/decorators/poke_mode_only.py ########## @@ -0,0 +1,80 @@ +# +# 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. +# +import inspect +import os +from functools import wraps + +from airflow.sensors.base_sensor_operator import BaseSensorOperator + + +def poke_mode_only(cls): + """ + Class Decorator for child classes of BaseSensorOperator to indicate + that instances of this class are only safe to use poke mode. + + Will decorate all methods in the class to assert they did not change + the mode from 'poke'. + + :param cls: BaseSensor class to enforce methods only use 'poke' mode. + :type cls: type + """ + def decorate(cls): + if not issubclass(cls, BaseSensorOperator): + raise ValueError("poke_mode_only decorator should only be " + + "applied to subclasses of BaseSensorOperator," + + f" got:{cls}.") + for method in inspect.getmembers(cls, inspect.isfunction): Review comment: Is it really necessary to decorate all methods? Is it not enough to create the read-only `poke` attribute? Something similar to code below: ```python @property def mode(self): return "poke" @mode.setter def mode(self, value): if value != "poke": raise AirflowException() ``` ########## File path: airflow/sensors/decorators/poke_mode_only.py ########## @@ -0,0 +1,80 @@ +# +# 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. +# +import inspect +import os +from functools import wraps + +from airflow.sensors.base_sensor_operator import BaseSensorOperator + + +def poke_mode_only(cls): + """ + Class Decorator for child classes of BaseSensorOperator to indicate + that instances of this class are only safe to use poke mode. + + Will decorate all methods in the class to assert they did not change + the mode from 'poke'. + + :param cls: BaseSensor class to enforce methods only use 'poke' mode. + :type cls: type + """ + def decorate(cls): + if not issubclass(cls, BaseSensorOperator): + raise ValueError("poke_mode_only decorator should only be " + + "applied to subclasses of BaseSensorOperator," + + f" got:{cls}.") + for method in inspect.getmembers(cls, inspect.isfunction): Review comment: or as a decorator ```python def poke_only(cls_type): def mode_getter(_): return "poke" cls_type.mode = property(mode_getter) def mode_setter(_, value): if value != "value": raise AirflowException() cls_type.mode.setter(mode_setter) ``` Both examples are not tested but should work. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org