pytest-dev / pytest-dev/pytest

TypeError with pytest.approx on nested dictionaries

Open
#3,164 14 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

topic: approx type: enhancement
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

{'a': {'b': 0.2 + 0.1}} == pytest.approx({'a': {'b': 0.3}})
~/.conda/lib/python3.6/site-packages/_pytest/python_api.py in __eq__(self, actual)
    195         # other or not.  The abs() calls are for compatibility with complex
    196         # numbers.
--> 197         if math.isnan(abs(self.expected)):
    198             return self.nan_ok and math.isnan(abs(actual))
    199 

TypeError: bad operand type for abs(): 'dict'

Pytest 3.3.2 on python 3.6.4 (Anaconda)
Ubuntu 16.04

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in _pytest/python_api.py around eq line 197 and reproduce the nested-dictionary comparison shown in the issue. Review the existing pytest.approx behavior and relevant tests before determining the expected comparison; done means the example no longer raises a TypeError and the intended result is covered by a test.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.