Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mypy errors in example DAGs #19918

Closed
wants to merge 2 commits into from

Conversation

josh-fell
Copy link
Contributor

Part of #19891


^ Add meaningful description above

Read the Pull Request Guidelines for more information.
In case of fundamental code change, Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in UPDATING.md.

@josh-fell josh-fell requested a review from turbaszek as a code owner December 1, 2021 03:20
@boring-cyborg boring-cyborg bot added area:providers provider:Apache provider:amazon-aws AWS/Amazon - related issues provider:microsoft-azure Azure-related issues provider:google Google (including GCP) related issues labels Dec 1, 2021
@josh-fell
Copy link
Contributor Author

CC @potiuk

delete_bucket = S3DeleteBucketOperator(task_id='s3_bucket_dag_delete', force_delete=True)
delete_bucket = S3DeleteBucketOperator( # type: ignore[call-arg]
task_id='s3_bucket_dag_delete', force_delete=True
)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Chose to ignore the argument checking for each individual operator rather than the entire file (like several other example DAGs) because the check should be performed on the TaskFlow function. However, the TaskFlow function is unlikely to change so happy to apply ignore check to the entire file as well.

@josh-fell josh-fell changed the title Fix mypy errors in example dags Fix mypy errors in example DAGs Dec 1, 2021
Comment on lines +18 to +19
# Ignore mypy argument checking. Some operator args will be passed via ``default_args``.
# type: ignore[call-arg]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think instead of ignoring like this, we should probably “fix” @task to make these not fail in the first place.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've got airflow.mypy.plugin.decorators doing some things already, so that would be the place to add that sort of check if we cant do it with type stubs etc.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. I fully agree we should use the opportunity to improve our type hinting rather than disable it, as our goal is not to pass mypy but to get better type hinting and less errors :)

There is a class of errors that can be avoided if we do it (for example most of the None`s) - that actually hit our users in the recent months, so this might have some long-lasting "real-world" consequences.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ashb Thanks for pro tip!

A little confused though and probably a dumb question coming: The @task decorator doesn't apply in these example DAGs. Is this more related to BaseOperator? This is my first real foray into mypy madness so just trying to getting my bearings on a fix.

But If we did start adding type annotations on TaskFlow functions in example DAGs, mypy probably wouldn't complain during its arg checking, right? We'll have to set a default value for the arg in the function signature to avoid a TypeError when parsing the DAG in order to use default_args (as we would if we wanted to directly access context fields).

Mypy is quiet on something like this:

from datetime import datetime
from typing import Dict, Optional

from airflow.decorators import dag, task


@task
def blah(stuff: Optional[str] = None, other_stuff: str = "Nope."):
    print(stuff)
    print(other_stuff)


@dag(
    start_date=datetime(2021, 12, 1),
    schedule_interval=None,
    default_args={"stuff": "Here is your stuff.", "other_stuff": "Mine."},
)
def blah_blah():
    _blah = blah()


dag = blah_blah()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is really about not add "blanket" ignore for whole file, but trying to find better ways to deal with it. Maybe not yet complete, but a way that will be "simple" and easy to apply to get rid of all the current MyPy complaints.

There is a very interesting parallel discussion on validation of the taskflow args dynamically or at runtime: #19884. but I think we should not go there yet, we should just focus on what MyPy complains on currently.

We also have this one: #19886 by @uranusjr where we have a stub added to context so that the parameters in context are also type-hinted.

I am quite sure with some clever tricks we will be able to use MyPy to do more type validation on context params and inputs/outputs passed between tasks via task flows but, I think this should be next step after we fix all the current problems.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is really about not add "blanket" ignore for whole file, but trying to find better ways to deal with it.

Yeah it felt icky to do it and to slap # type: ignore[call-args] on each operator. But I suppose if they felt icky, probably means I shouldn't have done it. Although maybe the latter is a "passable" interim approach until the other conversations/solutions you pointed out get fleshed out? If there is no urgent timeline on this then perhaps it is better to put in a solid fix rather than bandaids.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree. We have a few parallel changes for that one and hopefully we'll find some good solutions for similar cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @potiuk I worded my comment poorly. You agree that I should add the ignore[call-args] markers or I should hold off on this PR/defer to the better solution?

@josh-fell josh-fell mentioned this pull request Dec 11, 2021
@potiuk potiuk added the mypy Fixing MyPy problems after bumpin MyPy to 0.990 label Dec 13, 2021
@potiuk
Copy link
Member

potiuk commented Dec 13, 2021

@josh-fell - still waiting on this or shall we close it?

@josh-fell
Copy link
Contributor Author

josh-fell commented Dec 14, 2021

@josh-fell - still waiting on this or shall we close it?

I had a poorly-worded question to you and wanted to know what you had agreed to above. Is it OK to move forward with adding the # type: ignore[call-args] comment to the individual operators in these DAGs (rather than the current "fix" of a blanket ignore on the file) or wait for the more proper parallel changes that are happening (something to effect of not needing all the ignore comments if an underlying module was fixed)? If it's the former, I can take care of it ASAP so I'm not a blocker.

@potiuk
Copy link
Member

potiuk commented Dec 14, 2021

I think we should do proper-fixes then :) this is not a blocker at all - I just started to think that this one might be closed as soon as all the parallell "smaller" fixes are merged, because they will fix everything here ;)

@josh-fell
Copy link
Contributor Author

I think we should do proper-fixes then :) this is not a blocker at all - I just started to think that this one might be closed as soon as all the parallell "smaller" fixes are merged, because they will fix everything here ;)

Yeah this might be something we tackle last (i.e. getting MyPy to "understand" default_args in DAGs as to not throw an argument-call error). I'm inclined to close this for now.

@josh-fell
Copy link
Contributor Author

Closing. We'll see if other fixes bubble up into alleviating these. If not, we can revisit.

@josh-fell josh-fell closed this Dec 16, 2021
@josh-fell josh-fell deleted the fix-mypy-errors-example-dags branch December 17, 2021 16:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:providers mypy Fixing MyPy problems after bumpin MyPy to 0.990 provider:amazon-aws AWS/Amazon - related issues provider:google Google (including GCP) related issues provider:microsoft-azure Azure-related issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants