From bdff5a2f98366f0b41a4b4bf9053cac9dbf7705f Mon Sep 17 00:00:00 2001 From: Talley Lambert Date: Thu, 7 Nov 2024 08:17:00 -0500 Subject: [PATCH 1/6] linting, ruff, add tests --- .github/workflows/black.yaml | 17 ----------------- .github/workflows/mypy.yaml | 18 ------------------ .github/workflows/tests.yaml | 37 +++++++++++++++++++++--------------- .pre-commit-config.yaml | 8 +++++--- pyproject.toml | 10 +++++++--- src/witty/__init__.py | 2 ++ src/witty/compile_module.py | 6 +++++- tests/test_compile.py | 4 ++-- 8 files changed, 43 insertions(+), 59 deletions(-) delete mode 100644 .github/workflows/black.yaml delete mode 100644 .github/workflows/mypy.yaml diff --git a/.github/workflows/black.yaml b/.github/workflows/black.yaml deleted file mode 100644 index 60b6467..0000000 --- a/.github/workflows/black.yaml +++ /dev/null @@ -1,17 +0,0 @@ -name: Python Black - -on: [push, pull_request] - -jobs: - lint: - name: Python Lint - runs-on: ubuntu-latest - steps: - - name: Setup Python - uses: actions/setup-python@v4 - - name: Setup checkout - uses: actions/checkout@master - - name: Lint with Black - run: | - pip install black - black --diff --check src/witty tests diff --git a/.github/workflows/mypy.yaml b/.github/workflows/mypy.yaml deleted file mode 100644 index 495582f..0000000 --- a/.github/workflows/mypy.yaml +++ /dev/null @@ -1,18 +0,0 @@ -name: Python mypy - -on: [push, pull_request] - -jobs: - static-analysis: - name: Python mypy - runs-on: ubuntu-latest - steps: - - name: Setup Python - uses: actions/setup-python@v4 - - name: Setup checkout - uses: actions/checkout@v2 - - name: mypy - run: | - pip install . - pip install --upgrade mypy - mypy src/witty diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 384aaea..26e2ce4 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -1,25 +1,32 @@ name: Test -on: - push: +on: [push, pull_request] jobs: - test: + lint: + name: Lint runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Run pre-commit + run: pipx run pre-commit run --all-files + + test: + runs-on: [ubuntu-latest, macos-latest, windows-latest] strategy: fail-fast: false matrix: - python-version: ["3.9", "3.10"] + python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"] steps: - - uses: actions/checkout@v2 - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v4 - with: - python-version: ${{ matrix.python-version }} - - name: Install dependencies - run: | - pip install ".[dev]" - - name: Test with pytest - run: | - pytest tests + - uses: actions/checkout@v2 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + pip install ".[dev]" + - name: Test with pytest + run: | + pytest tests diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 17dc760..e6f0cfe 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -14,10 +14,12 @@ repos: - id: check-yaml - id: check-added-large-files - - repo: https://github.com/psf/black - rev: 24.10.0 + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.7.2 hooks: - - id: black + - id: ruff + args: [--fix, --unsafe-fixes] + - id: ruff-format - repo: https://github.com/pre-commit/mirrors-mypy rev: v1.13.0 diff --git a/pyproject.toml b/pyproject.toml index 0dcb32f..727e634 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,7 +6,7 @@ requires = ["setuptools", "wheel"] name = "witty" description = "Well-in-Time Compiler for Cython Modules" readme = "README.md" -requires-python = ">=3.7" +requires-python = ">=3.9" classifiers = [ "Programming Language :: Python :: 3", ] @@ -16,12 +16,12 @@ authors = [ { email = "funkej@janelia.hhmi.org", name = "Jan Funke" }, ] dynamic = ["version"] -dependencies = ["cython"] +dependencies = ["cython", "setuptools; python_version >= '3.12'"] [project.optional-dependencies] dev = [ 'pytest', - 'black', + 'ruff', 'mypy', 'pdoc', 'pre-commit' @@ -30,3 +30,7 @@ dev = [ [project.urls] homepage = "https://github.com/funkelab/witty" repository = "https://github.com/funkelab/witty" + +[tool.ruff] +target-version = "py39" +src = ["src"] diff --git a/src/witty/__init__.py b/src/witty/__init__.py index 9d723e0..05cc9d7 100644 --- a/src/witty/__init__.py +++ b/src/witty/__init__.py @@ -1,3 +1,5 @@ from .compile_module import compile_module __version__ = "0.1" + +__all__ = ["compile_module"] diff --git a/src/witty/compile_module.py b/src/witty/compile_module.py index c388d07..e20f1e4 100644 --- a/src/witty/compile_module.py +++ b/src/witty/compile_module.py @@ -6,9 +6,13 @@ from Cython.Build import cythonize from Cython.Build.Inline import to_unicode, _get_build_extension from Cython.Utils import get_cython_cache_dir -from distutils.core import Extension from pathlib import Path +try: + from setuptools import Extension +except ImportError: + from distutils.core import Extension + def load_dynamic(module_name, module_lib): spec = importlib.util.spec_from_file_location(module_name, module_lib) diff --git a/tests/test_compile.py b/tests/test_compile.py index af79bdc..c41e4fc 100644 --- a/tests/test_compile.py +++ b/tests/test_compile.py @@ -25,7 +25,7 @@ def to_vector({type} x): result = module_int.add(3, 4) assert result == 7 - assert type(result) == int + assert type(result) is int module_float = witty.compile_module( source_pxy_template.format(type="float"), language="c++" @@ -33,4 +33,4 @@ def to_vector({type} x): result = module_float.add(3, 4) assert result == 7 - assert type(result) == float + assert type(result) is float From 33fdc70cec1d20c87ba2338d04ae5dcdff2ac9c2 Mon Sep 17 00:00:00 2001 From: Talley Lambert Date: Thu, 7 Nov 2024 08:18:49 -0500 Subject: [PATCH 2/6] fix matrix --- .github/workflows/tests.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 26e2ce4..6bf2e66 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -12,10 +12,11 @@ jobs: run: pipx run pre-commit run --all-files test: - runs-on: [ubuntu-latest, macos-latest, windows-latest] + runs-on: ${{ matrix.os }} strategy: fail-fast: false matrix: + os: [ubuntu-latest, macos-latest, windows-latest] python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"] steps: From 207c03f2d047e531377c8c301b4de4a22963aec7 Mon Sep 17 00:00:00 2001 From: Talley Lambert Date: Thu, 7 Nov 2024 08:24:44 -0500 Subject: [PATCH 3/6] fix windows --- src/witty/compile_module.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/witty/compile_module.py b/src/witty/compile_module.py index e20f1e4..761c362 100644 --- a/src/witty/compile_module.py +++ b/src/witty/compile_module.py @@ -1,3 +1,4 @@ +import os import Cython import fcntl import hashlib @@ -115,8 +116,8 @@ def compile_module( module_dir.mkdir(parents=True, exist_ok=True) # make sure the same module is not build concurrently - with open(module_lock, "w") as lock_file: - fcntl.lockf(lock_file, fcntl.LOCK_EX) + with open(module_lock, "w") as lock_f: + lock_file(lock_f) # already compiled? if module_lib.is_file() and not force_rebuild: @@ -146,3 +147,22 @@ def compile_module( build_extension.run() return load_dynamic(module_name, module_lib) + + +if os.name == "nt": + import msvcrt + + def lock_file(file): + msvcrt.locking(file.fileno(), msvcrt.LK_LOCK, os.path.getsize(file.name)) + + def unlock_file(file): + msvcrt.locking(file.fileno(), msvcrt.LK_UNLCK, os.path.getsize(file.name)) + +else: + import fcntl + + def lock_file(file): + fcntl.lockf(file, fcntl.LOCK_EX) + + def unlock_file(file): + fcntl.lockf(file, fcntl.LOCK_UN) From 84ace9a40f688c343cc05a5794edc6af57077410 Mon Sep 17 00:00:00 2001 From: Talley Lambert Date: Thu, 7 Nov 2024 08:25:15 -0500 Subject: [PATCH 4/6] switch order of import --- src/witty/compile_module.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/witty/compile_module.py b/src/witty/compile_module.py index 761c362..f6d1289 100644 --- a/src/witty/compile_module.py +++ b/src/witty/compile_module.py @@ -10,9 +10,9 @@ from pathlib import Path try: - from setuptools import Extension -except ImportError: from distutils.core import Extension +except ImportError: + from setuptools import Extension def load_dynamic(module_name, module_lib): From 148e6ee3f54b63579fa22cac4f507912a04a5ddf Mon Sep 17 00:00:00 2001 From: Talley Lambert Date: Thu, 7 Nov 2024 08:27:13 -0500 Subject: [PATCH 5/6] fix linting and windows --- src/witty/compile_module.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/witty/compile_module.py b/src/witty/compile_module.py index f6d1289..cfa8a2b 100644 --- a/src/witty/compile_module.py +++ b/src/witty/compile_module.py @@ -1,6 +1,5 @@ import os import Cython -import fcntl import hashlib import importlib.util import sys @@ -12,7 +11,7 @@ try: from distutils.core import Extension except ImportError: - from setuptools import Extension + from setuptools import Extension # type: ignore [no-redef] def load_dynamic(module_name, module_lib): From 44eff2d6500ce4db14762e3fac5bfa792f307cd3 Mon Sep 17 00:00:00 2001 From: Talley Lambert Date: Thu, 7 Nov 2024 08:29:29 -0500 Subject: [PATCH 6/6] add color to test --- .github/workflows/tests.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 6bf2e66..7f5bcca 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -30,4 +30,4 @@ jobs: pip install ".[dev]" - name: Test with pytest run: | - pytest tests + pytest --color=yes -v tests