Skip to content

Commit

Permalink
provide_session keep return type (#9787)
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 514eb6d1e350818b31dca5adeaec2d7fd32b23ee
  • Loading branch information
mik-laj authored and Cloud Composer Team committed Sep 12, 2024
1 parent 3993ca8 commit 99a6f59
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion airflow/api_connexion/schemas/pool_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def get_queued_slots(obj: Pool) -> int:
return obj.queued_slots()

@staticmethod
def get_open_slots(obj: Pool) -> int:
def get_open_slots(obj: Pool) -> float:
"""
Returns the open slots of the pool.
"""
Expand Down
16 changes: 9 additions & 7 deletions airflow/jobs/backfill_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import time
from collections import OrderedDict
from datetime import datetime
from typing import Set
from typing import Optional, Set

from sqlalchemy.orm.session import Session, make_transient
from tabulate import tabulate
Expand Down Expand Up @@ -295,12 +295,14 @@ def _get_dag_run(self, run_date: datetime, dag: DAG, session: Session = None):

# check if we are scheduling on top of a already existing dag_run
# we could find a "scheduled" run instead of a "backfill"
run = DagRun.find(dag_id=dag.dag_id,
execution_date=run_date,
session=session)

if run is not None and len(run) > 0:
run = run[0]
runs = DagRun.find(
dag_id=dag.dag_id,
execution_date=run_date,
session=session
)
run: Optional[DagRun]
if runs:
run = runs[0]
if run.state == State.RUNNING:
respect_dag_max_active_limit = False
else:
Expand Down
8 changes: 6 additions & 2 deletions airflow/utils/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import contextlib
from functools import wraps
from typing import Callable, TypeVar

from airflow import settings

Expand All @@ -37,15 +38,18 @@ def create_session():
session.close()


def provide_session(func):
RT = TypeVar("RT") # pylint: disable=invalid-name


def provide_session(func: Callable[..., RT]) -> Callable[..., RT]:
"""
Function decorator that provides a session if it isn't provided.
If you want to reuse a session or run the function as part of a
database transaction, you pass it to the function, if not this wrapper
will create one and close it for you.
"""
@wraps(func)
def wrapper(*args, **kwargs):
def wrapper(*args, **kwargs) -> RT:
arg_session = 'session'

func_params = func.__code__.co_varnames
Expand Down

0 comments on commit 99a6f59

Please sign in to comment.