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

1065/enhancement/add ctx to __init__.py #1072

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Spaarsh
Copy link
Contributor

@Spaarsh Spaarsh commented Mar 17, 2025

Which issue does this PR close?

Closes #1065

Rationale for this change

To improve ergonomics of the API by providing a pre-initialized SessionContext instance as ctx. This allows users to import and use a context directly without initialization.

What changes are included in this PR?

  • Added ctx = SessionContext() in __init__.py to provide a default context instance
  • Added corresponding test test_default_ctx_instance to verify functionality
  • Included ctx in __all__ for explicit exports

Are there any user-facing changes?

Yes, users can now do:

from datafusion import ctx # Get pre-initialized context

Instead of:

from datafusion import SessionContext
ctx = SessionContext()  # Manual initialization

It adds a new API for the users.

@deanm0000
Copy link

To address @timsaucer's point from the issue, what about doing

ctx = SessionContext.global_ctx()

instead of just ctx=SessionContext().

I don't really know the internals so I don't know what the difference is but I did find https://github.com/apache/datafusion-python/pull/1060/files which makes that available.

@timsaucer
Copy link
Contributor

My question was more about ergonomics - now that we have the global context I suspect most users don't even need to import at all. In your use case, do you find you still need it?

@deanm0000
Copy link

I see that the other PR makes functions for the file readers at the module level but AFAIK, there are still utilities only available through ctx. For example, we still have to do ctx.from_pydict rather than just from_pydict. I'm personally indifferent between having all those functions available from a module rather than an instantiated class or to have an instance of the initialized class available to import.

@timsaucer
Copy link
Contributor

I recommend closing this PR or at lest changing it to:

global_ctx = SessionContext.global_ctx()

That I think will also help with making sure the naming is consistent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add ctx = SessionContext() to __init__
3 participants