Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend approx to complex objects #8902

Open
ivanovmg opened this issue Jul 13, 2021 · 0 comments
Open

Extend approx to complex objects #8902

ivanovmg opened this issue Jul 13, 2021 · 0 comments
Labels
topic: approx Related to pytest.approx function type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature

Comments

@ivanovmg
Copy link

What's the problem this feature will solve?

Approximate equality of complex objects using pytest.approx.

import pytest


class Item:
    def __init__(self, x):
        self.x = x

a = Item(1)
b = Item(1.0000001)

assert a == pytest.approx(b, rel=1e-4)

b.y = 2
assert a != pytest.approx(b)

a.y = 2.000000001
assert a == pytest.approx(b, rel=1e-5)

Apparently, in this toy example, we can simply compare each of the attributes using pytest.approx.
However, in reality the cases may become more complex, where comparison of the attributes may become fragile (for one objects some attrs were assigned after initialization, while for the others were not).

Describe the solution you'd like

I created a package approx to handle complex objects: https://github.com/ivanovmg/approx

I suppose that some of its functionality can be adopted by pytest if this behavior is desirable.
Basically, we loop through all the fields recursively and check if they are approximately equal.

Alternative Solutions

Delegate the responsibility for the approximate object comparisons to a developer of the class.

Additional context

Related issues:

@Zac-HD Zac-HD added topic: approx Related to pytest.approx function type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature labels Jul 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: approx Related to pytest.approx function type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature
Projects
None yet
Development

No branches or pull requests

2 participants