Skip to main content

A minimal library for writing tests with optional dependencies.

Project description

Needy

A minimal library for writing tests with optional dependencies.

Installation

Python 3.11+ is required.

pip install pyneedy

Quick Start

Define "probes" for the optional dependencies you want to use, e.g. JAX.

class check:
    @staticmethod
    def jax() -> None:
        # A probe just tries to import the optional dependency.
        import jax as jax

Then wrap imports related to the optional dependency in a needs.modules(...) scope like this:

from needy import needs

from pytest import mark

with needs.modules(check.jax):
    import jax.numpy as jnp

@mark.parametrize(
    ["array", "expected_sum"],
    [
        (
            array := jnp.array([1.0, 2.0]),
            expected_sum := 3.0
        ),
        (
            array := jnp.array([]),
            expected_sum := 0.0
        )
    ]
)
def test_that_a_sum_is_computed(array, expected_sum) -> None:
    assert array.sum() == expected_sum

If jax is installed, the imports inside the with block resolve normally. If it isn't, every missing import inside the block is replaced with a proxy, and only the first real use of the missing modules will raise an error.

See the docs for more.

Development

Prerequisites

  • Python >= 3.11
  • uv (recommended) or pip
  • just (optional, for convenience)

Getting Started

  1. Clone the repository:

    git clone https://gitlab.com/pytooling/needy.git
    cd needy
    
  2. Create a virtual environment and install dependencies:

    uv sync
    
  3. Install pre-commit hooks:

    uv run pre-commit install
    

Running Checks

just check

Or manually:

uv run ruff check --fix && uv run ruff format && uv run pyright && uv run pytest

License

This project is licensed under the MIT License. See LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyneedy-1.0.0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyneedy-1.0.0-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file pyneedy-1.0.0.tar.gz.

File metadata

  • Download URL: pyneedy-1.0.0.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pyneedy-1.0.0.tar.gz
Algorithm Hash digest
SHA256 ac3976f29edd18be41dac46b48933596000e1823e6d96d1296fb6c94e8f744fb
MD5 524f4a9c9cbf1abfee3e3d7eaf489459
BLAKE2b-256 8b640be431cd479e83dd1a8ac1c01c526a32b05d4ca7f528476069068f54e68c

See more details on using hashes here.

File details

Details for the file pyneedy-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: pyneedy-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pyneedy-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e3a726ae6d46a6777416ddabf5b5c9ad2b9730e268c85c9d24a1ebcec3463fd4
MD5 3abd86842ac2e72e4e8e272882749def
BLAKE2b-256 00fca96fb4c8edc54178e43ce6b03d2c49d1b87088980da182b8bd57b2276874

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page