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

Correct handling of pyproject.toml files lacking a [project] section #104

Merged
merged 5 commits into from
Jul 27, 2024
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
21 changes: 14 additions & 7 deletions src/incremental/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,11 +482,20 @@ def _load_pyproject_toml(toml_path): # type: (str) -> Optional[_IncrementalConf

tool_incremental = _extract_tool_incremental(data)

if tool_incremental is None or tool_incremental == {}:
package = None
if tool_incremental is not None and "name" in tool_incremental:
package = tool_incremental["name"]
if package is None:
try:
package = data["project"]["name"]
except KeyError:
raise ValueError("""\
pass
if package is None:
# We can't proceed without a project name, but that's only an error
# if [tool.incremental] is present.
if tool_incremental is None:
return None
raise ValueError("""\
Couldn't extract the package name from pyproject.toml. Specify it like:

[project]
Expand All @@ -496,12 +505,8 @@ def _load_pyproject_toml(toml_path): # type: (str) -> Optional[_IncrementalConf

[tool.incremental]
name = "Foo"
""")
elif tool_incremental.keys() == {"name"}:
package = tool_incremental["name"]
else:
raise ValueError("Unexpected key(s) in [tool.incremental]")

""")
if not isinstance(package, str):
raise TypeError(
"Package name must be a string, but found {}".format(type(package))
Expand All @@ -526,6 +531,8 @@ def _extract_tool_incremental(data): # type: (Dict[str, object]) -> Optional[Di
if not isinstance(tool_incremental, dict):
raise ValueError("[tool.incremental] must be a table")

if not {"name"}.issuperset(tool_incremental.keys()):
raise ValueError("Unexpected key(s) in [tool.incremental]")
return tool_incremental


Expand Down
1 change: 1 addition & 0 deletions src/incremental/newsfragments/100.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Incremental 24.7.0 would produce an error when parsing the ``pyproject.toml`` of a project that lacked the ``use_incremental=True`` or ``[tool.incremental]`` opt-in markers if that file lacked a ``[project]`` section containing the package name. This could cause a project that only uses ``pyproject.toml`` to configure tools to fail to build if Incremental is installed. Incremental now ignores such projects.
16 changes: 13 additions & 3 deletions src/incremental/tests/test_pyproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,24 @@ def test_fileNotFound(self):
path = os.path.join(cast(str, self.mktemp()), "pyproject.toml")
self.assertIsNone(_load_pyproject_toml(path))

def test_nameMissing(self):
def test_configMissing(self):
"""
`ValueError` is raised when ``[tool.incremental]`` is present but
he project name isn't available.
A ``pyproject.toml`` that exists but provides no relevant configuration
is ignored.
"""
for toml in [
"\n",
"[tool.notincremental]\n",
"[project]\n",
]:
self.assertIsNone(self._loadToml(toml))

def test_nameMissing(self):
"""
`ValueError` is raised when ``[tool.incremental]`` is present but
the project name isn't available.
"""
for toml in [
"[tool.incremental]\n",
"[project]\n[tool.incremental]\n",
]:
Expand Down
11 changes: 11 additions & 0 deletions tests/example_noop/example_noop/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""
An example setuptools project that doesn't use Incremental at all.

This is used to verify that Incremental correctly deactivates itself
when there is no opt-in, even though setuptools always invokes its
hook.
"""

__version__ = "100"

__all__ = ["__version__"]
3 changes: 3 additions & 0 deletions tests/example_noop/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# This file exists, but doesn't contain any [project] or
# [tool.incremental] configuration so Incremental ignores it.
[tool.something]
13 changes: 13 additions & 0 deletions tests/example_noop/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from setuptools import setup

setup(
name="example_noop",
version="100",
packages=["example_noop"],
zip_safe=False,
setup_requires=[
# Ensure Incremental is installed for test purposes
# (but it won't do anything).
"incremental",
],
)
12 changes: 12 additions & 0 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,15 @@ def test_hatch_version_set(self):
# Hatch may wrap the output, so we are flexible about the specifics of whitespace.
suggestion.replace(b".", rb"\.").replace(b" ", b"\\s+"),
)

def test_noop(self):
"""
The Incremental setuptools hook is a silent no-op when there is no Incremental
configuration to be found.
"""
build_and_install(TEST_DIR.child("example_noop"))

import example_noop

self.assertEqual(example_noop.__version__, "100")
self.assertEqual(metadata.version("example_noop"), "100")