-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
type setuptools.extension.Extension #5022
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
1
commit into
pypa:main
Choose a base branch
from
Avasam:type-setuptools.extension.Extension-from-Typeshed
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 all commits
Commits
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
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.
It is a bit disappointing that we have to repeat everything from
distutils.Extension
.Ideally it would be nice to rely on single source of truth, and that would also improve maintenance (e.g. if something changes in
distutils
we need to change it insetuptools
).So I was wondering...
If (and that is a big if) we managed to coordinate this step with
distutils
, could we use PEP 692 to simplify this?For example, let's suppose we manage to define
class _ExtensionArgs(TypedDict)
indistutils.extension
. Then it would be very lightweight to either use it insetuptools
or inherit from it.Does it make sense?
/cc @jaraco
Uh oh!
There was an error while loading. Please reload this page.
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.
Since this doesn't need to expose extra kwargs (unlike #5021) this can be done today.
You'd also need a tuple type to unpack for args. BUT, I don't think you can mark optional arguments there.
You could consider positional args here "unsafe"/"barely supported"/"deprecated"/whatever, so you'd only have to type the union of all possible arguments, (easy to copy/past and let Ruff simplify)
AFAIK, there's no way to extend kwargs from another function in Python's type system (it's possible for positional args since you can use
Concatenate
withParamSpec.args
)It would look like this:
Compared to the current solution:
name
andsources
), or you have to loose the fact that in positional-only, these two are required. (from a typing pov)Oh and type-checkers already ensure that the setuptools override stays compatible with the base distutils type (well, up to Python 3.11 and the stdlib version only until #4689 lands)
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.
Thanks @Avasam for looking into it.
Yeah, that is always a struggle... I went back to read PEP 612 and I noticed that they explicitly excluded that from the standard :(
I understand it is difficult to implement but it would be very useful. I did not know PEP 728, hope it gets passed.
If both
_ExtensionOptionalKWArgs
and_ExtensionOptionalArgs
are defined indistutils
and used for the definition ofdistutils.extension.Extension
, would that still be required?I think that is the major crux with this alternative approach. If we define
distutils.extension.Extension.__init__(self, *args: _ExtensionOptionalArgs, **kwargs: Unpack[_ExtensionOptionalKWArgs])
, this would require some effort to keep backwards compatibility1 and we may loose type safety for positional arguments.Ideally if we are going for that both signatures of setuptools and distutils should use
_ExtensionOptionalKWArgs
so that we don't have duplication of the definitions. So the extra churn indistutils
is likely to be: definining_ExtensionOptionalKWArgs
&_ExtensionOptionalArgs
, and refactoring the code to use them, right?There is a problem with this approach though.
I don't know if the type checkers are smart enough to understand something like:
and correctly infer the type of
self.runtime_library_dirs
. That could limit the viability of this option because it would require duplication again.Footnotes
e.g. zipping
args
with_ExtensionOptionalKWArgs.__annotations__.keys
into a dict and then merging it withkwargs
before processingkwargs
. ↩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.
For the sake of brainstorm I also messed around with:
https://gist.github.com/mypy-play/34021eebf754aa542f69631eed2f0dbd
Uh oh!
There was an error while loading. Please reload this page.
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.
I'm pleasantly surprised to see mypy support it: https://mypy-play.net/?mypy=latest&python=3.12&gist=b2112018a555537c2c67e212f7b98018
So does pyright (less surprised): Playground link
Even Pyrefly: Sandbox link
I can't imagine upcoming Astral's ty wouldn't (it's just not complete enough yet)
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.
Thanks @Avasam, I have opened pypa/distutils#373 on what would be my preference for this specific problem.