Skip to content

🔨 only delete __init__.pyi when "unstubbing" #570

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 1 commit into from
May 20, 2025
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
14 changes: 5 additions & 9 deletions tool/stubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ def __check_simd() -> None:

def __commit_pyi_genocide_for_mypy() -> None:
"""
Remove the ``py.typed`` and all ``*.pyi`` files from the installed numpy package.
Remove all ``__init__.pyi`` files from the installed numpy package.

This works around https://github.com/python/mypy/issues/18997 on `mypy<1.16.0`.

Raises
------
Expand All @@ -82,15 +84,9 @@ def __commit_pyi_genocide_for_mypy() -> None:
if not package.is_dir():
raise NotADirectoryError(f"{package} does not exist")

py_typed = package / "py.typed"
if py_typed.is_file():
py_typed.unlink()
if VERBOSE:
print(f"deleted {py_typed} (1)")

graveyard_size = sum(not pyi.unlink() for pyi in package.rglob("*.pyi")) # type: ignore[func-returns-value]
graveyard_size = sum(not pyi.unlink() for pyi in package.rglob("__init__.pyi")) # type: ignore[func-returns-value]
if VERBOSE and graveyard_size:
print(f"deleted {package}/**/*.pyi ({graveyard_size})\n")
print(f"deleted {graveyard_size} __init__.pyi from {package}\n")


def _allowlists() -> list[str]:
Expand Down
9 changes: 5 additions & 4 deletions tool/unstub.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""
Delete all `.pyi` files in the `numpy` site-packages directory.
Delete all `__init__.pyi` files in the `numpy` site-packages directory.

This is a workaround for https://github.com/python/mypy/issues/18997 on `mypy<1.16.0`

Run with `uv run tool/unstub.py`.
"""
Expand All @@ -10,6 +12,5 @@
package = Path(sysconfig.get_paths()["purelib"]) / "numpy"
assert package.is_dir(), package

(package / "py.typed").unlink(missing_ok=True)
bodycount = sum(not pyi.unlink() for pyi in package.rglob("*.pyi")) # type: ignore[func-returns-value]
print(f"{bodycount} files deleted from {package.relative_to(Path.cwd())}/**/*.pyi")
bodycount = sum(not pyi.unlink() for pyi in package.rglob("__init__.pyi")) # type: ignore[func-returns-value]
print(f"{bodycount} files deleted from {package.relative_to(Path.cwd())}")