Minor improvements to stubtest_third_party.py (#10605)

(1) Give a nicer error message if you try to run stubtest on non-existent stubs.
(2) Print the Python version to the terminal if there's an error, as well as the output of pip freeze.
This commit is contained in:
Alex Waygood
2023-08-22 19:57:14 +01:00
committed by GitHub
parent 32b750b6aa
commit a094aa09c2
2 changed files with 23 additions and 6 deletions

View File

@@ -20,6 +20,7 @@ from packaging.version import Version
from utils import cache
__all__ = [
"NoSuchStubError",
"StubMetadata",
"PackageDependencies",
"StubtestSettings",
@@ -160,6 +161,10 @@ _KNOWN_METADATA_TOOL_FIELDS: Final = {
_DIST_NAME_RE: Final = re.compile(r"^[a-z0-9]([a-z0-9._-]*[a-z0-9])?$", re.IGNORECASE)
class NoSuchStubError(ValueError):
"""Raise NoSuchStubError to indicate that a stubs/{distribution} directory doesn't exist"""
@cache
def read_metadata(distribution: str) -> StubMetadata:
"""Return an object describing the metadata of a stub as given in the METADATA.toml file.
@@ -169,8 +174,11 @@ def read_metadata(distribution: str) -> StubMetadata:
Use `read_dependencies` if you need to parse the dependencies
given in the `requires` field, for example.
"""
with Path("stubs", distribution, "METADATA.toml").open("rb") as f:
data: dict[str, object] = tomli.load(f)
try:
with Path("stubs", distribution, "METADATA.toml").open("rb") as f:
data: dict[str, object] = tomli.load(f)
except FileNotFoundError:
raise NoSuchStubError(f"Typeshed has no stubs for {distribution!r}!") from None
unknown_metadata_fields = data.keys() - _KNOWN_METADATA_FIELDS
assert not unknown_metadata_fields, f"Unexpected keys in METADATA.toml for {distribution!r}: {unknown_metadata_fields}"