Skip to content

Build backend: Support stubs packages #13563

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
May 22, 2025
Merged

Conversation

konstin
Copy link
Member

@konstin konstin commented May 20, 2025

Stubs packages are different in that their name ends with -stubs, their module is <module name>-stubs (with a dash, not the generally legal underscore) and their modules contain a __init__.pyi instead of an __init__.py (https://typing.python.org/en/latest/spec/distributing.html#stub-only-packages).

We add support in the uv build backend by detecting the -stubs suffix.

Fixes #13546

@konstin konstin requested review from BurntSushi and AlexWaygood May 20, 2025 21:35
@konstin konstin added enhancement New feature or improvement to existing functionality build-backend labels May 20, 2025
Stubs packages are different in that their name ends with `-stubs`, their module is `<module name>-stubs` (with a dash, not the generally legal underscore) and their modules contain a `__init__.pyi` instead of an `__init__.py` (https://typing.python.org/en/latest/spec/distributing.html#stub-only-packages).

We add support in the uv build backend by detecting the `-stubs` suffix.
@konstin konstin force-pushed the konsti/stubs-in-the-build-backend branch from e3f6368 to 99d21fa Compare May 20, 2025 21:36
Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

This LGTM as far as I can tell!

If you want to see how ty handles this in its module resolver, you can grep for uses of this struct in the crates/ty_python_semantic/src/module_resolver directory in the Ruff repo

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Identifier::from_str(package_name.as_dist_info_name().as_ref())?.to_string()
// Infer stubs packages from package name alone. There are potential false positives if
// someone had a regular package with `-stubs`.
if let Some(stem) = package_name.to_string().strip_suffix("-stubs") {
Copy link
Member

Choose a reason for hiding this comment

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

If a false positive happens, is there a way for a user to override that and say, "no, this is not a stubs package"?

Conversely, is there a way for a package that doesn't end with -stubs to be treated as a stubs package?

From this PR, it looks like the answer to both above is "no." Are we sure that's okay?

Copy link
Member Author

Choose a reason for hiding this comment

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

In one direction, https://typing.python.org/en/latest/spec/distributing.html#stub-only-packages is a clear MUST about the -stubs suffix. In the other direction, I think I'd try this out, and if there are really packages with let's say foo_stubs we can add an override (tool.uv.build-backend.stubs = False). If we can have one option less by having this inference I'd prefer that.

Another option would be to require stubs packages to always declare a module name, so we can tell -stubs (stubs package) from _stubs (regular module).

Copy link
Member

Choose a reason for hiding this comment

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

In one direction, https://typing.python.org/en/latest/spec/distributing.html#stub-only-packages is a clear MUST about the -stubs suffix. In the other direction, I think I'd try this out, and if there are really packages with let's say foo_stubs we can add an override (tool.uv.build-backend.stubs = False). If we can have one option less by having this inference I'd prefer that.

I think that's good enough for me.

Another option would be to require stubs packages to always declare a module name, so we can tell -stubs (stubs package) from _stubs (regular module).

I do like this idea! But I agree that inference probably makes more sense, and if necessary, we can add a way to override it if it comes up.

Copy link
Member Author

Choose a reason for hiding this comment

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

I realized that we actually do have overrides: Setting the module name to _stubs vs. -stubs will trigger regular package vs. stubs package behavior.

Copy link
Member

Choose a reason for hiding this comment

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

Nice. Yeah I think that SGTM!

@konstin
Copy link
Member Author

konstin commented May 21, 2025

Added some docs, mainly to make searching for "stubs" show that we support them.

@konstin konstin merged commit 46bc7d3 into main May 22, 2025
86 checks passed
@konstin konstin deleted the konsti/stubs-in-the-build-backend branch May 22, 2025 17:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
build-backend enhancement New feature or improvement to existing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

uv_build cannot find stubs-only package
3 participants