Repository navigation
Open
Labels
Description
Use case
pyathena/filesystem/ imports from the rest of PyAthena, so it cannot be used or released on its own, and changes to the core modules can affect it.
#1363 removed the runtime dependency on Connection and named a possible extraction of the filesystem into its own package as a goal.
This issue makes the dependency one-way: the filesystem imports nothing from PyAthena outside pyathena/filesystem/, and PyAthena depends on the filesystem.
Whether to publish the filesystem as a separate distribution is out of scope and can be decided afterwards.
Current dependencies
After #1378 (and #1386, which removes connection=), the filesystem uses these modules from outside its package:
| Dependency | Use |
|---|---|
pyathena.connection.Connection |
Type annotation only (TYPE_CHECKING), for connection= and pyathena.filesystem.s3_filesystem() |
pyathena.user_agent_extra |
The botocore user agent of the clients the filesystem builds (pyathena/filesystem/s3.py) |
pyathena.util.override |
Marking overrides |
pyathena.util.RetryConfig, retry_api_call, stop_retries_on, unstoppable_retries |
S3 API retries and the retry stop scopes |
pyathena.aio.util.create_in_thread |
Creating AioS3FileSystem resources in a thread |
Proposed change
- Move
s3_filesystem()to PyAthena's side of the boundary, in a module outsidepyathena/filesystem/that the pandas, Polars and s3fs result sets import.
It keeps the property agreed in Replace the filesystem's connection= with client= and retry_config= injection #1363:import pyathenadoes not load fsspec or the filesystem modules.
Keeppyathena.filesystem.s3_filesystemimportable until the next major, or move it in 5.0.0 together with Remove the filesystem's connection= and pass client= to custom s3fs filesystem classes #1386. - Give the filesystem its own user-agent value and
override. - Give the filesystem its own retry policy and retry stop scopes, and convert at the boundary.
- The filesystem owns a retry configuration class (for example
S3RetryConfig), its retry function, and its own stop-scopeContextVarwithstop_retries_on()andunstoppable_retries().
It does not need the Athena-specific parts ofpyathena.util.retry_api_call, such as unwrapping Glue errors fromMetadataException. s3_filesystem()convertsconnection.retry_config(exceptions,attempt,multiplier,max_delay,exponential_base) into the filesystem's configuration.- The cursors and the filesystem currently share one stop scope (
_RETRY_STOP_EVENTSinpyathena/util.py).
For example, the Arrow reader reads files through the filesystem insidestop_retries_on(), so closing the cursor stops the S3 retries.
To keep that link,pyathena.util.stop_retries_on()andunstoppable_retries()also enter the filesystem's corresponding scopes; the dependency points from PyAthena to the filesystem, as the boundary allows.
Both variables reach worker threads throughcontextvars.copy_context()andasyncio.to_thread()as the current one does.
wait_with_retry_stop()relays only PyAthena's scopes; the filesystem's executor relays its own. - The filesystem keeps its own copy of the thread helpers it uses from
pyathena/aio/util.py, such ascreate_in_thread(). import pyathenamust still not load fsspec oncepyathena.utilimports the filesystem's retry module.
pyathena/filesystem/__init__.pyimportsS3FileSystemand fsspec, so either make its imports lazy or place the retry module where importing it does not run that__init__.py.
Entering the filesystem's scope only when its module is already loaded is not enough: a filesystem imported inside an open scope would not see the scope.- Item 7 of Replace the filesystem's connection= with client= and retry_config= injection #1363 (
RetryConfigstays inpyathena.util) still holds; PyAthena's retry module does not move.
- The filesystem owns a retry configuration class (for example
- Enforce the boundary with a lint rule or a test that fails when a module in
pyathena/filesystem/imports from PyAthena outside the package. - Tests: the filesystem tests create filesystems through
s3_filesystem(connect()).
Decide whether they keep using a connection (acceptable while the code stays in one repository) or build clients from the test environment directly.
Decisions needed before implementing:
- How
import pyathenaavoids loading fsspec (item 3): lazy imports inpyathena/filesystem/__init__.py, or the location of the filesystem's retry module. - Default error codes of the filesystem's retry policy.
The defaultRetryConfigretriesThrottlingExceptionandTooManyRequestsException, which S3 does not return; S3 throttles withSlowDown, which only botocore's own retries handle today.
A separate policy allows an S3-specific default, which changes the default behavior and needs release notes. - Whether this lands in a minor release, or in 5.0.0.
Validation plan (if implementing)
just lint, including the boundary check from item 4.- The filesystem, Arrow, pandas, Polars, and s3fs tests, including the tests that close a cursor during a read to confirm that the retry stop scopes still stop filesystem retries.
import pyathenadoes not importfsspec(checked throughsys.modules).- A unit test that a stop scope opened through
pyathena.util.stop_retries_on()stops the filesystem's retries, in the same thread and in a thread started with a copy of the context.
Activity
laughingman7743 commentedon Nov 21, 2026
Decisions for the implementation, agreed on 2026-11-21. The change targets a 4.x minor release and breaks nothing, so the issue leaves the 5.0.0 milestone.
Current dependencies (master 703aaad; the table in the description is outdated):
pyathena.util.overridepyathena.util.RetryConfig,retry_api_call,_with_exceptions,THROTTLING_ERROR_CODESs3_core(s3,s3_asyncfor annotations)pyathena.util.stop_retries_ons3_executor,s3_writerpyathena.util.unstoppable_retriess3,s3_corepyathena.util.retries_stoppeds3_asyncpyathena.util.raise_if_stoppeds3,s3_writerpyathena.aio.util.create_in_thread(enterspyathena.util's stop scopes)s3_asyncpyathena.user_agent_extra(at run time)s3pyathena.connection.Connection(TYPE_CHECKINGonly)__init__,s3,s3_asyncDecisions:
stop_retries_on(),unstoppable_retries(),retries_stopped()andraise_if_stopped(), its own retry function (without the GlueMetadataExceptionunwrapping), and its own copy ofcreate_in_thread().pyathena.util.stop_retries_on()andunstoppable_retries()also enter the filesystem's scopes; the filesystem's internal scopes (executor batches, writers) enter only its own.pyathena.util.retry_api_call()andwait_with_retry_stop()are unchanged. No PyAthena retry runs inside a filesystem-only scope today, so the behavior does not change.import pyathenamay load fsspec. The property came from the rationale of Replace the filesystem's connection= with client= and retry_config= injection #1363 item 2; no test or document states it. Loadingpyathena.filesystemafter boto3 costs about 25 ms and 59 modules.pyathena.utilimports the filesystem's retry module at the top of the module;pyathena/filesystem/__init__.pystays eager.s3_filesystem()moves topyathena.fs.pyathena.filesystem.s3_filesystem(public since 4.3.0) stays as a copy that uses only the connection's attributes, so the boundary needs no exception; it emits aDeprecationWarningand is removed in 5.0.0 together with Remove the filesystem's connection= and pass client= to custom s3fs filesystem classes #1386.S3FileSystem(retry_config=pyathena.util.RetryConfig(...))is public, so the filesystem accepts any object with the five attributes ofRetryConfigand keeps the given type when it adds the S3 error codes;s3_filesystem()passesconnection.retry_configas it is. The question about the default S3 error codes is already settled by Retry the transient S3 errors with the default retry policy (GH-1375) #1400 (S3Core.resolve_retry_config()addsRETRY_ERROR_CODES) and carries over unchanged.override: the filesystem keeps its own copies, sending the samePyAthena/<version>user agent as now.pyathena/filesystem/and fails on an import from PyAthena outside the package.Sort the imports of the filesystem benchmark (GH-1388)
Let the filesystem's stop scopes stop PyAthena's retries too (GH-1388)
List the stop-scope functions of the filesystem in the API reference (G…
laughingman7743 commentedon Nov 21, 2026
Amendment to decision 1 (2026-11-22), from the independent review of #1431: the filesystem's own stop scopes (for example that of a cancelled
S3ThreadPool.run()call) used to stop apyathena.util.retry_api_call()in a callable run there, and the split would have lost that for callers of the public executors. PyAthena's retries and stop checks (retry_api_call(),retries_stopped(),raise_if_stopped(),wait_with_retry_stop()) therefore also observe the filesystem's scopes, throughpyathena.filesystem.s3_retry.retry_stop_events(). The dependency still points only from PyAthena to the filesystem, and any stop scope again stops any retry in it.Keep one retry stop state, owned by the filesystem (GH-1388)
laughingman7743 commentedon Nov 21, 2026
Second amendment to decision 1 (2026-11-22), from the follow-up review of #1431: PyAthena keeps no stop state of its own.
pyathena.util.stop_retries_on(),unstoppable_retries(), andretries_stopped()keep their names and documentation and usepyathena.filesystem.s3_retry's scopes, andpyathena.util.retry_api_call()andwait_with_retry_stop()reads3_retry.retry_stop_events(). Reading the union of two variables still let a stopped PyAthena scope stop a PyAthena retry inside the filesystem's unstoppable block; with one state, owned by the filesystem, the semantics are those before the split, and the dependency still points only from PyAthena to the filesystem.Decision 2 is kept for now:
import pyathenaloading fsspec and the filesystem (unneeded by cursors that read no files through it) is marked with a TODO inpyathena/util.py, to revisit when the filesystem becomes its own distribution (#1315 step 5).Stop the bridge tests' scopes while the retries and waits run (GH-1388)
Stop the relay test's scope after the waiter's second check (GH-1388)
Resolve import_module() anchors as Python does in the boundary test (G…
Reject any dynamic import in the filesystem package (GH-1388)
Name the client of chunked Polars Parquet reads in the usage guide (G…