Skip to content

Conversation

@khalidmammadov
Copy link
Contributor

Part of #19891

Issue fixed:

  • Ignoring issue when mandatory arguments are not provided but provided from default_args instead
  • Fixing type signature for _read method of WasbTaskHandler
  • Fixing type of execute method's context parameter as it currently violates (type wise) Liskov principle from SOLID, due to different signatures from base class

^ 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.

@github-actions github-actions bot added the okay to merge It's ok to merge this PR as it does not require more tests label Dec 19, 2021
@github-actions
Copy link

The PR is likely OK to be merged with just subset of tests for default Python and Database versions without running the full matrix of tests, because it does not modify the core of Airflow. If the committers decide that the full tests matrix is needed, they will add the label 'full tests needed'. Then you should rebase to the latest main or amend the last commit of the PR, and push it with --force-with-lease.

@potiuk
Copy link
Member

potiuk commented Dec 19, 2021

@khalidmamadov : There is one problem. Context does not exist in Airlfow 2.1 so you will have to change it to Any - until we decide to make providers depends on 2.3+

@khalidmammadov khalidmammadov force-pushed the mypy_providers_microsoft branch from 5e34032 to abec6c1 Compare December 19, 2021 23:45
@khalidmammadov
Copy link
Contributor Author

@khalidmamadov : There is one problem. Context does not exist in Airlfow 2.1 so you will have to change it to Any - until we decide to make providers depends on 2.3+

I see... let me fix

@josh-fell
Copy link
Contributor

josh-fell commented Dec 20, 2021

@potiuk Since some of the example DAGs (in this provider and several others) that have Mypy complaining about default_args, WDYT about adding to the logic when we render docs that ignores the # type: ignore... comments similar to what we do with the # START/END [...] markers so they don't show up in documentation? In this provider for example, we hide the Mypy ignore comment in "example_azure_blob_to_gcs.py".

@uranusjr
Copy link
Member

Context is a TypedDict, which naturally downcasts to Dict[str, Any].

@potiuk
Copy link
Member

potiuk commented Dec 20, 2021

Context is a TypedDict, which naturally downcasts to Dict[str, Any].

Oh yeah. that would be the best soluition - using Dict[str,Any] instead of Context should work also for 2.1 compatibility!

@potiuk
Copy link
Member

potiuk commented Dec 20, 2021

@potiuk Since some of the example DAGs (in this provider and several others) that have Mypy complaining about default_args, WDYT about adding to the logic when we render docs that ignores the # type: ignore... comments similar to what we do with the # START/END [...] markers so they don't show up in documentation? In this provider for example, we hide the Mypy ignore comment in "example_azure_blob_to_gcs.py".

Should be possible. I think for that we would have to write a little extra logic here:

class ExampleInclude(SphinxDirective):

@khalidmammadov
Copy link
Contributor Author

Context is a TypedDict, which naturally downcasts to Dict[str, Any].

I will try as you suggest

@potiuk
Copy link
Member

potiuk commented Dec 20, 2021

Context is a TypedDict, which naturally downcasts to Dict[str, Any].

I will try as you suggest

It will not work as exepected because of Liskov substitution principle.

airflow/providers/apache/hive/sensors/metastore_partition.py:70: error: Argument 1 of "poke" is incompatible with supertype "BaseSensorOperator"; supertype defines the argument type as "Context"
        def poke(self, context: MutableMapping[str, Any]) -> Any:
        ^
airflow/providers/apache/hive/sensors/metastore_partition.py:70: note: This violates the Liskov substitution principle
airflow/providers/apache/hive/sensors/metastore_partition.py:70: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides

But I think I found a nice "workaround":

In imports section of all operators/sensors:

if TYPE_CHECKING:
    from airflow.utils.context import Context
else:
    Context = MutableMapping[str, Any]

@potiuk
Copy link
Member

potiuk commented Dec 20, 2021

Example on how we can treat Context here: #20422

@khalidmammadov
Copy link
Contributor Author

Context is a TypedDict, which naturally downcasts to Dict[str, Any].

I will try as you suggest

It will not work as exepected because of Liskov substitution principle.

airflow/providers/apache/hive/sensors/metastore_partition.py:70: error: Argument 1 of "poke" is incompatible with supertype "BaseSensorOperator"; supertype defines the argument type as "Context"
        def poke(self, context: MutableMapping[str, Any]) -> Any:
        ^
airflow/providers/apache/hive/sensors/metastore_partition.py:70: note: This violates the Liskov substitution principle
airflow/providers/apache/hive/sensors/metastore_partition.py:70: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides

But I think I found a nice "workaround":

In imports section of all operators/sensors:

if TYPE_CHECKING:
    from airflow.utils.context import Context
else:
    Context = MutableMapping[str, Any]

This look good, will try this as well

@khalidmammadov khalidmammadov force-pushed the mypy_providers_microsoft branch from 733071d to 1e5940b Compare December 23, 2021 14:26
@khalidmammadov khalidmammadov mentioned this pull request Dec 23, 2021
10 tasks
# Mark closed so we don't double write if close is called twice
self.closed = True

def _read(self, ti, try_number: str, metadata: Optional[str] = None) -> Tuple[str, Dict[str, bool]]:
Copy link
Member

Choose a reason for hiding this comment

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

hmmmmmm :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there anything wrong here? didn't get :)

Copy link
Member

@potiuk potiuk left a comment

Choose a reason for hiding this comment

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

I wish we had better solution for the "default_args" but it must do for now :(

@potiuk
Copy link
Member

potiuk commented Dec 23, 2021

Some static checks :(

@khalidmammadov
Copy link
Contributor Author

Some static checks :(

Yes, pre-commit was disabled... re-enabled now and fixed

@potiuk
Copy link
Member

potiuk commented Dec 23, 2021

Some static checks :(

Yes, pre-commit was disabled... re-enabled now and fixed

BTW. You know you can disable just selected pre-commits @khalidmammadov ?

export SKIP="mypy"

Also git commit --no-verify will skip pre-commit just for this one commit without disabling it for the repo.

:D

@khalidmammadov
Copy link
Contributor Author

Some static checks :(

Yes, pre-commit was disabled... re-enabled now and fixed

BTW. You know you can disable just selected pre-commits @khalidmammadov ?

export SKIP="mypy"

Also git commit --no-verify will skip pre-commit just for this one commit without disabling it for the repo.

:D

Ah, didnt know that (looked up for something like that but didn't find..) thank for that!

@potiuk
Copy link
Member

potiuk commented Dec 23, 2021

Ah, didnt know that (looked up for something like that but didn't find..) thank for that!

You are most welcome. Pre-commit has a number of useful things built in - for example you can easily run pre-commit on the last commit only:

pre-commit run --from-ref=HEAD^ --to-ref=HEAD

Also see here: https://github.com/apache/airflow/blob/main/STATIC_CODE_CHECKS.rst#id1 "using pre-commit" for some other stuff.

@potiuk potiuk merged commit e63e23c into apache:main Dec 23, 2021
@khalidmammadov
Copy link
Contributor Author

Ah, didnt know that (looked up for something like that but didn't find..) thank for that!

You are most welcome. Pre-commit has a number of useful things built in - for example you can easily run pre-commit on the last commit only:

pre-commit run --from-ref=HEAD^ --to-ref=HEAD

Also see here: https://github.com/apache/airflow/blob/main/STATIC_CODE_CHECKS.rst#id1 "using pre-commit" for some other stuff.

Thanks for these, I will definitely will make use of these!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:logging area:providers okay to merge It's ok to merge this PR as it does not require more tests provider:microsoft-azure Azure-related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants