-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add overloads for argparse.ArgumentParser.parse_x_args()
#8538
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
Comments
That file also seems to not exist anymore in that location, but
So it looks like it's a matter of updating these with the proper overloads |
Corresponding Python source:
Due to the argument ordering and the way the methods return (always a list[str] but the namespace can be generic or not, I'm not 100% sure how to write the overloads properly, so I'll have to defer to a typeshed team member to provide guidance or assist in fixing this, if possible. |
I think the return type is fine as |
The issue I'm not sure how to properly handle in the overloads is related to having an argument without a default after an argument with a default value as we only need to overload the namespace, based on my review of the source code's behavior (unless I misread it). You're correct about the return type though, I'm just not sure how to represent it within the mypy's rules for argument orders and default values unless it's correct to just leave those out of the overloads. |
It would be tempting to write this: @overload
def parse_args(self, args: Iterable[str] | None = ..., namespace: None = ...) -> Namespace: ...
@overload
def parse_args(self, args: Iterable[str] | None = ..., namespace: _T) -> _T: ... But as you noticed, the second overload invalid syntax. We can split it into two overloads based on how the @overload
def parse_args(self, args: Iterable[str] | None, namespace: _T) -> _T: ... Or as a keyword argument, here @overload
def parse_args(self, args: Iterable[str] | None = ..., *, namespace: _T) -> _T: ... The Putting it all together, we get three overloads: @overload
def parse_args(self, args: Iterable[str] | None = ..., namespace: None = ...) -> Namespace: ...
@overload
def parse_args(self, args: Iterable[str] | None, namespace: _T) -> _T: ...
@overload
def parse_args(self, args: Iterable[str] | None = ..., *, namespace: _T) -> _T: ... This question comes up quite regularly. Presumably we could document it somewhere instead of explaining it in a comment every time :) |
Thank you, @Akuli for the great explanation and apologies for the delay on getting back to this. I've created a merge request with my understanding. Apologies if I got any aspect of it wrong. The one difference I left between the example you provided and the current stub was I left the use of In terms of documentation, where would be the correct place to submit a merge request to document this? Happy to take a stab at it based on what you've shared. Thanks again |
|
An extension of #2566, seems like the typings for
parse_known_args
,parse_intermixed_args
,parse_known_intermixed_args
(and the undocumented_parse_known_args
) ofargparse.ArgumentParser
were overlooked, as they all share the same pattern asparse_args
for typing the namespace class.The text was updated successfully, but these errors were encountered: