-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Generate distutils-stubs
on install
#4861
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
Open
Avasam
wants to merge
9
commits into
pypa:main
Choose a base branch
from
Avasam:generate-distuttils-stubs-on-install
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d9e413e
Generate `distutils-stubs` on install
Avasam 1ac7ac8
Use custom build backend for PEP 660 editable installs
Avasam 4e82d63
Correctly work around mypy issue
Avasam 05e7d53
Merge branch 'main' of https://github.com/pypa/setuptools into genera…
Avasam 4b7408e
Merge branch 'main' of https://github.com/pypa/setuptools into genera…
Avasam 74e7ea8
Fix all mypy type issues
Avasam 87edb0f
Merge branch 'main' of https://github.com/pypa/setuptools into genera…
Avasam 48f0a67
Fix odd mypy typing issue in setuptools.errors on Python 3.12+
Avasam 570a4ae
Fix 2 pyright issues in tests
Avasam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
"""Generate distutils stub files inside the source directory before packaging. | ||
We have to do this as a custom build backend for PEP 660 editable installs. | ||
Doing it this way also allows us to point local type-checkers to types/distutils, | ||
overriding the stdlib types even on Python < 3.12.""" | ||
|
||
from __future__ import annotations | ||
|
||
import os | ||
import shutil | ||
from pathlib import Path | ||
|
||
from setuptools._path import StrPath | ||
from setuptools.build_meta import * # noqa: F403 # expose everything | ||
from setuptools.build_meta import ( | ||
_ConfigSettings, | ||
build_editable as _build_editable, | ||
build_sdist as _build_sdist, | ||
build_wheel as _build_wheel, | ||
) | ||
|
||
_vendored_distutils_path = Path(__file__).parent / "setuptools" / "_distutils" | ||
_distutils_stubs_path = Path(__file__).parent / "distutils-stubs" | ||
|
||
|
||
def _regenerate_distutils_stubs() -> None: | ||
shutil.rmtree(_distutils_stubs_path, ignore_errors=True) | ||
_distutils_stubs_path.mkdir(parents=True) | ||
(_distutils_stubs_path / ".gitignore").write_text("*") | ||
(_distutils_stubs_path / "ruff.toml").write_text('[lint]\nignore = ["F403"]') | ||
(_distutils_stubs_path / "py.typed").write_text("\n") | ||
for path in _vendored_distutils_path.rglob("*.py"): | ||
relative_path = path.relative_to(_vendored_distutils_path) | ||
if "tests" in relative_path.parts: | ||
continue | ||
stub_path = _distutils_stubs_path / relative_path.with_suffix(".pyi") | ||
stub_path.parent.mkdir(parents=True, exist_ok=True) | ||
module = "setuptools._distutils." + str(relative_path.with_suffix("")).replace( | ||
os.sep, "." | ||
).removesuffix(".__init__") | ||
if str(relative_path) == "__init__.py": | ||
# Work around python/mypy#18775 | ||
stub_path.write_text("""\ | ||
from typing import Final | ||
|
||
__version__: Final[str] | ||
""") | ||
Comment on lines
+41
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Turns out even typeshed had to do this, I just missed it: https://github.com/python/typeshed/blob/da50b5c4112bc44dce08685f9e30708b8cb88489/stubs/setuptools/distutils/__init__.pyi |
||
else: | ||
stub_path.write_text(f"from {module} import *\n") | ||
|
||
|
||
def build_wheel( # type: ignore[no-redef] | ||
wheel_directory: StrPath, | ||
config_settings: _ConfigSettings = None, | ||
metadata_directory: StrPath | None = None, | ||
) -> str: | ||
_regenerate_distutils_stubs() | ||
return _build_wheel(wheel_directory, config_settings, metadata_directory) | ||
|
||
|
||
def build_sdist( # type: ignore[no-redef] | ||
sdist_directory: StrPath, | ||
config_settings: _ConfigSettings = None, | ||
) -> str: | ||
_regenerate_distutils_stubs() | ||
return _build_sdist(sdist_directory, config_settings) | ||
|
||
|
||
def build_editable( # type: ignore[no-redef] | ||
wheel_directory: StrPath, | ||
config_settings: _ConfigSettings = None, | ||
metadata_directory: StrPath | None = None, | ||
) -> str: | ||
_regenerate_distutils_stubs() | ||
return _build_editable(wheel_directory, config_settings, metadata_directory) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
``setuptools`` now provide its own ``distutils-stubs`` instead of relying on typeshed -- by :user:`Avasam` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like I got this working without leaving an unchecked
types/
folder in the source.