Skip to main content

Fully configurable unpacking and comparison logic for arbitrary objects

Project description

compyre

What is this?

compyre provides container unpacking and elementwise equality comparisons for arbitrary objects using their native functionality. It offers convenient defaults, while being fully configurable.

"X all the Y" meme with "compyre all the things" as text

Why do I need it?

Have you ever found yourself in a situation where you needed to test a potentially nested container of values against a reference? pytest, the de facto standard test framework for Python, features awesome failure reporting for builtin types such as dictionaries, lists, integers, strings, and so on.

But what about other common types that come with their own comparison logic, e.g. numpy.ndarray and numpy.testing.assert_allclose? How do you compare a dictionary or worse a dataclass of these?

  • Did you ever skip writing a proper test in such a situation and opted to write a simple, but incomplete one instead?
  • If not, did you write the test as loop over the individual elements, and later spend more time debugging, because you have no way of knowing for which element the test failure happened?
  • If not, is manually writing out all the assertions and maintaining them keeping you from working on the stuff that actually matters for your application or library?

If you have answered "yes" for any of the questions above, compyre was made for you.

How do I get started?

Most basic cases can be covered by compyre.equal or compyre.assert_equal. The former provides a boolean check, while the latter raises an AssertionError with information what elements mismatch and why.

import dataclasses

import numpy as np

import compyre

@dataclasses.dataclass
class MyObject:
    id: str
    data: list[np.ndarray]

expected = MyObject(
    id="foo",
    data=[np.array([1, 2]), np.array([3, 4])],
)

actual = MyObject(
    id="bar",
    data=[np.array([1, 2]), np.array([3, 5])],
)

compyre.assert_equal(actual, expected)
AssertionError: comparison resulted in 2 error(s):

id
    AssertionError: 'bar' != 'foo'
data.1
    AssertionError: 
    Not equal to tolerance rtol=1e-07, atol=0

    Mismatched elements: 1 / 2 (50%)
    Max absolute difference among violations: 1
    Max relative difference among violations: 0.25
     ACTUAL: array([3, 5])
     DESIRED: array([3, 4])

How do I learn more?

Please have a look at the documentation.

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

compyre-0.1.0.tar.gz (263.5 kB view details)

Uploaded Source

Built Distribution

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

compyre-0.1.0-py3-none-any.whl (16.8 kB view details)

Uploaded Python 3

File details

Details for the file compyre-0.1.0.tar.gz.

File metadata

  • Download URL: compyre-0.1.0.tar.gz
  • Upload date:
  • Size: 263.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.7.12

File hashes

Hashes for compyre-0.1.0.tar.gz
Algorithm Hash digest
SHA256 69174822c4b561a9491722b43d596f911e88477095b8915710f23e58e4aa6617
MD5 ca65dada8197d7176612d47210c66927
BLAKE2b-256 4947cbf9ad6708522f0cb0348faeaad01c2b9c0a45f731114f14205a5e098977

See more details on using hashes here.

File details

Details for the file compyre-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: compyre-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 16.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.7.12

File hashes

Hashes for compyre-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a118838ef4bec1d40116506c4615ef5e3907473530146315e76a612a658aaea9
MD5 8d23c8e04cd341b1425f6ee95d5aa9d2
BLAKE2b-256 b0d9cc69b3f4a5c22a1bd209ef4c7a1621cd0f664e0998a35077a9ea572e93d0

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