Skip to content

approx on Decimal value fails with default tolerance arguments #3247

@durocher

Description

@durocher

When using default tolerance on a Decimal, approx fails:

>>> from pytest import approx
>>> from decimal import Decimal
>>> approx(Decimal('1'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mike/temp/env/lib/python3.6/site-packages/_pytest/python_api.py", line 175, in __repr__
    vetted_tolerance = '{:.1e}'.format(self.tolerance)
  File "/Users/mike/temp/env/lib/python3.6/site-packages/_pytest/python_api.py", line 244, in tolerance
    relative_tolerance = set_default(self.rel, 1e-6) * abs(self.expected)
TypeError: unsupported operand type(s) for *: 'float' and 'decimal.Decimal'
>>> import sys
>>> sys.version
'3.6.4 (v3.6.4:d48ecebad5, Dec 18 2017, 21:07:28) \n[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]'
>>> import pytest
>>> pytest.__version__
'3.4.1'

but it works fine with rel and/or abs:

>>> approx(foo, rel=Decimal('1e-6'))
1 ± 1.0e-6
>>> approx(foo, abs=Decimal('1e-12'))
1 ± 1.0e-12
$ pip list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
attrs (17.4.0)
pip (9.0.1)
pluggy (0.6.0)
py (1.5.2)
pytest (3.4.1)
setuptools (38.5.1)
six (1.11.0)
tox (2.9.1)
virtualenv (15.1.0)
wheel (0.30.0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions