From fbc75bcc13558d3ce2a4a7ed90a6e6c32abbf947 Mon Sep 17 00:00:00 2001 From: Avasam Date: Fri, 9 Aug 2024 15:30:13 -0400 Subject: [PATCH] Raise `TypeError` in `easy_install.CommandSpec.from_param` --- newsfragments/4548.feature.rst | 1 + setuptools/command/easy_install.py | 3 +-- setuptools/tests/test_easy_install.py | 10 ++++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 newsfragments/4548.feature.rst diff --git a/newsfragments/4548.feature.rst b/newsfragments/4548.feature.rst new file mode 100644 index 0000000000..7fcf5f4377 --- /dev/null +++ b/newsfragments/4548.feature.rst @@ -0,0 +1 @@ +Changed the type of error raised by ``setuptools.command.easy_install.CommandSpec.from_param`` on unsupported argument from `AttributeError` to `TypeError` -- by :user:`Avasam` diff --git a/setuptools/command/easy_install.py b/setuptools/command/easy_install.py index 54d1e48449..2770a72b92 100644 --- a/setuptools/command/easy_install.py +++ b/setuptools/command/easy_install.py @@ -2068,8 +2068,7 @@ def from_param(cls, param: Self | str | Iterable[str] | None) -> Self: return cls(param) if param is None: return cls.from_environment() - # AttributeError to keep backwards compatibility, this should really be a TypeError though - raise AttributeError(f"Argument has an unsupported type {type(param)}") + raise TypeError(f"Argument has an unsupported type {type(param)}") @classmethod def from_environment(cls): diff --git a/setuptools/tests/test_easy_install.py b/setuptools/tests/test_easy_install.py index ca6af9667e..de257db80c 100644 --- a/setuptools/tests/test_easy_install.py +++ b/setuptools/tests/test_easy_install.py @@ -1332,6 +1332,16 @@ def test_from_simple_string_uses_shlex(self): assert len(cmd) == 2 assert '"' not in cmd.as_header() + def test_from_param_raises_expected_error(self) -> None: + """ + from_param should raise its own TypeError when the argument's type is unsupported + """ + with pytest.raises(TypeError) as exc_info: + ei.CommandSpec.from_param(object()) # type: ignore[arg-type] # We want a type error here + assert ( + str(exc_info.value) == "Argument has an unsupported type " + ), exc_info.value + class TestWindowsScriptWriter: def test_header(self):