Skip to content

Commit 6c335cd

Browse files
Refactor PyProjectData initialization and improve error handling for missing sections
- Introduced a class method `empty` in PyProjectData for creating empty instances. - Simplified the `read_pyproject` function to utilize the new `empty` method when the configuration file is missing. - Enhanced logging for missing tool sections in the TOML configuration.
1 parent 869ae51 commit 6c335cd

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

src/setuptools_scm/_integration/pyproject_reading.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ def for_testing(
5757
project_present=project_present,
5858
)
5959

60+
@classmethod
61+
def empty(cls, path: Path, tool_name: str) -> PyProjectData:
62+
return cls(
63+
path=path,
64+
tool_name=tool_name,
65+
project={},
66+
section={},
67+
is_required=False,
68+
section_present=False,
69+
project_present=False,
70+
)
71+
6072
@property
6173
def project_name(self) -> str | None:
6274
return self.project.get("name")
@@ -121,29 +133,23 @@ def read_pyproject(
121133
except FileNotFoundError:
122134
if missing_file_ok:
123135
log.warning("File %s not found, using empty configuration", path)
124-
return PyProjectData(
125-
path=path,
126-
tool_name=tool_name,
127-
project={},
128-
section={},
129-
is_required=False,
130-
section_present=False,
131-
project_present=False,
132-
)
136+
return PyProjectData.empty(path=path, tool_name=tool_name)
133137
else:
134138
raise
135139

136140
requires: list[str] = defn.get("build-system", {}).get("requires", [])
137141
is_required = has_build_package(requires, canonical_build_package_name)
138142

139-
try:
140-
section = defn.get("tool", {})[tool_name]
141-
section_present = True
142-
except LookupError:
143-
error = f"{path} does not contain a tool.{tool_name} section"
144-
log.warning("toml section missing %r", error, exc_info=True)
145-
section = {}
146-
section_present = False
143+
tool_section = defn.get("tool", {})
144+
section = tool_section.get(tool_name, {})
145+
section_present = tool_name in tool_section
146+
147+
if not section_present:
148+
log.warning(
149+
"toml section missing %r does not contain a tool.%s section",
150+
path,
151+
tool_name,
152+
)
147153

148154
project = defn.get("project", {})
149155
project_present = "project" in defn

0 commit comments

Comments
 (0)