A better ANY helper for Python testing
Project description
AnyValue
A better ANY helper for Python testing
Documentation: https://frankie567.github.io/anyvalue/
Source Code: https://github.com/frankie567/anyvalue
QuickStart
A smarter alternative to unittest.mock.ANY that allows type checking and validation constraints.
Installation
pip install anyvalue
uv add anyvalue
Basic Usage
from anyvalue import AnyValue
from annotated_types import Ge, Le, Len, Predicate
from datetime import datetime
from unittest.mock import Mock
# Basic type matching
assert 42 == AnyValue(int)
assert "hello" == AnyValue(str)
assert datetime.now() == AnyValue(datetime)
# Multiple types with union operator
assert 42 == AnyValue(int | float)
assert "test" == AnyValue(str | bytes)
# None support
assert None == AnyValue(None)
assert None == AnyValue(str | None)
assert 42 == AnyValue(int | None)
# Validation constraints
assert 42 == AnyValue(int, Ge(0)) # Non-negative integer
assert "hello" == AnyValue(str, Len(5, 5)) # String of length 5
assert 99 == AnyValue(int, Ge(0), Le(100)) # Integer between 0 and 100
# Predicate validators
is_even = Predicate(lambda x: x % 2 == 0)
assert 42 == AnyValue(int, is_even)
# Custom callable validators
def is_palindrome(s: str) -> bool:
return s == s[::-1]
assert "racecar" == AnyValue(str, is_palindrome)
# Integration with unittest.mock
mock_func = Mock()
mock_func(42, "test", datetime.now())
# Verify calls with flexible matching
mock_func.assert_called_once_with(
AnyValue(int, Ge(0)),
AnyValue(str, Len(4, 10)),
AnyValue(datetime)
)
Key Features
- Type checking: Accept specific types or union of types
- None support: Explicitly allow or disallow None values
- Validation constraints: Use
annotated-typesfor advanced validation (length, ranges, patterns, etc.) - Mock integration: Works seamlessly with
unittest.mockfor validating call arguments
Development
Setup environment
We use uv to manage the development environment and production build, and just to manage command shortcuts. Ensure they are installed on your system.
Run unit tests
You can run all the tests with:
just test
Format the code
Execute the following command to apply linting and check typing:
just lint
Publish a new version
You can bump the version, create a commit and associated tag with one command:
just version patch
just version minor
just version major
Your default Git text editor will open so you can add information about the release.
When you push the tag on GitHub, the workflow will automatically publish it on PyPi and a GitHub release will be created as draft.
Serve the documentation
You can serve the Mkdocs documentation with:
just docs-serve
It'll automatically watch for changes in your code.
License
This project is licensed under the terms of the MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file anyvalue-0.1.0.tar.gz.
File metadata
- Download URL: anyvalue-0.1.0.tar.gz
- Upload date:
- Size: 66.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
40d971fb6638036adc5e2dfbfe5d5c7631a14c2ca24555d96e6f640add09a964
|
|
| MD5 |
011ddfe690476a4867b9969f5dd77d45
|
|
| BLAKE2b-256 |
aa174616f0bdc27c2ee3aef6e115f468cb85609ea3d96b50be17b66e1332dda6
|
File details
Details for the file anyvalue-0.1.0-py3-none-any.whl.
File metadata
- Download URL: anyvalue-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cc7d3a49eb057e405a6a591dbca1f12d8a21205fac32d1e7a20902179797e03d
|
|
| MD5 |
ef5871a9c93da691409aea3f16924a1a
|
|
| BLAKE2b-256 |
820fc8ea573c1346d585d69af4d6d5184fcd0285935471ae6e046934ab19b74e
|