From 5bd9ad049db825fb620f1cc3e897ff899c94082f Mon Sep 17 00:00:00 2001 From: Neil Girdhar Date: Sun, 4 May 2025 00:28:34 -0400 Subject: [PATCH] Switch to pyproject.toml; remove setuptools dependency Also, switch from flake8 to Ruff --- .github/workflows/ci.yml | 23 ++++++-- .readthedocs.yaml | 6 +-- MANIFEST.in | 4 -- pyproject.toml | 84 ++++++++++++++++++++++++++++++ requirements/requirements-docs.txt | 3 -- requirements/requirements-test.txt | 2 - requirements/requirements.txt | 7 --- setup.py | 76 --------------------------- test.sh | 20 ++++--- 9 files changed, 114 insertions(+), 111 deletions(-) delete mode 100644 MANIFEST.in create mode 100644 pyproject.toml delete mode 100644 requirements/requirements-docs.txt delete mode 100644 requirements/requirements-test.txt delete mode 100644 requirements/requirements.txt delete mode 100644 setup.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f9306bce..34fe5d9c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,7 +18,7 @@ concurrency: jobs: build-and-test: - name: "Python ${{ matrix.python-version }} on ${{ matrix.os }} jax=${{ matrix.jax-version}}" + name: "Python ${{ matrix.python-version }} on ${{ matrix.os }} jax=${{ matrix.jax-version }}" runs-on: "${{ matrix.os }}" strategy: @@ -29,7 +29,7 @@ jobs: include: - python-version: "3.10" os: "ubuntu-latest" - jax-version: "0.4.27" # Keep this in sync with version in requirements.txt + jax-version: "0.4.27" # Keep this in sync with version in pyproject.toml - python-version: "3.13" os: "ubuntu-latest" jax-version: "nightly" @@ -38,9 +38,22 @@ jobs: - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 - uses: actions/setup-python@0b93645e9fea7318ecaed2b359559ac225c90a2b # v5.3.0 with: - python-version: "${{ matrix.python-version }}" - cache: "pip" - cache-dependency-path: '**/requirements*.txt' + python-version: "${{ matrix.python-version }}" + + - name: Compute pyproject.toml hash + id: deps + run: | + echo "hash=$(shasum -a 256 pyproject.toml | cut -d' ' -f1)" >> $GITHUB_OUTPUT + + - name: Cache pip based on pyproject.toml + uses: actions/cache@v3 + with: + path: ~/.cache/pip + key: pip-${{ runner.os }}-${{ matrix.python-version }}-${{ steps.deps.outputs.hash }} + + - name: Install build dependencies + run: pip install .[dev,test] + - name: Run CI tests run: JAX_VERSION="${{ matrix.jax-version }}" bash test.sh shell: bash diff --git a/.readthedocs.yaml b/.readthedocs.yaml index 1823fadf..e3e74e26 100644 --- a/.readthedocs.yaml +++ b/.readthedocs.yaml @@ -15,7 +15,7 @@ sphinx: python: install: - - requirements: requirements/requirements-docs.txt - - requirements: requirements/requirements.txt - - method: setuptools + - method: pip path: . + extra_requirements: + - docs diff --git a/MANIFEST.in b/MANIFEST.in deleted file mode 100644 index f645b040..00000000 --- a/MANIFEST.in +++ /dev/null @@ -1,4 +0,0 @@ -include README.md -include LICENSE -include requirements/* -include chex/py.typed diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 00000000..f35cd70f --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,84 @@ +[build-system] +requires = ["setuptools", "wheel"] +build-backend = "setuptools.build_meta" + +[project] +name = "chex" +dynamic = ["version"] +description = "Chex: Testing made fun, in JAX!" +readme = "README.md" +license = "Apache-2.0" +requires-python = ">=3.9" +authors = [ + { name = "DeepMind", email = "chex-dev@google.com" }, +] +keywords = [ + "debugging", + "jax", + "learning", + "machine", + "python", + "testing", +] +classifiers = [ + "Development Status :: 5 - Production/Stable", + "Environment :: Console", + "Intended Audience :: Developers", + "Intended Audience :: Science/Research", + "Operating System :: OS Independent", + "Programming Language :: Python", + "Programming Language :: Python :: 3", + "Topic :: Scientific/Engineering :: Artificial Intelligence", + "Topic :: Software Development :: Libraries :: Python Modules", + "Topic :: Software Development :: Testing :: Mocking", + "Topic :: Software Development :: Testing :: Unit", +] +dependencies = [ + "absl-py>=0.9.0", + "jax>=0.4.27", + "jaxlib>=0.4.27", + "numpy>=1.24.1", + "toolz>=0.9.0", + "typing_extensions>=4.2.0", +] + +[project.optional-dependencies] +dev = [ + "ruff", + "pytest-xdist", + "pylint", + "pylint-exit", +] +test = [ + "cloudpickle==3.1.0", + "dm-tree>=0.1.9", +] +docs = [ + "sphinx>=6.0.0", + "sphinx-book-theme>=1.0.1", + "sphinxcontrib-katex", +] + +[project.urls] +Homepage = "https://github.com/deepmind/chex" + +[tool.hatch.version] +path = "chex/__init__.py" + +[tool.hatch.build.targets.sdist] +include = [ + "/chex", +] + +[tool.ruff] +preview = true + +[tool.ruff.lint] +select = [ + "E9", + "F63", + "F7", + "F82", + "E225", + "E251", +] diff --git a/requirements/requirements-docs.txt b/requirements/requirements-docs.txt deleted file mode 100644 index d20b8268..00000000 --- a/requirements/requirements-docs.txt +++ /dev/null @@ -1,3 +0,0 @@ -sphinx>=6.0.0 -sphinx-book-theme>=1.0.1 -sphinxcontrib-katex diff --git a/requirements/requirements-test.txt b/requirements/requirements-test.txt deleted file mode 100644 index 3f597eb0..00000000 --- a/requirements/requirements-test.txt +++ /dev/null @@ -1,2 +0,0 @@ -cloudpickle==3.1.0 -dm-tree>=0.1.9 diff --git a/requirements/requirements.txt b/requirements/requirements.txt deleted file mode 100644 index c46e2569..00000000 --- a/requirements/requirements.txt +++ /dev/null @@ -1,7 +0,0 @@ -absl-py>=0.9.0 -typing_extensions>=4.2.0 -jax>=0.4.27 -jaxlib>=0.4.27 -numpy>=1.24.1 -setuptools;python_version>="3.12" -toolz>=0.9.0 diff --git a/setup.py b/setup.py deleted file mode 100644 index f05a10dd..00000000 --- a/setup.py +++ /dev/null @@ -1,76 +0,0 @@ -# Copyright 2020 DeepMind Technologies Limited. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Install script for setuptools.""" - -import os -from setuptools import find_packages -from setuptools import setup - -_CURRENT_DIR = os.path.dirname(os.path.abspath(__file__)) - - -def _get_version(): - with open(os.path.join(_CURRENT_DIR, 'chex', '__init__.py')) as fp: - for line in fp: - if line.startswith('__version__') and '=' in line: - version = line[line.find('=') + 1:].strip(' \'"\n') - if version: - return version - raise ValueError('`__version__` not defined in `chex/__init__.py`') - - -def _parse_requirements(path): - with open(os.path.join(_CURRENT_DIR, path)) as f: - return [ - line.rstrip() - for line in f - if not (line.isspace() or line.startswith('#')) - ] - - -setup( - name='chex', - version=_get_version(), - url='https://github.com/deepmind/chex', - license='Apache 2.0', - author='DeepMind', - description=('Chex: Testing made fun, in JAX!'), - long_description=open(os.path.join(_CURRENT_DIR, 'README.md')).read(), - long_description_content_type='text/markdown', - author_email='chex-dev@google.com', - keywords='jax testing debugging python machine learning', - packages=find_packages(exclude=['*_test.py']), - install_requires=_parse_requirements( - os.path.join(_CURRENT_DIR, 'requirements', 'requirements.txt')), - tests_require=_parse_requirements( - os.path.join(_CURRENT_DIR, 'requirements', 'requirements-test.txt')), - zip_safe=False, # Required for full installation. - include_package_data=True, - python_requires='>=3.9', - classifiers=[ - 'Development Status :: 5 - Production/Stable', - 'Environment :: Console', - 'Intended Audience :: Science/Research', - 'Intended Audience :: Developers', - 'License :: OSI Approved :: Apache Software License', - 'Operating System :: OS Independent', - 'Programming Language :: Python', - 'Programming Language :: Python :: 3', - 'Topic :: Scientific/Engineering :: Artificial Intelligence', - 'Topic :: Software Development :: Testing :: Mocking', - 'Topic :: Software Development :: Testing :: Unit', - 'Topic :: Software Development :: Libraries :: Python Modules', - ], -) diff --git a/test.sh b/test.sh index 4b55abe3..ff4a59ab 100755 --- a/test.sh +++ b/test.sh @@ -27,10 +27,8 @@ source "${VENV_DIR}/bin/activate" python --version # Install dependencies. -pip install --upgrade pip setuptools wheel -pip install flake8 pytest-xdist pylint pylint-exit -pip install -r requirements/requirements.txt -pip install -r requirements/requirements-test.txt +pip install --upgrade pip +pip install .[dev] # Install the requested JAX version if [ "$JAX_VERSION" = "" ]; then @@ -43,8 +41,8 @@ else pip install "jax==${JAX_VERSION}" "jaxlib==${JAX_VERSION}" fi -# Lint with flake8. -flake8 `find chex -name '*.py' | xargs` --count --select=E9,F63,F7,F82,E225,E251 --show-source --statistics +# Lint with Ruff. +ruff check # Lint with pylint. PYLINT_ARGS="-efail -wfail -cfail -rfail" @@ -62,9 +60,10 @@ pylint --rcfile=.pylintrc `find chex -name '*_test.py' | xargs` -d W0212,E1130,E rm .pylintrc # Build the package. -python setup.py sdist +pip install build +python -m build pip wheel --verbose --no-deps --no-clean dist/chex*.tar.gz -pip install chex*.whl +pip install chex*.whl --force-reinstall # Check types with pytype. # Note: pytype does not support 3.11 as of 25.06.23 @@ -77,7 +76,7 @@ fi; # Run tests using pytest. # Change directory to avoid importing the package from repo root. -pip install -r requirements/requirements-test.txt +pip install .[test] cd _testing # Main tests. @@ -88,8 +87,7 @@ pytest -n "$(grep -c ^processor /proc/cpuinfo)" --pyargs chex -k "fake_set_n_cpu cd .. # Build Sphinx docs. - -pip install -r requirements/requirements-docs.txt +pip install .[docs] cd docs make coverage_check make html