Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give a default version if the version is dynamic in setup.cfg #1101

Merged
merged 3 commits into from
May 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/1101.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Give a default version if the version is dynamic in `setup.cfg`.
8 changes: 5 additions & 3 deletions pdm/models/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def read_setup_py(cls, file: Path) -> Setup:

return Setup(
name=cls._find_single_string(setup_call, body, "name"),
version=cls._find_single_string(setup_call, body, "version"),
version=cls._find_single_string(setup_call, body, "version") or "0.0.0",
install_requires=cls._find_install_requires(setup_call, body),
extras_require=cls._find_extras_require(setup_call, body),
python_requires=cls._find_single_string(
Expand All @@ -107,12 +107,14 @@ def read_setup_cfg(file: Path) -> Setup:
parser.read(str(file))

name = None
version = None
version = "0.0.0"
if parser.has_option("metadata", "name"):
name = parser.get("metadata", "name")

if parser.has_option("metadata", "version"):
version = parser.get("metadata", "version")
meta_version = parser.get("metadata", "version")
if not meta_version.startswith("attr:"):
version = meta_version

install_requires = []
extras_require: Dict[str, List[str]] = {}
Expand Down
114 changes: 114 additions & 0 deletions tests/models/test_setup_parsing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import pytest

from pdm.models.setup import Setup


@pytest.mark.parametrize(
"content, result",
[
(
"""[metadata]
name = foo
version = 0.1.0
""",
Setup("foo", "0.1.0"),
),
(
"""[metadata]
name = foo
version = attr:foo.__version__
""",
Setup("foo", "0.0.0"),
),
(
"""[metadata]
name = foo
version = 0.1.0

[options]
python_requires = >=3.6
install_requires =
click
requests
[options.extras_require]
tui =
rich
""",
Setup("foo", "0.1.0", ["click", "requests"], {"tui": ["rich"]}, ">=3.6"),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this work if a version is just 2.0. Can you please add this scenario too?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are the same case, as long as it is a valid version string.

),
],
)
def test_parse_setup_cfg(content, result, tmp_path):
tmp_path.joinpath("setup.cfg").write_text(content)
assert Setup.from_directory(tmp_path) == result


@pytest.mark.parametrize(
"content,result",
[
(
"""from setuptools import setup

setup(name="foo", version="0.1.0")
""",
Setup("foo", "0.1.0"),
),
(
"""import setuptools

setuptools.setup(name="foo", version="0.1.0")
""",
Setup("foo", "0.1.0"),
),
(
"""from setuptools import setup

kwargs = {"name": "foo", "version": "0.1.0"}
setup(**kwargs)
""",
Setup("foo", "0.1.0"),
),
(
"""from setuptools import setup
name = 'foo'
setup(name=name, version="0.1.0")
""",
Setup("foo", "0.1.0"),
),
(
"""from setuptools import setup

setup(name="foo", version="0.1.0", install_requires=['click', 'requests'],
python_requires='>=3.6', extras_require={'tui': ['rich']})
""",
Setup("foo", "0.1.0", ["click", "requests"], {"tui": ["rich"]}, ">=3.6"),
),
(
"""from setuptools import setup

version = open('__version__.py').read().strip()

setup(name="foo", version=version)
""",
Setup("foo", "0.0.0"),
),
],
)
def test_parse_setup_py(content, result, tmp_path):
tmp_path.joinpath("setup.py").write_text(content)
assert Setup.from_directory(tmp_path) == result


def test_parse_pyproject_toml(tmp_path):
content = """[project]
name = "foo"
version = "0.1.0"
requires-python = ">=3.6"
dependencies = ["click", "requests"]

[project.optional-dependencies]
tui = ["rich"]
"""
tmp_path.joinpath("pyproject.toml").write_text(content)
result = Setup("foo", "0.1.0", ["click", "requests"], {"tui": ["rich"]}, ">=3.6")
assert Setup.from_directory(tmp_path) == result