tomlcompat.py¶
clew/tomlcompat.py
The single TOML-parser import in this package.
require_toml_module
ModuleType clew.tomlcompat.require_toml_module()
Return a TOML-parsing module, raising when none is importable.
Called by _load_declared_keys, _parse, _pyproject, _read_toml, _read_toml, _version, test_require_toml_module_raises_when_absent, test_wheel_contains_every_console_script_module
Tested by
Calls toml_module
Source · 21 lines
def require_toml_module() -> ModuleType:
"""The default accessor. The raise is the whole feature: on 3.10 without the
backport the alternative is a build that succeeds while silently skipping
every `pyproject.toml` it was supposed to read.
The message names the missing distribution rather than describing the
condition, because the only way to reach it is an install whose conditional
`tomli` dependency did not arrive, and `pip install tomli` is the fix.
@brief Import a TOML parser or refuse.
@return The parser module.
@version 1
"""
module = toml_module()
if module is None:
raise TomlParserUnavailableError(
"no TOML parser available: 'tomllib' is stdlib only on Python 3.11+, "
f"and the '{_BACKPORT}' backport is not installed. "
f"Install it with: pip install {_BACKPORT}"
)
return module1 more lines, not shown
Activity · 4 steps
Flows
toml_module
ModuleType | None clew.tomlcompat.toml_module()
Return a TOML-parsing module, or None when none is importable.
Called by _checkout_version, _toml_accepted, discover, parse_ingot_manifest, require_toml_module, test_toml_module_is_available_here, test_toml_module_returns_none_when_no_parser
Calls none
Source · 26 lines
def toml_module() -> ModuleType | None:
"""Prefers the stdlib module so a 3.11+ interpreter never depends on the
backport being present, and never prefers it if it happens to be.
Returns None rather than raising, for the caller that is probing rather than
parsing. A caller that needs a document should use `require_toml_module`;
a caller that maps None onto an empty result is reintroducing gh#23.
@brief Import tomllib, falling back to tomli.
@return The parser module, or None when neither imports.
@version 2
"""
try:
import tomllib
return tomllib
except ImportError:
pass
try:
import tomli
return tomli
except ImportError:
return None