{"id":"05b56d3b2fda749a","repo":"pytest-dev/pytest","slug":"approx-is-not-supported-in-a-boolean-context-di","errorCode":null,"errorMessage":"approx() is not supported in a boolean context.\nDid you mean: `assert a == approx(b)`?","messagePattern":"approx\\(\\) is not supported in a boolean context\\.\nDid you mean: `assert a == approx\\(b\\)`\\?","errorType":"exception","errorClass":"AssertionError","httpStatus":null,"severity":"error","filePath":"src/_pytest/approx.py","lineNumber":112,"sourceCode":"    @abc.abstractmethod\n    def __repr__(self) -> str:\n        raise NotImplementedError\n\n    def _repr_compare(self, other_side) -> list[str]:\n        return [\n            \"comparison failed\",\n            f\"Obtained: {other_side}\",\n            f\"Expected: {self}\",\n        ]\n\n    def __eq__(self, actual) -> bool:\n        return all(\n            a == self._approx_scalar(x) for a, x in self._yield_comparisons(actual)\n        )\n\n    def __bool__(self):\n        __tracebackhide__ = True\n        raise AssertionError(\n            \"approx() is not supported in a boolean context.\\nDid you mean: `assert a == approx(b)`?\"\n        )\n\n    # Ignore type because of https://github.com/python/mypy/issues/4266.\n    __hash__ = None  # type: ignore\n\n    def _approx_scalar(self, x) -> ApproxScalar[Any] | ApproxTimedelta:\n        if isinstance(x, Decimal):\n            return ApproxDecimal(x, rel=self.rel, abs=self.abs, nan_ok=self.nan_ok)\n        if isinstance(x, (datetime, timedelta)):\n            return ApproxTimedelta(x, rel=self.rel, abs=self.abs, nan_ok=self.nan_ok)\n        return ApproxScalar(x, rel=self.rel, abs=self.abs, nan_ok=self.nan_ok)\n\n    def _yield_comparisons(self, actual: object):\n        \"\"\"Yield all the pairs of numbers to be compared.\n\n        This is used to implement the `__eq__` method.\n        \"\"\"","sourceCodeStart":94,"sourceCodeEnd":130,"githubUrl":"https://github.com/pytest-dev/pytest/blob/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/approx.py#L94-L130","documentation":"An approx() object is not a boolean; it is a comparison helper meant to be used only as `assert a == approx(b)`. If used in a boolean context (if, bool(), conditional, or bare `assert approx(b)`), pytest raises AssertionError in __bool__ because the result is meaningless without an actual value to compare against.","triggerScenarios":"Writing `if approx(x):`, `bool(approx(x))`, or `assert approx(x)` without a comparison. The __bool__ dunder is invoked, raising AssertionError with a hint.","commonSituations":"Developers unfamiliar with approx() who treat it like a predicate, or who write `assert approx(x)` instead of `assert a == approx(x)`.","solutions":["Use approx() only in an equality comparison: assert actual == approx(expected).","Replace `if approx(x):` with a concrete comparison: if abs(actual - expected) < tol.","Review the hint in the error message: 'Did you mean: assert a == approx(b)?'"],"exampleFix":"# before\nassert approx(3.0)  # missing the actual value\n\n# after\nassert result == approx(3.0)","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always use approx() in an equality comparison: assert actual == approx(expected).","Never use approx() in an if/while/bool() context or bare assert.","Remember approx(x) returns a comparison object, not a boolean predicate."],"tags":["approx","assertion","api-misuse","assertion-error"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}