Skip to content
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

Helper for provide_session-decorated functions #20104

Merged
merged 2 commits into from
Dec 7, 2021

Conversation

uranusjr
Copy link
Member

@uranusjr uranusjr commented Dec 7, 2021

This can be used like this:

@provide_session
def some_function(arg1, arg2, session: Session = NEW_SESSION) -> None:
    session.query(Model)...

This eliminates the need to use Optional[Session] = None to reduce boilerplate code.

cc @potiuk this can be useful for your #19914.

@potiuk
Copy link
Member

potiuk commented Dec 7, 2021

OH NICE!

@github-actions
Copy link

github-actions bot commented Dec 7, 2021

The PR most likely needs to run full matrix of tests because it modifies parts of the core of Airflow. However, committers might decide to merge it quickly and take the risk. If they don't merge it quickly - please rebase it to the latest main at your convenience, or amend the last commit of the PR, and push it with --force-with-lease.

@github-actions github-actions bot added the full tests needed We need to run full set of tests for this PR to merge label Dec 7, 2021
@@ -77,7 +80,7 @@
DAGS_FOLDER: str = os.path.expanduser(conf.get('core', 'DAGS_FOLDER'))

engine: Optional[Engine] = None
Session: Optional[SASession] = None
Session: Callable[..., SASession]
Copy link
Member

Choose a reason for hiding this comment

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

This will conflict with #20000 (and also , the SessionMaker is both callable, and behaves like an actual Session. SQLA is weird like that)

Copy link
Member

Choose a reason for hiding this comment

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

This might be fine though

Copy link
Member Author

Choose a reason for hiding this comment

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

Let me see if I can just make this Session: SessionMaker

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm no, SQLAlchemy is not type hinted well enough, annotating this as sessionmaker would make Session() return Any. I’ll keep this for now since this is more useful in general.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh actually :)

Copy link
Member

Choose a reason for hiding this comment

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

This is probably fine as it is (and it's how we use it in Airflow)

@ashb ashb merged commit a80ac1e into apache:main Dec 7, 2021
@uranusjr uranusjr deleted the default-session branch December 7, 2021 15:07
@jedcunningham jedcunningham added this to the Airflow 2.2.4 milestone Jan 21, 2022
@jedcunningham jedcunningham added the changelog:skip Changes that should be skipped from the changelog (CI, tests, etc..) label Jan 21, 2022
jedcunningham pushed a commit that referenced this pull request Jan 27, 2022
* Helper for provide_session-decorated functions

* Apply NEW_SESSION trick on XCom

(cherry picked from commit a80ac1e)
jedcunningham pushed a commit that referenced this pull request Jan 28, 2022
* Helper for provide_session-decorated functions

* Apply NEW_SESSION trick on XCom

(cherry picked from commit a80ac1e)
jedcunningham pushed a commit that referenced this pull request Feb 17, 2022
* Helper for provide_session-decorated functions

* Apply NEW_SESSION trick on XCom

(cherry picked from commit a80ac1e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog:skip Changes that should be skipped from the changelog (CI, tests, etc..) full tests needed We need to run full set of tests for this PR to merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants