Skip to content

Commit b3c34c0

Browse files
Refactor TOML error handling and improve pyproject reading logic
- Introduced `InvalidTomlError` to handle parsing errors in TOML files. - Simplified the `read_pyproject` function by removing unnecessary exception handling for file not found. - Updated exception handling in version inference to use `InvalidTomlError` for better clarity in error logging.
1 parent c2f7caf commit b3c34c0

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

src/setuptools_scm/_integration/pyproject_reading.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,7 @@ def read_pyproject(
127127
tool_name: str = "setuptools_scm",
128128
canonical_build_package_name: str = "setuptools-scm",
129129
) -> PyProjectData:
130-
try:
131-
defn = read_toml_content(path)
132-
except FileNotFoundError:
133-
raise
130+
defn = read_toml_content(path)
134131

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

src/setuptools_scm/_integration/setuptools.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from .pyproject_reading import PyProjectData
1313
from .pyproject_reading import read_pyproject
1414
from .setup_cfg import _dist_name_from_legacy
15+
from .toml import InvalidTomlError
1516
from .version_inference import get_version_inference_config
1617

1718
log = logging.getLogger(__name__)
@@ -97,7 +98,7 @@ def version_keyword(
9798
pyproject_data = PyProjectData.empty(
9899
Path("pyproject.toml"), "setuptools_scm"
99100
)
100-
except (LookupError, ValueError) as e:
101+
except InvalidTomlError as e:
101102
log.debug("Configuration issue in pyproject.toml: %s", e)
102103
return
103104

@@ -134,7 +135,7 @@ def infer_version(
134135
except FileNotFoundError:
135136
log.debug("pyproject.toml not found, skipping infer_version")
136137
return
137-
except (LookupError, ValueError) as e:
138+
except InvalidTomlError as e:
138139
log.debug("Configuration issue in pyproject.toml: %s", e)
139140
return
140141

src/setuptools_scm/_integration/toml.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
TOML_LOADER: TypeAlias = Callable[[str], TOML_RESULT]
3030

3131

32+
class InvalidTomlError(ValueError):
33+
"""Raised when TOML data cannot be parsed."""
34+
35+
3236
def read_toml_content(path: Path, default: TOML_RESULT | None = None) -> TOML_RESULT:
3337
try:
3438
data = path.read_text(encoding="utf-8")
@@ -39,7 +43,10 @@ def read_toml_content(path: Path, default: TOML_RESULT | None = None) -> TOML_RE
3943
log.debug("%s missing, presuming default %r", path, default)
4044
return default
4145
else:
42-
return load_toml(data)
46+
try:
47+
return load_toml(data)
48+
except Exception as e: # tomllib/tomli raise different decode errors
49+
raise InvalidTomlError(f"Invalid TOML in {path}") from e
4350

4451

4552
class _CheatTomlData(TypedDict):
@@ -52,8 +59,11 @@ def load_toml_or_inline_map(data: str | None) -> dict[str, Any]:
5259
"""
5360
if not data:
5461
return {}
55-
elif data[0] == "{":
56-
data = "cheat=" + data
57-
loaded: _CheatTomlData = cast(_CheatTomlData, load_toml(data))
58-
return loaded["cheat"]
59-
return load_toml(data)
62+
try:
63+
if data[0] == "{":
64+
data = "cheat=" + data
65+
loaded: _CheatTomlData = cast(_CheatTomlData, load_toml(data))
66+
return loaded["cheat"]
67+
return load_toml(data)
68+
except Exception as e: # tomllib/tomli raise different decode errors
69+
raise InvalidTomlError("Invalid TOML content") from e

0 commit comments

Comments
 (0)