Skip to content

Commit

Permalink
Update contributing guide to use typed session arg (#20181)
Browse files Browse the repository at this point in the history
  • Loading branch information
uranusjr authored Dec 9, 2021
1 parent 82102e6 commit 64bb592
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -950,14 +950,16 @@ Database Session Handling
**Explicit is better than implicit.** If a function accepts a ``session`` parameter it should not commit the
transaction itself. Session management is up to the caller.

To make this easier there is the ``create_session`` helper:
To make this easier, there is the ``create_session`` helper:

.. code-block:: python
from sqlalchemy.orm import Session
from airflow.utils.session import create_session
def my_call(*args, session):
def my_call(*args, session: Session):
...
# You MUST not commit the session here.
Expand All @@ -969,14 +971,23 @@ If this function is designed to be called by "end-users" (i.e. DAG authors) then

.. code-block:: python
from airflow.utils.session import provide_session
from sqlalchemy.orm import Session
from airflow.utils.session import NEW_SESSION, provide_session
@provide_session
def my_method(arg, session=None):
def my_method(arg, *, session: Session = NEW_SESSION):
...
# You SHOULD not commit the session here. The wrapper will take care of commit()/rollback() if exception
In both cases, the ``session`` argument is a `keyword-only argument`_. This is the most preferred form if
possible, although there are some exceptions in the code base where this cannot be used, due to backward
compatibility considerations. In most cases, ``session`` argument should be last in the argument list.

.. _`keyword-only argument`: https://www.python.org/dev/peps/pep-3102/


Don't use time() for duration calculations
-----------------------------------------

Expand Down

0 comments on commit 64bb592

Please sign in to comment.