Skip to content

Commit e79e713

Browse files
Add workflows to the project (#1)
* Add workflows to the project * add tests * don't run on python 3.13
1 parent 79e7363 commit e79e713

File tree

6 files changed

+147
-6
lines changed

6 files changed

+147
-6
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ jobs:
4646
name: Release ${{ github.event.release.tag_name }}
4747
commit: main
4848
body: Release ${{ github.event.release.tag_name }}
49-
token: ${{ secrets.GITHUB_TOKEN }}
49+
token: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/test.yml

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
name: Numba-cache-lock
2+
3+
on:
4+
# Trigger the workflow on push or pull request,
5+
# but only for the main branch
6+
pull_request:
7+
branches:
8+
- main
9+
10+
# kill any previous running job on a new commit
11+
concurrency:
12+
group: build-and-test-cache-lock-${{ github.head_ref }}
13+
cancel-in-progress: true
14+
15+
jobs:
16+
lint:
17+
if: ${{ !contains(github.event.pull_request.labels.*.name, 'skip-tests') }}
18+
runs-on: ${{ matrix.os }}
19+
strategy:
20+
fail-fast: true
21+
matrix:
22+
os: [ubuntu-latest]
23+
python-version: ['3.12']
24+
25+
steps:
26+
- name: Checkout code
27+
uses: actions/checkout@v2
28+
29+
- name: Set up Python
30+
uses: actions/setup-python@v2
31+
with:
32+
python-version: ${{ matrix.python-version }}
33+
34+
- name: Install Poetry
35+
uses: snok/install-poetry@v1
36+
37+
- name: Install dependencies
38+
run: poetry install --no-interaction
39+
40+
- name: Run black
41+
run: poetry run black --check .
42+
43+
- name: Run ruff
44+
run: poetry run ruff check .
45+
46+
- name: Run mypy
47+
run: poetry run mypy .
48+
49+
test:
50+
name: ${{ matrix.os }} - Python v${{ matrix.python-version }}
51+
runs-on: ${{ matrix.os }}
52+
timeout-minutes: 10
53+
strategy:
54+
fail-fast: false
55+
matrix:
56+
os: [ubuntu-latest]
57+
python-version: ['3.11', '3.12']
58+
59+
steps:
60+
- name: Checkout code
61+
uses: actions/checkout@v2
62+
63+
- name: Set up Python ${{ matrix.python-version }}
64+
uses: actions/setup-python@v2
65+
with:
66+
python-version: ${{ matrix.python-version }}
67+
68+
- name: Install Poetry
69+
uses: snok/install-poetry@v1
70+
71+
- name: Install dependencies
72+
run: poetry install --no-interaction
73+
74+
- name: Run tests
75+
shell: bash -l {0}
76+
run: |
77+
poetry run python -m unittest discover -s tests

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "numba-cache-lock"
3-
version = "0.1.0"
3+
version = "0.1.0a1"
44
description = "A caching system for Numba with file locking support"
55
authors = [
66
{name = "Guilherme Leobas"}
@@ -21,7 +21,6 @@ classifiers = [
2121
"Programming Language :: Python :: 3.10",
2222
"Programming Language :: Python :: 3.11",
2323
"Programming Language :: Python :: 3.12",
24-
"Programming Language :: Python :: 3.13",
2524
]
2625

2726
[project.urls]

src/numba_cache_lock/__init__.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
from datetime import timedelta
3-
from typing import Final, Optional
3+
from typing import Final
44

55
from flufl.lock import Lock
66
from numba.core.caching import FunctionCache
@@ -41,9 +41,17 @@ def save_overload(self, sig, data):
4141

4242

4343
def patch_numba_cache(lifetime: timedelta = DEFAULT_LOCK_LIFETIME):
44-
Dispatcher = dispatcher_registry[target_registry["CPU"]]
44+
dispatcher = dispatcher_registry[target_registry["CPU"]]
4545

4646
def enable_caching(self):
4747
self._cache = FileLockFunctionCache(self.py_func, lifetime=lifetime)
4848

49-
Dispatcher.enable_caching = enable_caching
49+
dispatcher.enable_caching = enable_caching
50+
51+
52+
try:
53+
from importlib.metadata import version
54+
except ImportError:
55+
from importlib_metadata import version # type: ignore[no-redef]
56+
57+
__version__ = version("numba_cache_lock")

tests/cache_usecases.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from numba import jit
2+
3+
from numba_cache_lock import patch_numba_cache
4+
5+
patch_numba_cache()
6+
7+
8+
@jit(cache=True, nopython=True)
9+
def add_fn_usecase(x, y):
10+
return x + y
11+
12+
13+
@jit(cache=True, nopython=True)
14+
def incr_fn_usecase(x):
15+
return x + 1

tests/test_cache_lock.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import multiprocessing
2+
import os
3+
import unittest
4+
5+
from numba.tests.test_caching import DispatcherCacheUsecasesTest
6+
7+
8+
class TestCacheLock(DispatcherCacheUsecasesTest):
9+
here = os.path.dirname(__file__)
10+
usecases_file = os.path.join(here, "cache_usecases.py")
11+
12+
def test_caching(self):
13+
self.check_pycache(0)
14+
mod = self.import_module()
15+
self.check_pycache(0)
16+
17+
f = mod.add_fn_usecase
18+
self.assertPreciseEqual(f(2, 3), 5)
19+
self.check_pycache(2) # 1 index, 1 data
20+
21+
def test_multiprocessing(self):
22+
# Check caching works from multiple processes at once (#2028)
23+
mod = self.import_module()
24+
# Calling a pure Python caller of the JIT-compiled function is
25+
# necessary to reproduce the issue.
26+
f = mod.incr_fn_usecase
27+
n = 3
28+
try:
29+
ctx = multiprocessing.get_context("spawn")
30+
except AttributeError:
31+
ctx = multiprocessing
32+
pool = ctx.Pool(n)
33+
args = range(n)
34+
try:
35+
res = sum(pool.imap(f, args))
36+
finally:
37+
pool.close()
38+
self.assertEqual(res, sum(range(n)) + n)
39+
40+
41+
if __name__ == "__main__":
42+
unittest.main()

0 commit comments

Comments
 (0)