Skip to content

Include type checker versions in long description #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions stub_uploader/build_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import argparse
import os
import os.path
from pathlib import Path
import shutil
import subprocess
import sys
Expand All @@ -37,6 +38,7 @@
THIRD_PARTY_NAMESPACE,
)
from stub_uploader.metadata import Metadata, read_metadata
from stub_uploader.ts_data import TypeshedData, read_typeshed_data

CHANGELOG = "CHANGELOG.md"

Expand Down Expand Up @@ -108,7 +110,9 @@

DESCRIPTION_OUTRO_TEMPLATE = """
See https://github.com/python/typeshed/blob/main/README.md for more details.
This package was generated from typeshed commit `{commit}`.
This package was generated from typeshed commit `{commit}` and was tested
with mypy {ts_data.mypy_version}, pyright {ts_data.pyright_version}, and
pytype {ts_data.pytype_version}.
""".strip()


Expand Down Expand Up @@ -234,7 +238,11 @@ def add_partial_marker(package_data: dict[str, list[str]], stub_dir: str) -> Non


def generate_setup_file(
build_data: BuildData, metadata: Metadata, version: str, commit: str
build_data: BuildData,
ts_data: TypeshedData,
metadata: Metadata,
version: str,
commit: str,
) -> str:
"""Auto-generate a setup.py file for given distribution using a template."""
all_requirements = [
Expand All @@ -247,7 +255,7 @@ def generate_setup_file(
distribution=build_data.distribution,
stub_distribution=metadata.stub_distribution,
long_description=generate_long_description(
build_data.distribution, commit, metadata
build_data.distribution, commit, ts_data, metadata
),
version=version,
requires=all_requirements,
Expand All @@ -257,7 +265,7 @@ def generate_setup_file(


def generate_long_description(
distribution: str, commit: str, metadata: Metadata
distribution: str, commit: str, ts_data: TypeshedData, metadata: Metadata
) -> str:
extra_description = metadata.extra_description.strip()
parts: list[str] = []
Expand All @@ -280,7 +288,7 @@ def generate_long_description(
)
if metadata.partial:
parts.append(PARTIAL_STUBS_DESCRIPTION)
parts.append(DESCRIPTION_OUTRO_TEMPLATE.format(commit=commit))
parts.append(DESCRIPTION_OUTRO_TEMPLATE.format(commit=commit, ts_data=ts_data))
return "\n\n".join(parts)


Expand All @@ -294,6 +302,7 @@ def main(
Note: the caller should clean the temporary directory where wheel is
created after uploading it.
"""
ts_data = read_typeshed_data(Path(typeshed_dir))
build_data = BuildData(typeshed_dir, distribution)
if build_dir:
tmpdir = build_dir
Expand All @@ -307,7 +316,7 @@ def main(
).stdout.strip()
metadata = read_metadata(typeshed_dir, distribution)
with open(os.path.join(tmpdir, "setup.py"), "w") as f:
f.write(generate_setup_file(build_data, metadata, version, commit))
f.write(generate_setup_file(build_data, ts_data, metadata, version, commit))
copy_stubs(build_data.stub_dir, tmpdir)
copy_changelog(distribution, tmpdir)
current_dir = os.getcwd()
Expand Down
64 changes: 64 additions & 0 deletions stub_uploader/ts_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"""
Information about typeshed.
"""

from __future__ import annotations
import argparse
from collections.abc import Iterable
from dataclasses import dataclass, fields
from packaging.requirements import Requirement
from pathlib import Path
from tomli import load as toml_load


REQUIREMENTS = "requirements-tests.txt"
PYPROJECT = "pyproject.toml"


@dataclass
class TypeshedData:
mypy_version: str
pyright_version: str
pytype_version: str


def read_typeshed_data(typeshed_dir: Path) -> TypeshedData:
with (typeshed_dir / PYPROJECT).open("rb") as f:
pyproject = toml_load(f)
with (typeshed_dir / REQUIREMENTS).open() as f:
requirements = parse_requirements(f)
return TypeshedData(
mypy_version=requirements["mypy"],
pyright_version=pyproject["tool"]["typeshed"]["pyright_version"],
pytype_version=requirements["pytype"],
)


def parse_requirements(stream: Iterable[str]) -> dict[str, str]:
"""Parse all exact requirements from a requirements.txt file.

Only requirements that are pinned to a specific version are returned.
"""
requirements = {}
for line in stream:
line = line.strip()
line = line.split("#")[0] # strip comments
if not line.strip(): # skip empty lines
continue
req = Requirement(line.strip())
if len(req.specifier) != 1:
continue
spec = next(iter(req.specifier))
if spec.operator != "==":
continue
requirements[req.name] = spec.version
return requirements


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("typeshed_dir", help="Path to typeshed checkout directory")
args = parser.parse_args()
data = read_typeshed_data(Path(args.typeshed_dir))
for field in fields(data):
print(f"{field.name}: {getattr(data, field.name)}")
10 changes: 10 additions & 0 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
a typeshed checkout side by side.
"""
import os
from pathlib import Path
import re

import pytest
from packaging.requirements import Requirement
Expand All @@ -21,6 +23,7 @@
verify_external_req,
verify_typeshed_req,
)
from stub_uploader.ts_data import read_typeshed_data

TYPESHED = "../typeshed"

Expand Down Expand Up @@ -145,3 +148,10 @@ def test_dependency_order_single() -> None:
)
def test_recursive_verify(distribution: str) -> None:
recursive_verify(read_metadata(TYPESHED, distribution), TYPESHED)


def test_read_typeshed_data() -> None:
ts_data = read_typeshed_data(Path(TYPESHED))
assert re.match(r"^\d+\.\d+\.\d+$", ts_data.mypy_version)
assert re.match(r"^\d+\.\d+\.\d+$", ts_data.pyright_version)
assert re.match(r"^\d+\.\d+\.\d+$", ts_data.pytype_version)
40 changes: 40 additions & 0 deletions tests/test_unit.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Unit tests for simple helpers should go here."""

from io import StringIO
import os
import tempfile

Expand All @@ -9,6 +10,7 @@
from stub_uploader.build_wheel import collect_setup_entries
from stub_uploader.get_version import compute_incremented_version, ensure_specificity
from stub_uploader.metadata import _UploadedPackages, strip_types_prefix
from stub_uploader.ts_data import parse_requirements


def test_strip_types_prefix() -> None:
Expand Down Expand Up @@ -136,3 +138,41 @@ def test_uploaded_packages() -> None:

with open(file_path) as f:
assert f.read() == "types-SqLaLcHeMy\ntypes-six"


_REQUIREMENTS_TXT = """# This is a comment

pkg1==1.2
pkg2==2.3.4 # This is a comment
pkg3==2023.4.13; python_version >= "3.6"
multispec==3.4.5,>3.4.5
range>=1.2.3
no_version
"""


@pytest.mark.parametrize(
"name,version",
[
("pkg1", "1.2"),
("pkg2", "2.3.4"),
("pkg3", "2023.4.13"),
],
)
def test_parse_requirements__parsed_packages(name: str, version: str) -> None:
requirements = parse_requirements(StringIO(_REQUIREMENTS_TXT))
assert name in requirements, f"package {name} not found"
assert requirements[name] == version, f"package {name} has wrong version"


@pytest.mark.parametrize(
"name",
[
"multispec",
"range",
"no_version",
],
)
def test_parse_requirements__skipped_packages(name: str) -> None:
requirements = parse_requirements(StringIO(_REQUIREMENTS_TXT))
assert name not in requirements, f"package {name} was not skipped"