-
Notifications
You must be signed in to change notification settings - Fork 15.3k
Drop redundant project id null checks. #8145
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
Conversation
01499ea
to
37c79f6
Compare
@jmcarp those checks are only to please mypy. Initially, we used |
Just thinking - maybe we can do it differently and find a way to both - keep mypy happy and remove the ifs... Let me think about it a bit :) |
Some more would not hurt :) |
Sure, would be awesome to remove it. But typehints with decorators are tricky :) |
Hold my 🍺 and 🐱 |
Are you going to propose PEP to make Python statically typed language? 😅 |
Yep. That's my secret plan. Just started to write it :) |
37c79f6
to
325f379
Compare
I added a simple mypy plugin to preserve types for decorated functions, and to update types when decorators change function signatures. For example, By the way, mypy might support this kind of behavior natively at some point, but this issue has been open for a long time: python/mypy#3157. |
8b41a5a
to
988afe8
Compare
I have mixed feelings... First, I am not a big fan of adding more and more custom plugins (especially to solve problems that should be solved on language level #static_types_fan) . Especially to solve problems that I am not even sure are real problems. Since we have the On the other hand, this change does no harm. It makes our codebase bigger and adds next moving part to it. So, I am not sure. I am curious about what others think? @mik-laj @kaxil @ashb @potiuk |
Quick thought: The same issue about project_id also applies to session/provide_session |
I was also hesitant about writing a mypy plugin, even a simple one, but as far as I can tell, it's the only way to write decorators that don't produce callables with type |
aae4fb9
to
e5ece17
Compare
One more thing: one of the mypy developers suggests using mypy plugins to preserve types for decorated functions until mypy supports this kind of thing: python/mypy#1927 (comment). |
As well as any other decorator that will inject arguments. @jmcarp will we be able to somehow check if all decorators that require it use this plugin? |
4eced5c
to
1161ea6
Compare
6f29451
to
c0983b4
Compare
I don't know if we can programmatically audit use of the plugin, but the number of decorators is small and shouldn't change often, and it's simple enough to check by hand by grepping for |
c0983b4
to
0f6020a
Compare
@@ -139,7 +139,6 @@ def get_target_column_spec(columns_specs: List[Dict], column_name: str) -> str: | |||
task_id="update_dataset_task", | |||
dataset=update, | |||
location=GCP_AUTOML_LOCATION, | |||
project_id=GCP_PROJECT_ID, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this change necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. I think we should leave that in.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This parameter was unused in CloudAutoMLHook.update_dataset
, so I dropped the parameter from that method, as well as AutoMLTablesUpdateDatasetOperator
and the example code here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. I thik it's good :)
@@ -54,7 +54,7 @@ def _get_client(self, project_id: str): | |||
return self._client | |||
|
|||
@GoogleBaseHook.fallback_to_default_project_id | |||
def get_instance(self, instance_id: str, project_id: Optional[str] = None) -> Instance: | |||
def get_instance(self, instance_id: str, project_id: str) -> Instance: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's decide what we want to use: str
or Optional[str] = None
and be cosistent
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. Agree. project_id: str is better IMHO. it is semantically correct - we expect project_id to be set. And it is used most of the time below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jmcarp I checked the changes and in some places where we use the decorator we are using project_id: str
and in others project_id: Optional[str] = None
. I think we should be consistent :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Few small thigs. I like it :)
@@ -139,7 +139,6 @@ def get_target_column_spec(columns_specs: List[Dict], column_name: str) -> str: | |||
task_id="update_dataset_task", | |||
dataset=update, | |||
location=GCP_AUTOML_LOCATION, | |||
project_id=GCP_PROJECT_ID, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. I think we should leave that in.
@@ -54,7 +54,7 @@ def _get_client(self, project_id: str): | |||
return self._client | |||
|
|||
@GoogleBaseHook.fallback_to_default_project_id | |||
def get_instance(self, instance_id: str, project_id: Optional[str] = None) -> Instance: | |||
def get_instance(self, instance_id: str, project_id: str) -> Instance: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. Agree. project_id: str is better IMHO. it is semantically correct - we expect project_id to be set. And it is used most of the time below.
0f6020a
to
e0f61d7
Compare
If you would like to add support for more decorators, the most popular decorator is `apply_defaullts. However, this will be best done in a separate PR. This is very problematic and has made it difficult for me to introduce metaclasses - #7450 |
@turbaszek @mik-laj -> looks good to me. Can you take another look? |
Codecov Report
@@ Coverage Diff @@
## master #8145 +/- ##
==========================================
- Coverage 88.33% 87.93% -0.41%
==========================================
Files 936 937 +1
Lines 45321 45197 -124
==========================================
- Hits 40036 39743 -293
- Misses 5285 5454 +169
Continue to review full report at Codecov.
|
I still have concerns about type annotations consistency: |
e0f61d7
to
f8d4c4b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
fde9de9
to
81e549a
Compare
Thanks for reviewing. Is it all right to merge while |
e032024
to
535efb5
Compare
* Preserve types for decorators that don't change signatures * Augment types for decorators that change signatures * Drop redundant type checks
535efb5
to
f33e29e
Compare
Thanks @jmcarp! Great change! |
@jmcarp do you plan to work on |
The
fallback_to_default_project_id
method already raises an exceptionif both
self.project_id
andkwargs["project_id"]
are missing, soskip redundant null checks in hook methods.
Make sure to mark the boxes below before creating PR: [x]
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.
Read the Pull Request Guidelines for more information.