From 6cd7a0890aff864fdf955987677aacff040cbb7c Mon Sep 17 00:00:00 2001 From: Anton Agestam Date: Mon, 24 Nov 2025 22:33:10 +0100 Subject: [PATCH] feature: Raise for Marker used as bool --- src/injected/_base.py | 3 +++ tests/test_base.py | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/injected/_base.py b/src/injected/_base.py index b9aec73..c415e5a 100644 --- a/src/injected/_base.py +++ b/src/injected/_base.py @@ -45,6 +45,9 @@ def __str__(self) -> NoReturn: "Marker object does not have a string representation." ) + def __bool__(self) -> NoReturn: + raise NotImplementedError("Marker object does not have a bool representation.") + P = ParamSpec("P", default=Any) R = TypeVar("R", default=Any) diff --git a/tests/test_base.py b/tests/test_base.py index f86a842..e22543c 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -41,6 +41,24 @@ def test_raises_not_implemented_error_when_cast_to_str(self): with pytest.raises(NotImplementedError): str(marker) + def test_raises_not_implemented_error_when_cast_to_bool(self): + request = Request(provider=lambda: None, args=(), kwargs=Map()) + marker = Marker(request=request) + with pytest.raises(NotImplementedError): + bool(marker) + + def test_raises_not_implemented_error_when_used_in_logical_or(self): + request = Request(provider=lambda: None, args=(), kwargs=Map()) + marker = Marker(request=request) + with pytest.raises(NotImplementedError): + marker or True # type: ignore[unreachable] # noqa: B018 + + def test_raises_not_implemented_error_when_used_in_logical_and(self): + request = Request(provider=lambda: None, args=(), kwargs=Map()) + marker = Marker(request=request) + with pytest.raises(NotImplementedError): + marker and True # type: ignore[unreachable] # noqa: B018 + class ContextEvent(enum.Enum): setup = enum.auto()