From b7f82923975b823a3937c3cf6bd755ae2deae76f Mon Sep 17 00:00:00 2001 From: Aaron Zuspan Date: Thu, 27 Apr 2023 10:45:43 -0700 Subject: [PATCH 1/5] Replace setup.py with pyproject.toml and setuptools with hatch #9 Other changes: - Remove optional test dependencies in favor of hatch environments - Remove optional dependencies already defined by pre-commit - Remove bumpversion and twine in favor of hatch - Set dynamic __version__ handled by hatch - Update CI to use hatch - Add Aaron Zuspan as second author - Update developer guide --- .github/workflows/ci.yaml | 12 +++++---- README.md | 48 +++++++++++++++++++++++++++++----- pyproject.toml | 51 +++++++++++++++++++++++++++++++++++++ setup.py | 40 ----------------------------- src/sklearn_knn/__init__.py | 2 ++ 5 files changed, 102 insertions(+), 51 deletions(-) create mode 100644 pyproject.toml delete mode 100644 setup.py diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 4cf9643..89ea973 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -24,10 +24,10 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - pip install -e .[dev] + pip install hatch - name: Run tests - run: pytest + run: hatch run test:all lint: runs-on: ubuntu-latest @@ -38,10 +38,12 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - pip install .[dev] + pip install hatch - - name: Sourcery login - run: sourcery login --token ${{ secrets.SOURCERY_TOKEN }} + - name: Setup environment + run: | + hatch shell + sourcery login --token ${{ secrets.SOURCERY_TOKEN }} - name: Set up pre-commit uses: pre-commit/action@v3.0.0 diff --git a/README.md b/README.md index 4d0f0df..206d3a3 100644 --- a/README.md +++ b/README.md @@ -6,15 +6,23 @@ This package is in active development. ### Setup -After cloning the repository, install the package in editable mode with the development dependencies using: +This project uses [hatch](https://hatch.pypa.io/latest/) to manage the development environment and build and publish releases. Make sure `hatch` is [installed](https://hatch.pypa.io/latest/install/) first: ```bash -$ pip install -e .[dev] +$ pip install hatch ``` +Now you can [enter the development environment](https://hatch.pypa.io/latest/environment/#entering-environments) using: + +```bash +$ hatch shell +``` + +This will install development dependencies in an isolated environment and drop you into a shell (use `exit` to leave). + ### Pre-commit -This project uses [pre-commit](https://pre-commit.com/) to run testing, linting, type-checking, and formatting. You can run pre-commit manually with: +Use [pre-commit](https://pre-commit.com/) to run linting, type-checking, and formatting: ```bash $ pre-commit run --all-files @@ -26,16 +34,44 @@ $ pre-commit run --all-files $ pre-commit install ``` +You can run pre-commit hooks separately and pass additional arguments to them. For example, to run `flake8` on a single file: + +```bash +$ pre-commit run flake8 --files=src/sklearn_knn/_base.py +``` + ### Testing -Unit tests are *not* run by `pre-commit`, but can be run manually with: +Unit tests are *not* run by `pre-commit`, but can be run manually using `hatch` [scripts](https://hatch.pypa.io/latest/config/environment/overview/#scripts): ```bash -$ pytest +$ hatch run test:all ``` Measure test coverage with: ```bash -$ pytest --cov=sklearn_knn +$ hatch run test:coverage +``` + +Any additional arguments are passed to `pytest`. For example, to run a subset of tests matching a keyword: + +```bash +$ hatch run test:all -k gnn +``` + +### Releasing + +First, use `hatch` to [update the version number](https://hatch.pypa.io/latest/version/#updating). + +```bash +$ hatch version [major|minor|patch] +``` + +Then, [build](https://hatch.pypa.io/latest/build/#building) and [publish](https://hatch.pypa.io/latest/publish/#publishing) the release to PyPI with: + +```bash +$ hatch clean +$ hatch build +$ hatch publish ``` \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..c630ae6 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,51 @@ +[build-system] +requires = ["hatchling"] +build-backend = "hatchling.build" + +[project] +name = "scikit-learn-knn" +dynamic = ["version"] +description = "Scikit-learn estimators for kNN methods" +readme = "README.md" +license = "" +requires-python = ">=3.8" +authors = [ + { name = "LEMMA group @ Oregon State University", email = "matt.gregory@oregonstate.edu" }, + { name = "Aaron Zuspan", email = "aaron.zuspan@oregonstate.edu" } +] +dependencies = [ + "numpy", + "scikit-learn", +] + +[project.urls] +Homepage = "https://github.com/lemma-osu/scikit-learn-knn" +Source = "https://github.com/lemma-osu/scikit-learn-knn" + +[tool.hatch.version] +path = "src/sklearn_knn/__init__.py" + +[tool.hatch.build.targets.sdist] +include = [ + "/src", +] + +[tool.hatch.envs.default] +dependencies = [ + "pre-commit", + "sourcery", +] + +[tool.hatch.envs.test] +dependencies = [ + "pytest", + "pytest-cov", + "pandas" +] + +[tool.hatch.envs.test.scripts] +all = "pytest {args}" +coverage = "pytest --cov=src/sklearn_knn {args}" + +[tool.pytest.ini_options] +pythonpath = "src/" diff --git a/setup.py b/setup.py deleted file mode 100644 index 491d04b..0000000 --- a/setup.py +++ /dev/null @@ -1,40 +0,0 @@ -from setuptools import find_packages, setup - - -def setup_package(): - metadata = dict( - name="scikit-learn-knn", - author="LEMMA group @ Oregon State University", - author_email="matt.gregory@oregonstate.edu", - description="Scikit-learn estimators for kNN methods", - url="http://github.com/lemma-osu/scikit-learn-knn/", - version="0.1.0", - package_dir={"": "src"}, - packages=find_packages(where="src"), - python_requires=">=3.8", - install_requires=[ - "numpy", - "scikit-learn", - ], - extras_require={ - "dev": [ - "black", - "bumpversion", - "flake8", - "flake8-bugbear", - "isort", - "pytest", - "pytest-cov", - "pandas", - "pre-commit", - "mypy", - "twine", - "sourcery", - ], - }, - ) - setup(**metadata) - - -if __name__ == "__main__": - setup_package() diff --git a/src/sklearn_knn/__init__.py b/src/sklearn_knn/__init__.py index eafc84d..653aa03 100644 --- a/src/sklearn_knn/__init__.py +++ b/src/sklearn_knn/__init__.py @@ -4,6 +4,8 @@ from ._msn import MSN from ._raw import Raw +__version__ = "0.1.0" + __all__ = [ "Raw", "Euclidean", From 10c5e67a23aafddc885201bafede3f18b51b6a1c Mon Sep 17 00:00:00 2001 From: Aaron Zuspan Date: Thu, 27 Apr 2023 10:56:57 -0700 Subject: [PATCH 2/5] Fix CI `hatch shell` doesn't seem to work in a Github action. Instead, it should work to use `hatch run` to run the sourcery login in the dev environment and let the pre-commit action handle installation of pre-commit. --- .github/workflows/ci.yaml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 89ea973..6ee1cef 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -42,8 +42,7 @@ jobs: - name: Setup environment run: | - hatch shell - sourcery login --token ${{ secrets.SOURCERY_TOKEN }} + hatch run sourcery login --token ${{ secrets.SOURCERY_TOKEN }} - name: Set up pre-commit uses: pre-commit/action@v3.0.0 From 11aad97fa887c8cc7b110bcc57aef90b1997f1b2 Mon Sep 17 00:00:00 2001 From: Aaron Zuspan Date: Fri, 28 Apr 2023 09:46:32 -0700 Subject: [PATCH 3/5] Update authors and move __version__ into __about__.py --- pyproject.toml | 4 ++-- src/sklearn_knn/__about__.py | 1 + src/sklearn_knn/__init__.py | 3 +-- 3 files changed, 4 insertions(+), 4 deletions(-) create mode 100644 src/sklearn_knn/__about__.py diff --git a/pyproject.toml b/pyproject.toml index c630ae6..a64c210 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,7 +10,7 @@ readme = "README.md" license = "" requires-python = ">=3.8" authors = [ - { name = "LEMMA group @ Oregon State University", email = "matt.gregory@oregonstate.edu" }, + { name = "Matthew Gregory", email = "matt.gregory@oregonstate.edu" }, { name = "Aaron Zuspan", email = "aaron.zuspan@oregonstate.edu" } ] dependencies = [ @@ -23,7 +23,7 @@ Homepage = "https://github.com/lemma-osu/scikit-learn-knn" Source = "https://github.com/lemma-osu/scikit-learn-knn" [tool.hatch.version] -path = "src/sklearn_knn/__init__.py" +path = "src/sklearn_knn/__about__.py" [tool.hatch.build.targets.sdist] include = [ diff --git a/src/sklearn_knn/__about__.py b/src/sklearn_knn/__about__.py new file mode 100644 index 0000000..3dc1f76 --- /dev/null +++ b/src/sklearn_knn/__about__.py @@ -0,0 +1 @@ +__version__ = "0.1.0" diff --git a/src/sklearn_knn/__init__.py b/src/sklearn_knn/__init__.py index 653aa03..b24f827 100644 --- a/src/sklearn_knn/__init__.py +++ b/src/sklearn_knn/__init__.py @@ -1,11 +1,10 @@ +from .__about__ import __version__ # noqa: F401 from ._euclidean import Euclidean from ._gnn import GNN from ._mahalanobis import Mahalanobis from ._msn import MSN from ._raw import Raw -__version__ = "0.1.0" - __all__ = [ "Raw", "Euclidean", From b9f5a369f3f16a0b7877f8daeda08228bdcc4f35 Mon Sep 17 00:00:00 2001 From: Matt Gregory Date: Fri, 28 Apr 2023 15:04:45 -0700 Subject: [PATCH 4/5] Update author nickname --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index a64c210..2f06a56 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,7 +10,7 @@ readme = "README.md" license = "" requires-python = ">=3.8" authors = [ - { name = "Matthew Gregory", email = "matt.gregory@oregonstate.edu" }, + { name = "Matt Gregory", email = "matt.gregory@oregonstate.edu" }, { name = "Aaron Zuspan", email = "aaron.zuspan@oregonstate.edu" } ] dependencies = [ From d27508d54af487b125da58cdcc616933752a6c5e Mon Sep 17 00:00:00 2001 From: Aaron Zuspan Date: Tue, 2 May 2023 08:45:12 -0700 Subject: [PATCH 5/5] Add package path to hatch config See pypa/hatch#845. This was necessary because our project name is different from our package name, so hatch wasn't able to locate it in the default path. Note that if the shell has been built before making this change, it will need to be manually rebuilt by running `hatch env prune`. --- pyproject.toml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index 2f06a56..407b82f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,6 +25,9 @@ Source = "https://github.com/lemma-osu/scikit-learn-knn" [tool.hatch.version] path = "src/sklearn_knn/__about__.py" +[tool.hatch.build.targets.wheel] +packages = ["src/sklearn_knn"] + [tool.hatch.build.targets.sdist] include = [ "/src",