0

I am new to Python and airflow. Trying to implement a sensor as below, and the error says "AttributeError: 'mySensor' object has no attribute 'l'" I had a look at other attribute error questions, but I have no idea where the 'l' in my error comes from. Could someone help shed some light on this? Below is the whole class for mySensor. Many thanks.

class mySensor(SFTPSensor):
"""
Subclass of SFTPSensor to override the poke() method 
"""
template_fields = "previous_month"

@apply_defaults
def __init__(self,
             last_day_previous_month,
             *args,
             **kwargs):
    self.previous_month = previous_month
    super(mySensor, self).__init__(*args, **kwargs)

def poke(self, context):
    remote_path = self.path+"file_to_check"+self.previous_month
    file_count = len(self.hook.list_directory(remote_path))
    if file_count == 0:
        return False
    else:
        logging.info("Found %d files", file_count)
        return True

and where I used the Sensor

sensor_task = mySensor(
                    previous_month=_previous_month_template,
                    task_id="check-remote-files",
                    dag=dag,
                    sftp_conn_id=my_conn_id,
                    path="/my/path/"
                    )
2
  • can you please format your complete error. It would be helpful to debug. Commented Jul 2, 2019 at 5:10
  • 1
    Can you change template_fields = "previous_month" to template_fields = ["previous_month"]? It is expected to be a list or tuple. Also I don't see last_day_previous_month argument being passed to the sensor but it is an expected argument in __init__ function in your sensor Commented Jul 2, 2019 at 6:54

1 Answer 1

6

I was getting a similar error with an Airflow operator:

AttributeError: 'MyOperator' object has no attribute 't'

To resolve, check that the template_fields makes sense compared to your __init__ arguments.

You have template_fields = "previous_month" but in your __init__ there is no such parameter.

In my case, the __init__ and template_fields did align. However, I had template_fields = ("myfield") instead of template_fields = ("myfield",). The comma must be present.

Sign up to request clarification or add additional context in comments.

2 Comments

wow this is a strange one, thanks for catching the fact that the comma is needed. It is not mentioned nor present in the docs: airflow.apache.org/docs/apache-airflow/1.10.14/howto/…
In Python ("xyz") becomes a string, whereas ("xyz",) is a tuple. In BaseOperator the template_fields parameter expects a list / tuple of things to iterate through. Co-incidentally a string is also an iterable of strings which presumably breaks it. An issue has been logged for this - github.com/apache/airflow/issues/12876 Just needs someone to contribute a fix :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.