-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Allow any sliceable sequence as getopt args #13116
base: main
Are you sure you want to change the base?
Conversation
This comment has been minimized.
This comment has been minimized.
stdlib/getopt.pyi
Outdated
|
||
__all__ = ["GetoptError", "error", "getopt", "gnu_getopt"] | ||
|
||
def getopt(args: list[str], shortopts: str, longopts: Iterable[str] | str = []) -> tuple[list[tuple[str, str]], list[str]]: ... | ||
def getopt( | ||
args: _StrSequenceT, shortopts: str, longopts: Iterable[str] | str = [] |
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 don't think the TypeVar makes sense. It doesn't return the same type as the input, it returns the result of slicing the input. The Sequence class doesn't require that slicing a Sequence
of a particular type gives you the same type back.
Concretely, this would produce wrong results if the input type is say tuple[str, str, str]
; the returned value may be an empty tuple, which is no longer a tuple[str, str, str]
.
I would recommend using Sequence[str]
as both the input and output type, without trying to make it generic. I haven't looked at the other files touched in this PR but I assume the same thing would apply there.
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.
Good point about slicing, let me try something. I should be able to make it generic on slice (so at least a tuple[str, str, str]
will become tuple[str, ...]
)
Also, this may be worth a test ?
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
Whilst trying to type setuptools/distutils'
Distribution.script_args
and thescript_args
arg ofsetup
for #12595, I noticed that this argument could be made more flexible as to allow any sequence.