Skip to content

Add type hints to aws provider - #11531

Merged
mik-laj merged 4 commits into
apache:masterfrom
potix2:typing-aws-provider
Oct 22, 2020
Merged

mik-laj merged 4 commits into
apache:masterfrom
potix2:typing-aws-provider

Conversation

@potix2

@potix2 potix2 commented Oct 14, 2020 •

Copy link
Copy Markdown
Contributor

This PR is about increasing typing coverage for the aws provider. Part of: #9708
I added type hints to the rest modules of amazon provider.

The typing coverage of amazon provider becomes 77.7.

This PR relates to #11434 and #11612.


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

@boring-cyborg boring-cyborg Bot added area:logging provider:amazon AWS/Amazon - related issues labels Oct 14, 2020
@potix2 potix2 mentioned this pull request Oct 14, 2020
@github-actions

Copy link
Copy Markdown
Contributor

The Workflow run is cancelling this PR. It has some failed jobs matching ^Pylint$,^Static checks$,^Build docs$,^Spell check docs$,^Backport packages$,^Checks: Helm tests$,^Test OpenAPI*.

@github-actions

Copy link
Copy Markdown
Contributor

The Workflow run is cancelling this PR. It has some failed jobs matching ^Pylint$,^Static checks$,^Build docs$,^Spell check docs$,^Backport packages$,^Checks: Helm tests$,^Test OpenAPI*.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We cannot pass None as the argument parameters. The API document is here.

I checked the behavior of submit_job with the following code.

#!/usr/bin/env python

import boto3
import json

cl = boto3.client('batch')
ap=None
params=None
overrides=None
resp = cl.submit_job(
        jobName='myjob-1',
        jobQueue='test-batch-job-queue',
        jobDefinition='test',
        arrayProperties=ap,
        parameters=params,
        containerOverrides=overrides,
        )
print(json.dumps(resp, indent=4, sort_keys=True))

Result:

Traceback (most recent call last):
  File "main.py", line 17, in <module>
    containerOverrides=overrides,
  File "/Users/potix2/.pyenv/versions/3.7.5/lib/python3.7/site-packages/botocore/client.py", line 357, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/Users/potix2/.pyenv/versions/3.7.5/lib/python3.7/site-packages/botocore/client.py", line 649, in _make_api_call
    api_params, operation_model, context=request_context)
  File "/Users/potix2/.pyenv/versions/3.7.5/lib/python3.7/site-packages/botocore/client.py", line 697, in _convert_to_request_dict
    api_params, operation_model)
  File "/Users/potix2/.pyenv/versions/3.7.5/lib/python3.7/site-packages/botocore/validate.py", line 297, in serialize_to_request
    raise ParamValidationError(report=report.generate_report())
botocore.exceptions.ParamValidationError: Parameter validation failed:
Invalid type for parameter arrayProperties, value: None, type: <class 'NoneType'>, valid types: <class 'dict'>
Invalid type for parameter parameters, value: None, type: <class 'NoneType'>, valid types: <class 'dict'>
Invalid type for parameter containerOverrides, value: None, type: <class 'NoneType'>, valid types: <class 'dict'>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

script_arguments is passed to Glue.Client.start_job_run as Arguments whose type is dict.
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/glue.html#Glue.Client.start_job_run

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

job_description must not be None because its type is not Optional[T].

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

To avoid deeply nested if-statements I rearrange the order of code.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

argmin's type is Optional[T] so we need to pass the default value.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's too hard to add type hint to positions.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

self.hook is initialized by None so its type is Optional[AWSDataSyncHook]. To eliminate the possibility that get_hook returns None, we need to rearrange the order of evaluation.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This returned value is not used anywhere and _create_datasync_task is private method.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This returned value is not used anywhere also.

Copy link
Copy Markdown
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 also.

Copy link
Copy Markdown
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 also.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fix typo

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This attribute is directly referenced from child classes. To avoid None I defined it as cached_property.

@potix2
potix2 marked this pull request as ready for review October 16, 2020 00:38
@potix2
potix2 force-pushed the typing-aws-provider branch 2 times, most recently from fa65b9e to 02b2991 Compare October 17, 2020 05:23
@potix2

potix2 commented Oct 17, 2020

Copy link
Copy Markdown
Contributor Author

I checked failed tests in local but it's successful. I try to trigger actions in CI.

@mik-laj
mik-laj requested a review from feluelle October 19, 2020 13:01
@mik-laj

mik-laj commented Oct 19, 2020

Copy link
Copy Markdown
Member

@mlgruby Can you look at it?

@mlgruby

mlgruby commented Oct 20, 2020

Copy link
Copy Markdown
Contributor

Yup I can have a look

@mlgruby mlgruby left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice work @potix2 , learned few things (that chached_property one) while reviewing

Thanks

@mlgruby mlgruby Oct 20, 2020 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This if is because of None default value of hook and hence mypy complaining for it?
I have seen several other places the same new if statement.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes. get_hook() returns an actual value, but in the original form mypy hence type error.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Well, I liked this one :)

@potix2 potix2 Oct 20, 2020 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Me,too. I would like to rewrite all get_hook() to this way but I think it's out of scope for this work, so I keep the original form.

@mik-laj

mik-laj commented Oct 21, 2020

Copy link
Copy Markdown
Member

Can you do a rebase? This is the only thing blocking me from merging this change.

@potix2
potix2 force-pushed the typing-aws-provider branch from be6bf52 to 17c2dfd Compare October 21, 2020 14:00
@potix2

potix2 commented Oct 21, 2020

Copy link
Copy Markdown
Contributor Author

I rebased but CI looks like hung up... After a few hours I'll try it again.

@mik-laj

mik-laj commented Oct 21, 2020 •

Copy link
Copy Markdown
Member

I rebased but CI looks like hung up... After a few hours I'll try it again.

Everything is green with me. Should I merge it or would you like to make any more changes?
Screenshot 2020-10-21 at 21 25 21

@potix2

potix2 commented Oct 21, 2020

Copy link
Copy Markdown
Contributor Author

@mik-laj It's ready for merging. I overlooked that static checking was successful because CI Build / Static checks: basic checks only was skipped.

@mik-laj
mik-laj merged commit b9d677c into apache:master Oct 22, 2020
michalmisiewicz pushed a commit to michalmisiewicz/airflow that referenced this pull request Oct 30, 2020
* Added type hints to aws provider

* Update airflow/providers/amazon/aws/log/s3_task_handler.py

* Fix expectation for submit_job

* Fix documentation

Co-authored-by: Kamil Breguła <mik-laj@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:logging provider:amazon AWS/Amazon - related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants