From 611033a7335f3c8e2b74dd58688fb9021cf84a5b Mon Sep 17 00:00:00 2001 From: ento Date: Tue, 17 Oct 2023 09:59:14 -0800 Subject: [PATCH] Read description field from setup files (#8545) --- src/poetry/utils/setup_reader.py | 7 +++ tests/utils/fixtures/setups/minimal/setup.py | 6 +++ .../setups/with-setup-cfg-minimal/setup.cfg | 0 .../setups/with-setup-cfg-minimal/setup.py | 6 +++ .../fixtures/setups/with-setup-cfg/setup.cfg | 1 + tests/utils/test_setup_reader.py | 52 +++++++++++++++++++ 6 files changed, 72 insertions(+) create mode 100644 tests/utils/fixtures/setups/minimal/setup.py create mode 100644 tests/utils/fixtures/setups/with-setup-cfg-minimal/setup.cfg create mode 100644 tests/utils/fixtures/setups/with-setup-cfg-minimal/setup.py diff --git a/src/poetry/utils/setup_reader.py b/src/poetry/utils/setup_reader.py index 921768722ac..ffc10a13a87 100644 --- a/src/poetry/utils/setup_reader.py +++ b/src/poetry/utils/setup_reader.py @@ -22,6 +22,7 @@ class SetupReader: DEFAULT: ClassVar[dict[str, Any]] = { "name": None, "version": None, + "description": None, "install_requires": [], "extras_require": {}, "python_requires": None, @@ -62,6 +63,7 @@ def read_setup_py(self, filepath: Path) -> dict[str, Any]: call, body = setup_call result["name"] = self._find_single_string(call, body, "name") result["version"] = self._find_single_string(call, body, "version") + result["description"] = self._find_single_string(call, body, "description") result["install_requires"] = self._find_install_requires(call, body) result["extras_require"] = self._find_extras_require(call, body) result["python_requires"] = self._find_single_string( @@ -77,12 +79,16 @@ def read_setup_cfg(self, filepath: Path) -> dict[str, Any]: name = None version = None + description = None if parser.has_option("metadata", "name"): name = parser.get("metadata", "name") if parser.has_option("metadata", "version"): version = Version.parse(parser.get("metadata", "version")).text + if parser.has_option("metadata", "description"): + description = parser.get("metadata", "description") + install_requires = [] extras_require: dict[str, list[str]] = {} python_requires = None @@ -112,6 +118,7 @@ def read_setup_cfg(self, filepath: Path) -> dict[str, Any]: return { "name": name, "version": version, + "description": description, "install_requires": install_requires, "extras_require": extras_require, "python_requires": python_requires, diff --git a/tests/utils/fixtures/setups/minimal/setup.py b/tests/utils/fixtures/setups/minimal/setup.py new file mode 100644 index 00000000000..536cf1785ef --- /dev/null +++ b/tests/utils/fixtures/setups/minimal/setup.py @@ -0,0 +1,6 @@ +from __future__ import annotations + +from setuptools import setup + + +setup() diff --git a/tests/utils/fixtures/setups/with-setup-cfg-minimal/setup.cfg b/tests/utils/fixtures/setups/with-setup-cfg-minimal/setup.cfg new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/utils/fixtures/setups/with-setup-cfg-minimal/setup.py b/tests/utils/fixtures/setups/with-setup-cfg-minimal/setup.py new file mode 100644 index 00000000000..536cf1785ef --- /dev/null +++ b/tests/utils/fixtures/setups/with-setup-cfg-minimal/setup.py @@ -0,0 +1,6 @@ +from __future__ import annotations + +from setuptools import setup + + +setup() diff --git a/tests/utils/fixtures/setups/with-setup-cfg/setup.cfg b/tests/utils/fixtures/setups/with-setup-cfg/setup.cfg index 7f566a99ad1..edae13166af 100644 --- a/tests/utils/fixtures/setups/with-setup-cfg/setup.cfg +++ b/tests/utils/fixtures/setups/with-setup-cfg/setup.cfg @@ -1,6 +1,7 @@ [metadata] name = with-setup-cfg version = 1.2.3 +description = Package with setup.cfg [options] zip_safe = true diff --git a/tests/utils/test_setup_reader.py b/tests/utils/test_setup_reader.py index f6752853f36..cd75aa3eaa8 100644 --- a/tests/utils/test_setup_reader.py +++ b/tests/utils/test_setup_reader.py @@ -22,6 +22,24 @@ def _setup(name: str) -> Path: return _setup +def test_setup_reader_read_minimal_setup_py(setup: Callable[[str], Path]) -> None: + result = SetupReader.read_from_directory(setup("minimal")) + + expected_name = None + expected_version = None + expected_description = None + expected_install_requires: list[str] = [] + expected_extras_require: dict[str, list[str]] = {} + expected_python_requires = None + + assert result["name"] == expected_name + assert result["version"] == expected_version + assert result["description"] == expected_description + assert result["install_requires"] == expected_install_requires + assert result["extras_require"] == expected_extras_require + assert result["python_requires"] == expected_python_requires + + def test_setup_reader_read_first_level_setup_call_with_direct_types( setup: Callable[[str], Path] ) -> None: @@ -29,6 +47,7 @@ def test_setup_reader_read_first_level_setup_call_with_direct_types( expected_name = "Flask" expected_version = None + expected_description = "A simple framework for building complex web applications." expected_install_requires = [ "Werkzeug>=0.14", "Jinja2>=2.10", @@ -51,6 +70,7 @@ def test_setup_reader_read_first_level_setup_call_with_direct_types( assert result["name"] == expected_name assert result["version"] == expected_version + assert result["description"] == expected_description assert result["install_requires"] == expected_install_requires assert result["extras_require"] == expected_extras_require assert result["python_requires"] == expected_python_requires @@ -63,6 +83,7 @@ def test_setup_reader_read_first_level_setup_call_with_variables( expected_name = None expected_version = None + expected_description = None expected_install_requires = [ "chardet>=3.0.2,<3.1.0", "idna>=2.5,<2.8", @@ -78,6 +99,7 @@ def test_setup_reader_read_first_level_setup_call_with_variables( assert result["name"] == expected_name assert result["version"] == expected_version + assert result["description"] == expected_description assert result["install_requires"] == expected_install_requires assert result["extras_require"] == expected_extras_require assert result["python_requires"] == expected_python_requires @@ -90,6 +112,7 @@ def test_setup_reader_read_sub_level_setup_call_with_direct_types( expected_name = "SQLAlchemy" expected_version = None + expected_description = "Database Abstraction Library" expected_install_requires: list[str] = [] expected_extras_require = { "mysql": ["mysqlclient"], @@ -104,6 +127,7 @@ def test_setup_reader_read_sub_level_setup_call_with_direct_types( assert result["name"] == expected_name assert result["version"] == expected_version + assert result["description"] == expected_description assert result["install_requires"] == expected_install_requires assert result["extras_require"] == expected_extras_require assert result["python_requires"] is None @@ -114,6 +138,7 @@ def test_setup_reader_read_setup_cfg(setup: Callable[[str], Path]) -> None: expected_name = "with-setup-cfg" expected_version = "1.2.3" + expected_description = "Package with setup.cfg" expected_install_requires = ["six", "tomlkit"] expected_extras_require = { "validation": ["cerberus"], @@ -123,6 +148,25 @@ def test_setup_reader_read_setup_cfg(setup: Callable[[str], Path]) -> None: assert result["name"] == expected_name assert result["version"] == expected_version + assert result["description"] == expected_description + assert result["install_requires"] == expected_install_requires + assert result["extras_require"] == expected_extras_require + assert result["python_requires"] == expected_python_requires + + +def test_setup_reader_read_minimal_setup_cfg(setup: Callable[[str], Path]) -> None: + result = SetupReader.read_from_directory(setup("with-setup-cfg-minimal")) + + expected_name = None + expected_version = None + expected_description = None + expected_install_requires: list[str] = [] + expected_extras_require: dict[str, list[str]] = {} + expected_python_requires = None + + assert result["name"] == expected_name + assert result["version"] == expected_version + assert result["description"] == expected_description assert result["install_requires"] == expected_install_requires assert result["extras_require"] == expected_extras_require assert result["python_requires"] == expected_python_requires @@ -138,12 +182,14 @@ def test_setup_reader_read_setup_kwargs(setup: Callable[[str], Path]) -> None: expected_name = "pendulum" expected_version = "2.0.4" + expected_description = "Python datetimes made easy" expected_install_requires = ["python-dateutil>=2.6,<3.0", "pytzdata>=2018.3"] expected_extras_require = {':python_version < "3.5"': ["typing>=3.6,<4.0"]} expected_python_requires = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" assert result["name"] == expected_name assert result["version"] == expected_version + assert result["description"] == expected_description assert result["install_requires"] == expected_install_requires assert result["extras_require"] == expected_extras_require assert result["python_requires"] == expected_python_requires @@ -154,12 +200,14 @@ def test_setup_reader_read_setup_call_in_main(setup: Callable[[str], Path]) -> N expected_name = "PyYAML" expected_version = "3.13" + expected_description = "YAML parser and emitter for Python" expected_install_requires: list[str] = [] expected_extras_require: dict[str, list[str]] = {} expected_python_requires = None assert result["name"] == expected_name assert result["version"] == expected_version + assert result["description"] == expected_description assert result["install_requires"] == expected_install_requires assert result["extras_require"] == expected_extras_require assert result["python_requires"] == expected_python_requires @@ -172,12 +220,14 @@ def test_setup_reader_read_extras_require_with_variables( expected_name = "extras_require_with_vars" expected_version = "0.0.1" + expected_description = "test setup_reader.py" expected_install_requires: list[str] = [] expected_extras_require = {"test": ["pytest"]} expected_python_requires = None assert result["name"] == expected_name assert result["version"] == expected_version + assert result["description"] == expected_description assert result["install_requires"] == expected_install_requires assert result["extras_require"] == expected_extras_require assert result["python_requires"] == expected_python_requires @@ -188,6 +238,8 @@ def test_setup_reader_setuptools(setup: Callable[[str], Path]) -> None: expected_name = "my_package" expected_version = "0.1.2" + expected_description = "Just a description" assert result["name"] == expected_name assert result["version"] == expected_version + assert result["description"] == expected_description