Skip to content
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

setuptools 69 includes type information by default #160

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ tomli

# build and upload
build
setuptools
setuptools >= 69.0.0
twine
wheel

Expand Down
29 changes: 9 additions & 20 deletions stub_uploader/build_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,30 +216,24 @@ def is_namespace_package(path: Path) -> bool:
return not (path / "__init__.pyi").exists()


def find_stub_files(top: str) -> list[str]:
"""Find all stub files for a given package, relative to package root.
def validate_only_stub_files(top: str) -> None:
"""Check all stub files for a given package, relative to package root.

Raise if we find any unknown file extensions during collection.
Raise if we find any unknown file extensions or invalid names.
"""
result: list[str] = []
for root, _, files in os.walk(top):
for _, _, files in os.walk(top):
for file in files:
if file.endswith(".pyi"):
name, _ = os.path.splitext(file)
assert (
name.isidentifier()
), "All file names must be valid Python modules"
result.append(os.path.relpath(os.path.join(root, file), top))
elif file == "py.typed":
result.append(os.path.relpath(os.path.join(root, file), top))
Comment on lines -233 to -234
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By removing this, I'd expect this function to crash, as the build directory will have py.typed files and the next elif branch checks for unknown files.

if not (name.isidentifier()):
raise ValueError("All file names must be valid Python modules")
elif not file.endswith((".md", ".rst")):
# Allow having README docs, as some stubs have these (e.g. click).
if (
subprocess.run(["git", "check-ignore", file], cwd=top).returncode
!= 0
):
raise ValueError(f"Only stub files are allowed, not {file!r}")
return sorted(result)


def copy_stubs(base_dir: Path, dst: Path) -> None:
Expand Down Expand Up @@ -307,18 +301,13 @@ def collect_package_data(base_path: Path) -> PackageData:
continue
elif entry.is_file() and entry.suffix == ".pyi":
pkg_name = entry.stem
# Module -> package transformation is done while copying.
stub_files = ["__init__.pyi"]
elif entry.is_dir():
pkg_name = entry.name
stub_files = find_stub_files(str(entry))
validate_only_stub_files(str(entry))
else:
raise ValueError(f"Only stub files are allowed, not {entry.name!r}")
package_data[pkg_name + SUFFIX] = [*stub_files, META]
pkg_data = PackageData(base_path, package_data)
for package in pkg_data.top_level_non_namespace_packages:
pkg_data.add_file(package, "py.typed")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can see, this is the only call of PackageData.add_file(), so we can remove that method.

return pkg_data
package_data[pkg_name + SUFFIX] = [META]
return PackageData(base_path, package_data)


def is_ignored_distribution_file(entry: Path) -> bool:
Expand Down
46 changes: 5 additions & 41 deletions tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,38 +117,15 @@ def test_collect_package_data() -> None:
stubs = Path("data") / "test_typeshed" / "stubs"
pkg_data = collect_package_data(stubs / "singlefilepkg")
assert pkg_data.top_level_packages == ["singlefilepkg-stubs"]
assert pkg_data.package_data == (
{"singlefilepkg-stubs": ["__init__.pyi", "METADATA.toml", "py.typed"]}
)
assert pkg_data.package_data == ({"singlefilepkg-stubs": ["METADATA.toml"]})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests were checking for strings in package_data, but I don't think there's any test to ensure that the right files are actually collected.


pkg_data = collect_package_data(stubs / "multifilepkg")
assert pkg_data.top_level_packages == ["multifilepkg-stubs"]
assert pkg_data.package_data == (
{
"multifilepkg-stubs": [
"__init__.pyi",
"a.pyi",
"b.pyi",
os.path.join("c", "__init__.pyi"),
os.path.join("c", "d.pyi"),
os.path.join("c", "e.pyi"),
"METADATA.toml",
"py.typed",
]
}
)
assert pkg_data.package_data == ({"multifilepkg-stubs": ["METADATA.toml"]})

pkg_data = collect_package_data(stubs / "nspkg")
assert pkg_data.top_level_packages == ["nspkg-stubs"]
assert pkg_data.package_data == (
{
"nspkg-stubs": [
os.path.join("innerpkg", "__init__.pyi"),
"METADATA.toml",
os.path.join("innerpkg", "py.typed"),
]
}
)
assert pkg_data.package_data == ({"nspkg-stubs": ["METADATA.toml"]})


def test_collect_package_data_bogusfile() -> None:
Expand All @@ -162,27 +139,14 @@ def test_collect_package_data_bogusfile() -> None:
with open(os.path.join(stubs, "singlefilepkg", ".METADATA.toml.swp"), "w"):
pass
pkg_data = collect_package_data(stubs / "singlefilepkg")
assert set(pkg_data.package_data["singlefilepkg-stubs"]) == {
"__init__.pyi",
"py.typed",
"METADATA.toml",
}
assert set(pkg_data.package_data["singlefilepkg-stubs"]) == {"METADATA.toml"}

with open(
os.path.join(stubs, "multifilepkg", "multifilepkg", ".METADATA.toml.swp"), "w"
):
pass
pkg_data = collect_package_data(stubs / "multifilepkg")
assert set(pkg_data.package_data["multifilepkg-stubs"]) == {
"__init__.pyi",
"a.pyi",
"b.pyi",
"c/__init__.pyi",
"c/d.pyi",
"c/e.pyi",
"py.typed",
"METADATA.toml",
}
assert set(pkg_data.package_data["multifilepkg-stubs"]) == {"METADATA.toml"}


def test_uploaded_packages() -> None:
Expand Down