You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "Prab-27 (via GitHub)" <gi...@apache.org> on 2025/10/30 14:40:03 UTC

[PR] Fix MyPy type errors in providers in cncf/kubernetes provider [airflow]

Prab-27 opened a new pull request, #57563:
URL: https://github.com/apache/airflow/pull/57563

   related : #56738
   
   <!--
    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License.  You may obtain a copy of the License at
   
      http://www.apache.org/licenses/LICENSE-2.0
   
    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.
    -->
   
   <!--
   Thank you for contributing! Please make sure that your code changes
   are covered with tests. And in case of new features or big changes
   remember to adjust the documentation.
   
   Feel free to ping committers for the review!
   
   In case of an existing issue, reference it using one of the following:
   
   closes: #ISSUE
   related: #ISSUE
   
   How to write a good git commit message:
   http://chris.beams.io/posts/git-commit/
   -->
   
   
   
   <!-- Please keep an empty line above the dashes. -->
   ---
   **^ Add meaningful description above**
   Read the **[Pull Request Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)** for more information.
   In case of fundamental code changes, an Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals)) is needed.
   In case of a new dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
   In case of backwards incompatible changes please leave a note in a newsfragment file, named `{pr_number}.significant.rst` or `{issue_number}.significant.rst`, in [airflow-core/newsfragments](https://github.com/apache/airflow/tree/main/airflow-core/newsfragments).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix MyPy type errors in providers in cncf/kubernetes provider [airflow]

Posted by "vincbeck (via GitHub)" <gi...@apache.org>.
vincbeck commented on code in PR #57563:
URL: https://github.com/apache/airflow/pull/57563#discussion_r2487204514


##########
providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/executors/kubernetes_executor.py:
##########
@@ -569,7 +574,7 @@ def try_adopt_task_instances(self, tis: Sequence[TaskInstance]) -> Sequence[Task
             tis_to_flush_by_key = {ti.key: ti for ti in tis if ti.queued_by_job_id}
             kube_client: client.CoreV1Api = self.kube_client
             for scheduler_job_id in scheduler_job_ids:
-                scheduler_job_id = self._make_safe_label_value(str(scheduler_job_id))
+                scheduler_job_id = cast("int", self._make_safe_label_value(str(scheduler_job_id)))

Review Comment:
   Thanks @o-nikolas :) @Prab-27 can you do that?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix MyPy type errors in providers in cncf/kubernetes provider [airflow]

Posted by "vincbeck (via GitHub)" <gi...@apache.org>.
vincbeck commented on code in PR #57563:
URL: https://github.com/apache/airflow/pull/57563#discussion_r2478421887


##########
providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/executors/kubernetes_executor.py:
##########
@@ -569,7 +574,7 @@ def try_adopt_task_instances(self, tis: Sequence[TaskInstance]) -> Sequence[Task
             tis_to_flush_by_key = {ti.key: ti for ti in tis if ti.queued_by_job_id}
             kube_client: client.CoreV1Api = self.kube_client
             for scheduler_job_id in scheduler_job_ids:
-                scheduler_job_id = self._make_safe_label_value(str(scheduler_job_id))
+                scheduler_job_id = cast("int", self._make_safe_label_value(str(scheduler_job_id)))

Review Comment:
   `_make_safe_label_value` returns a str and we cast it in int? Do you know why?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix MyPy type errors in providers in cncf/kubernetes provider [airflow]

Posted by "Prab-27 (via GitHub)" <gi...@apache.org>.
Prab-27 commented on code in PR #57563:
URL: https://github.com/apache/airflow/pull/57563#discussion_r2487632788


##########
providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/executors/kubernetes_executor.py:
##########
@@ -569,7 +574,7 @@ def try_adopt_task_instances(self, tis: Sequence[TaskInstance]) -> Sequence[Task
             tis_to_flush_by_key = {ti.key: ti for ti in tis if ti.queued_by_job_id}
             kube_client: client.CoreV1Api = self.kube_client
             for scheduler_job_id in scheduler_job_ids:
-                scheduler_job_id = self._make_safe_label_value(str(scheduler_job_id))
+                scheduler_job_id = cast("int", self._make_safe_label_value(str(scheduler_job_id)))

Review Comment:
   I’ll make sure it’s done tomorrow without delay



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix MyPy type errors in providers in cncf/kubernetes provider [airflow]

Posted by "o-nikolas (via GitHub)" <gi...@apache.org>.
o-nikolas commented on code in PR #57563:
URL: https://github.com/apache/airflow/pull/57563#discussion_r2487198639


##########
providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/executors/kubernetes_executor.py:
##########
@@ -569,7 +574,7 @@ def try_adopt_task_instances(self, tis: Sequence[TaskInstance]) -> Sequence[Task
             tis_to_flush_by_key = {ti.key: ti for ti in tis if ti.queued_by_job_id}
             kube_client: client.CoreV1Api = self.kube_client
             for scheduler_job_id in scheduler_job_ids:
-                scheduler_job_id = self._make_safe_label_value(str(scheduler_job_id))
+                scheduler_job_id = cast("int", self._make_safe_label_value(str(scheduler_job_id)))

Review Comment:
   When I touched this code it was just to reorganize it. It was still doing the same logic (with string casting described below).
   
   We want to make a K8s pod label out of the scheduler job id. The job ID is an int and the labels are all strings so we cast the job id to a str (that existed before this PR) and pass it to the string/label sanitizer. Then the result should be a string and we then add the result inside of another string with string f-formatting.
   
   The casting and resulting types are all correct, the issue is that the new string result is being assigned back to the same variable `scheduler_job_id` which is an int type (it's the original carrier of the int job id from the for loop). So the result assignment from `self._make_safe_label_value` should be assigned to a new variable (maybe `_scheduler_job_id` or `scheduler_job_id_safe_label`) and then that used in the string formatting. Then MyPy should be happy.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix MyPy type errors in providers in cncf/kubernetes provider [airflow]

Posted by "vincbeck (via GitHub)" <gi...@apache.org>.
vincbeck commented on code in PR #57563:
URL: https://github.com/apache/airflow/pull/57563#discussion_r2481322527


##########
providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/executors/kubernetes_executor.py:
##########
@@ -569,7 +574,7 @@ def try_adopt_task_instances(self, tis: Sequence[TaskInstance]) -> Sequence[Task
             tis_to_flush_by_key = {ti.key: ti for ti in tis if ti.queued_by_job_id}
             kube_client: client.CoreV1Api = self.kube_client
             for scheduler_job_id in scheduler_job_ids:
-                scheduler_job_id = self._make_safe_label_value(str(scheduler_job_id))
+                scheduler_job_id = cast("int", self._make_safe_label_value(str(scheduler_job_id)))

Review Comment:
   Yeah but we need to understand what is going on. We cannot just cast the string in int because mypy says so. @o-nikolas (you're the last one who modified this line, sorry haha). Do you know why we pass `scheduler_job_id` (which is an int) to `_make_safe_label_value` which seems to be a function to secure a string?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix MyPy type errors in providers in cncf/kubernetes provider [airflow]

Posted by "vincbeck (via GitHub)" <gi...@apache.org>.
vincbeck merged PR #57563:
URL: https://github.com/apache/airflow/pull/57563


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix MyPy type errors in providers in cncf/kubernetes provider [airflow]

Posted by "Prab-27 (via GitHub)" <gi...@apache.org>.
Prab-27 commented on PR #57563:
URL: https://github.com/apache/airflow/pull/57563#issuecomment-3468402105

   While resolving the conflicts, I accidentally committed vincbecks's changes. 
   That's why I made another commit to recover mine.
   If this isn't fine, I'm happy to raise a separate PR.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix MyPy type errors in providers in cncf/kubernetes provider [airflow]

Posted by "Prab-27 (via GitHub)" <gi...@apache.org>.
Prab-27 commented on code in PR #57563:
URL: https://github.com/apache/airflow/pull/57563#discussion_r2480317107


##########
providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/executors/kubernetes_executor.py:
##########
@@ -569,7 +574,7 @@ def try_adopt_task_instances(self, tis: Sequence[TaskInstance]) -> Sequence[Task
             tis_to_flush_by_key = {ti.key: ti for ti in tis if ti.queued_by_job_id}
             kube_client: client.CoreV1Api = self.kube_client
             for scheduler_job_id in scheduler_job_ids:
-                scheduler_job_id = self._make_safe_label_value(str(scheduler_job_id))
+                scheduler_job_id = cast("int", self._make_safe_label_value(str(scheduler_job_id)))

Review Comment:
   @vincbeck Thanks ! 
   
   without casting this  
   ```
   def try_adopt_task_instances(self, tis: Sequence[TaskInstance]) -> Sequence[TaskInstance]:
           with Stats.timer("kubernetes_executor.adopt_task_instances.duration"):
               # Always flush TIs without queued_by_job_id
               tis_to_flush = [ti for ti in tis if not ti.queued_by_job_id]
               scheduler_job_ids = {ti.queued_by_job_id for ti in tis}
               tis_to_flush_by_key = {ti.key: ti for ti in tis if ti.queued_by_job_id}
               kube_client: client.CoreV1Api = self.kube_client
               for scheduler_job_id in scheduler_job_ids:
                   scheduler_job_id = self._make_safe_label_value(str(scheduler_job_id))
   ```
   
   shows
   ```
    providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/executors/kubernetes_executor.py:577: error:
     Incompatible types in assignment (expression has type "str", variable has type
     "int | None")  [assignment]
                         scheduler_job_id = self._make_safe_label_value(str(sch...
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix MyPy type errors in providers in cncf/kubernetes provider [airflow]

Posted by "Prab-27 (via GitHub)" <gi...@apache.org>.
Prab-27 commented on code in PR #57563:
URL: https://github.com/apache/airflow/pull/57563#discussion_r2480317107


##########
providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/executors/kubernetes_executor.py:
##########
@@ -569,7 +574,7 @@ def try_adopt_task_instances(self, tis: Sequence[TaskInstance]) -> Sequence[Task
             tis_to_flush_by_key = {ti.key: ti for ti in tis if ti.queued_by_job_id}
             kube_client: client.CoreV1Api = self.kube_client
             for scheduler_job_id in scheduler_job_ids:
-                scheduler_job_id = self._make_safe_label_value(str(scheduler_job_id))
+                scheduler_job_id = cast("int", self._make_safe_label_value(str(scheduler_job_id)))

Review Comment:
   here , 
   ```
   def try_adopt_task_instances(self, tis: Sequence[TaskInstance]) -> Sequence[TaskInstance]:
           with Stats.timer("kubernetes_executor.adopt_task_instances.duration"):
               # Always flush TIs without queued_by_job_id
               tis_to_flush = [ti for ti in tis if not ti.queued_by_job_id]
               scheduler_job_ids = {ti.queued_by_job_id for ti in tis}
               tis_to_flush_by_key = {ti.key: ti for ti in tis if ti.queued_by_job_id}
               kube_client: client.CoreV1Api = self.kube_client
               for scheduler_job_id in scheduler_job_ids:
                   scheduler_job_id = self._make_safe_label_value(str(scheduler_job_id))
   ```
   
   shows
   ```
    providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/executors/kubernetes_executor.py:577: error:
     Incompatible types in assignment (expression has type "str", variable has type
     "int | None")  [assignment]
                         scheduler_job_id = self._make_safe_label_value(str(sch...
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org