Skip to content
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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Avasam
Copy link
Collaborator

@Avasam Avasam commented Nov 25, 2024

Whilst trying to type setuptools/distutils' Distribution.script_args and the script_args arg of setup for #12595, I noticed that this argument could be made more flexible as to allow any sequence.

This comment has been minimized.


__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 = []
Copy link
Member

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.

Copy link
Collaborator Author

@Avasam Avasam Dec 27, 2024

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 ?

Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@Avasam Avasam changed the title Allow any sequence as getopt args Allow any sliceable sequence as getopt args Dec 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants