Skip to content

Commit d70db53

Browse files
committed
py3.10 compat
1 parent 9e7a7d4 commit d70db53

File tree

5 files changed

+39
-20
lines changed

5 files changed

+39
-20
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ repos:
4848
- id: setup-cfg-fmt
4949

5050
- repo: https://github.com/astral-sh/ruff-pre-commit
51-
rev: v0.14.3
51+
rev: v0.14.4
5252
hooks:
5353
- id: ruff
5454
args: [--fix, --show-fixes]
@@ -64,5 +64,5 @@ repos:
6464
rev: v1.18.2
6565
hooks:
6666
- id: mypy
67-
files: src/coffea/compute
67+
pass_filenames: false # to allow mypy to respect pyproject.toml config
6868
additional_dependencies: []

pyproject.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ dependencies = [
6565
"fsspec",
6666
"rich",
6767
"ipywidgets",
68+
"typing-extensions;python_version<'3.11'",
6869
]
6970
dynamic = ["version"]
7071

@@ -131,3 +132,11 @@ line-length = 160
131132

132133
[tool.ruff.lint]
133134
ignore = ["F403", "F405", "E402"]
135+
136+
[tool.mypy]
137+
python_version = "3.10"
138+
files = "src/coffea/compute"
139+
140+
[[tool.mypy.overrides]]
141+
module = ["uproot.*"]
142+
follow_untyped_imports = true

src/coffea/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2828
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2929
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30-
from . import _version
30+
from ._version import __version__
3131

32-
__version__ = _version.__version__
33-
34-
__all__ = ["_version", "__version__"]
32+
__all__ = ["__version__"]

src/coffea/compute/backends/threaded.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from collections.abc import Iterator
22
from dataclasses import dataclass, field
33
from itertools import starmap
4-
from queue import Queue, ShutDown
4+
from queue import Queue
55
from threading import Condition, Thread
66
from types import TracebackType
77
from typing import TYPE_CHECKING, Generic
@@ -199,11 +199,20 @@ def _run(self) -> None:
199199
self._cv.notify_all()
200200

201201

202+
class _Shutdown:
203+
"""A sentinel to signal worker threads to exit.
204+
205+
In python 3.13+, we can use queue.ShutDown directly.
206+
"""
207+
208+
pass
209+
210+
202211
def _work(task_queue: Queue[ThreadedTask]) -> None: # type: ignore[type-arg]
203212
while True:
204-
try:
205-
task = task_queue.get()
206-
except ShutDown:
213+
task = task_queue.get()
214+
if isinstance(task, _Shutdown):
215+
task_queue.task_done()
207216
break
208217
try:
209218
task._run()
@@ -215,7 +224,7 @@ def _work(task_queue: Queue[ThreadedTask]) -> None: # type: ignore[type-arg]
215224

216225

217226
class SingleThreadedBackend:
218-
_task_queue: Queue[ThreadedTask] | None # type: ignore[type-arg]
227+
_task_queue: Queue[ThreadedTask | _Shutdown] | None # type: ignore[type-arg]
219228
_thread: Thread | None
220229

221230
def __init__(self) -> None:
@@ -230,7 +239,7 @@ def __enter__(self) -> "RunningSingleThreadedBackend":
230239
args=(self._task_queue,),
231240
)
232241
self._thread.start()
233-
return RunningSingleThreadedBackend(self._task_queue)
242+
return RunningSingleThreadedBackend(self)
234243

235244
def __exit__(
236245
self,
@@ -239,32 +248,32 @@ def __exit__(
239248
traceback: TracebackType | None,
240249
) -> None:
241250
assert self._thread and self._task_queue
242-
self._task_queue.shutdown()
251+
self._task_queue.put(_Shutdown())
243252
self._thread.join()
244253
self._task_queue = None
245254
self._thread = None
246255

247256

248257
class RunningSingleThreadedBackend:
249-
queue: Queue[ThreadedTask] # type: ignore[type-arg]
258+
_backend: SingleThreadedBackend
250259

251-
def __init__(self, queue: Queue[ThreadedTask]): # type: ignore[type-arg]
252-
self.queue = queue
260+
def __init__(self, backend: SingleThreadedBackend): # type: ignore[type-arg]
261+
self._backend = backend
253262

254263
def compute(
255264
self,
256265
item: Computable[InputT, ResultT],
257266
/,
258267
error_policy: ErrorPolicy = ErrorPolicy(),
259268
) -> ThreadedTask[InputT, ResultT]:
260-
if self.queue.is_shutdown:
269+
if self._backend._task_queue is None:
261270
raise RuntimeError(
262271
"Cannot compute on a backend that has been exited from its context manager"
263272
)
264273
if hasattr(item, "__next__"):
265274
raise TypeError("Computable items must be iterables, not iterators")
266275
task = ThreadedTask(item, error_policy)
267-
self.queue.put(task)
276+
self._backend._task_queue.put(task)
268277
return task
269278

270279
def wait_all(self, progress: bool = False) -> None:
@@ -278,7 +287,8 @@ def wait_all(self, progress: bool = False) -> None:
278287
if progress:
279288
raise NotImplementedError("Progress bars are not yet implemented")
280289
else:
281-
self.queue.join()
290+
if self._backend._task_queue:
291+
self._backend._task_queue.join()
282292

283293

284294
if TYPE_CHECKING:

src/coffea/compute/group.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from dataclasses import dataclass
2-
from typing import Callable, Generic, Self
2+
from typing import Callable, Generic
3+
4+
from typing_extensions import Self # Python < 3.11
35

46
from coffea.compute.context import ContextInput, Ctx_co
57
from coffea.compute.protocol import InputT, ResultT

0 commit comments

Comments
 (0)