diff --git a/pyproject.toml b/pyproject.toml index e773d841c..0192497e0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -30,7 +30,7 @@ classifiers = [ ] requires-python = ">=3.10" dependencies = [ - "beartype", + "beartype>=0.12.0", "click", "license_expression", "ply", diff --git a/src/spdx_tools/common/typing/dataclass_with_properties.py b/src/spdx_tools/common/typing/dataclass_with_properties.py index e0b5c9614..1e929894c 100644 --- a/src/spdx_tools/common/typing/dataclass_with_properties.py +++ b/src/spdx_tools/common/typing/dataclass_with_properties.py @@ -3,7 +3,7 @@ # SPDX-License-Identifier: Apache-2.0 from dataclasses import dataclass -from beartype import beartype +from beartype import BeartypeConf, beartype from beartype.roar import BeartypeCallHintParamViolation @@ -23,7 +23,7 @@ def dataclass_with_properties(cls): def make_setter(field_name, field_type): """helper method to avoid late binding when generating functions in a for loop""" - @beartype + @beartype(conf=BeartypeConf(is_color=False)) def set_field(self, value: field_type): setattr(self, f"_{field_name}", value) diff --git a/tests/spdx/model/test_type_error_formatting.py b/tests/spdx/model/test_type_error_formatting.py new file mode 100644 index 000000000..c8b565412 --- /dev/null +++ b/tests/spdx/model/test_type_error_formatting.py @@ -0,0 +1,36 @@ +# SPDX-FileCopyrightText: 2026 spdx contributors +# +# SPDX-License-Identifier: Apache-2.0 +import sys + +import pytest + +from spdx_tools.common.typing.constructor_type_errors import ConstructorTypeErrors +from spdx_tools.spdx.model import Package, SpdxNoAssertion + + +@pytest.mark.parametrize("is_terminal", [True, False]) +def test_constructor_errors_are_plain_text(monkeypatch, is_terminal): + monkeypatch.setattr(sys.stdout, "isatty", lambda: is_terminal) + + with pytest.raises(ConstructorTypeErrors) as error: + Package("SPDXRef-Package", "example", SpdxNoAssertion(), attribution_texts="invalid") + + messages = error.value.get_messages() + assert len(messages) == 1 + assert "attribution_texts" in messages[0] + assert "invalid" in messages[0] + assert "\x1b" not in messages[0] + + +@pytest.mark.parametrize("is_terminal", [True, False]) +def test_setter_errors_are_plain_text(monkeypatch, is_terminal): + monkeypatch.setattr(sys.stdout, "isatty", lambda: is_terminal) + package = Package("SPDXRef-Package", "example", SpdxNoAssertion()) + + with pytest.raises(TypeError) as error: + package.attribution_texts = "invalid" + + assert "attribution_texts" in str(error.value) + assert "invalid" in str(error.value) + assert "\x1b" not in str(error.value)