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

pytest.approx fails with TypeError: unsupported operand type(s) for -: 'float' and 'NoneType' #10702

Closed
milanwiedemann opened this issue Feb 3, 2023 · 3 comments · Fixed by #11178
Labels
topic: approx related to pytest.approx function

Comments

@milanwiedemann
Copy link

When using approx to test float and one of the objects in the assert statement contain None I see the following TypeError:

TypeError: unsupported operand type(s) for -: 'float' and 'NoneType'.

Minimal example

Test

import pytest


# Expecting assertion error with differing item
# Instead I see "TypeError: unsupported operand type(s) for -: 'float' and 'NoneType'."
def test_pytest_none_approx():
    actual_result = {"a": 1.2}
    expected_result = {"a": None}
    approx_expected_result = pytest.approx(expected_result)
    assert approx_expected_result == actual_result

Output

E       AssertionError: assert approx({'a': 1.2 ± 1.2e-06}) == {'a': None}
E         (pytest_assertion plugin: representation of details failed: /Users/milanwiedemann/.pyenv/versions/3.10.4/lib/python3.10/site-packages/_pytest/python_api.py:270: TypeError: unsupported operand type(s) for -: 'float' and 'NoneType'.
E          Probably an object has a faulty __repr__.)

pip list

Package        Version
-------------- -------
attrs          22.2.0
exceptiongroup 1.1.0
iniconfig      2.0.0
packaging      23.0
pip            22.0.4
pluggy         1.0.0
pytest         7.2.1
setuptools     58.1.0
tomli          2.0.1

Cersions of OS and pytest

  • macOS 12.6.3
  • python 3.10.4
  • pytest 7.2.1
@RonnyPfannschmidt
Copy link
Member

We should probably error earlier, none is not approx-able

It seems to be a mistake to let approx take dict for convenience

@Zac-HD Zac-HD added the topic: approx related to pytest.approx function label Feb 10, 2023
@kenny-y-dev
Copy link
Contributor

It appears that None is currently functional when passing it directly to approx, but not when passed as part of a dictionary (which gets processed by the ApproxMapping class, and a subtraction operation on the dict value is throwing the error).

I do agree that None shouldn't be approx-able based on what the function should be doing, though an early error on None may break existing tests, so should ApproxMapping instead be adjusted to better handle None? The documentation does include dictionaries with None values, and keeping the dict functionality may be helpful when dealing with multiple values from the same function run in a way that parameterize wouldn't cover.

I'm open to work on any of the above options, let me know the preferred direction!

@nicoddemus
Copy link
Member

so should ApproxMapping instead be adjusted to better handle None?

I vote for that one; deprecating None would be a whole can of worms, but handling None seems to be the way to fix this, given the current failure is not acceptable.

jsuchenia pushed a commit to jsuchenia/adventofcode that referenced this issue Dec 2, 2023
This PR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [pytest](https://docs.pytest.org/en/latest/) ([source](https://github.com/pytest-dev/pytest), [changelog](https://docs.pytest.org/en/stable/changelog.html)) | patch | `==7.4.0` -> `==7.4.2` |

---

### Release Notes

<details>
<summary>pytest-dev/pytest (pytest)</summary>

### [`v7.4.2`](https://github.com/pytest-dev/pytest/releases/tag/7.4.2): pytest 7.4.2 (2023-09-07)

[Compare Source](pytest-dev/pytest@7.4.1...7.4.2)

### Bug Fixes

-   [#&#8203;11237](pytest-dev/pytest#11237): Fix doctest collection of `functools.cached_property` objects.

-   [#&#8203;11306](pytest-dev/pytest#11306): Fixed bug using `--importmode=importlib` which would cause package `__init__.py` files to be imported more than once in some cases.

-   [#&#8203;11367](pytest-dev/pytest#11367): Fixed bug where `user_properties` where not being saved in the JUnit XML file if a fixture failed during teardown.

-   [#&#8203;11394](pytest-dev/pytest#11394): Fixed crash when parsing long command line arguments that might be interpreted as files.

### Improved Documentation

-   [#&#8203;11391](pytest-dev/pytest#11391): Improved disclaimer on pytest plugin reference page to better indicate this is an automated, non-curated listing.

### [`v7.4.1`](https://github.com/pytest-dev/pytest/releases/tag/7.4.1): pytest 7.4.1 (2023-09-02)

[Compare Source](pytest-dev/pytest@7.4.0...7.4.1)

## Bug Fixes

-   [#&#8203;10337](pytest-dev/pytest#10337): Fixed bug where fake intermediate modules generated by `--import-mode=importlib` would not include the
    child modules as attributes of the parent modules.

-   [#&#8203;10702](pytest-dev/pytest#10702): Fixed error assertion handling in `pytest.approx` when `None` is an expected or received value when comparing dictionaries.

-   [#&#8203;10811](pytest-dev/pytest#10811): Fixed issue when using `--import-mode=importlib` together with `--doctest-modules` that caused modules
    to be imported more than once, causing problems with modules that have import side effects.

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNi44LjExIiwidXBkYXRlZEluVmVyIjoiMzYuMTA3LjIiLCJ0YXJnZXRCcmFuY2giOiJtYXN0ZXIifQ==-->

Reviewed-on: https://git.apud.pl/jacek/adventofcode/pulls/32
Co-authored-by: Renovate <renovate@apud.pl>
Co-committed-by: Renovate <renovate@apud.pl>
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants