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.
Metadata
Release files for anyvalue 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| anyvalue-0.1.0.tar.gz | 66.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| anyvalue-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 72.5 kB
Release files / anyvalue-0.1.0.tar.gz
| Download URL | anyvalue-0.1.0.tar.gz |
|---|---|
| Size | 66.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
40d971fb6638036adc5e2dfbfe5d5c7631a14c2ca24555d96e6f640add09a964
|
|
BLAKE2b-256 checksum How to use checksums |
aa174616f0bdc27c2ee3aef6e115f468cb85609ea3d96b50be17b66e1332dda6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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}
|
Release files / anyvalue-0.1.0-py3-none-any.whl
| Download URL | anyvalue-0.1.0-py3-none-any.whl |
|---|---|
| Size | 5.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
cc7d3a49eb057e405a6a591dbca1f12d8a21205fac32d1e7a20902179797e03d
|
|
BLAKE2b-256 checksum How to use checksums |
820fc8ea573c1346d585d69af4d6d5184fcd0285935471ae6e046934ab19b74e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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}
|