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.

clew/tomlcompat.py:74

Show on the map →

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

2 teststest_status_reports_the_real_package_version_not_the_unknown_fallback, test_the_plugin_ships_a_wiki_command_at_its_pinned_release

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 module

1 more lines, not shown

Activity · 4 steps
lines 87–87module = toml_module()lines 88–88◇ if module is Nonelines 89–93raise 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}" )lines 94–94return module

Flows

toml_module

ModuleType | None clew.tomlcompat.toml_module()

Return a TOML-parsing module, or None when none is importable.

clew/tomlcompat.py:44

Show on the map →

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

Activity · 10 steps
lines 56–56trylines 57–57import tomlliblines 59–59return tomlliblines 60–60except ImportErrorlines 61–61passlines 62–62trylines 63–63import tomlilines 65–65return tomlilines 66–66except ImportErrorlines 67–67return None

Flows