-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add private members to argparse.pyi. #1937
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
Conversation
Is there some issue with mypy travis right now? We're seeing:
|
Those lines are like We should merge this (after it's reviewed), then immediately make a PR to mypy syncing typeshed and removing the type: ignores. |
ping |
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.
Most of these changes look correct, although I left a couple of minor comments on specific points.
Adding this many private methods makes me uneasy. It will be hard to get all the version checks right and to keep things up to date. However, in #1902 we recently agreed to let this sort of type information in, provided that we use # undocumented
comments where appropriate. Could you add those?
stdlib/2and3/argparse.pyi
Outdated
) | ||
import sys | ||
|
||
_T = TypeVar('_T') | ||
_ActionVar = TypeVar('_ActionVar', bound='Action') |
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.
Usually we suffix typevars with T
(e.g., _ActionT
).
stdlib/2and3/argparse.pyi
Outdated
def _registry_get(self, registry_name: _Text, value: Any, default: Any = ...) -> Any: ... | ||
def set_defaults(self, **kwargs: Any) -> None: ... | ||
def get_default(self, dest: _Text) -> Any: ... | ||
def add_argument(self, *args: Any, **kwargs: Any) -> Action: ... |
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.
Why did you remove the more specific arguments to this function in the previous stub?
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.
Those were incorrect. I'm actually not entirely sure where they came from, given that in argparse.py
, add_argument
is defined using *args and **kwargs.
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.
The Python-visible signature is *args, **kwargs, but obviously in practice this method doesn't take any argument. Since it's probably the most frequently called part of argparse's interface, I think it's important that we give more specific types where it's at all possible. The documentation (https://docs.python.org/3.7/library/argparse.html#argparse.ArgumentParser.add_argument) also gives more specific arguments.
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.
Oh, I see! I think I was confused by the docstring for add_argument, which looks pretty different from the documentation. Done.
stdlib/2and3/argparse.pyi
Outdated
def __init__(self, **kwargs: Any) -> None: ... | ||
def __getattr__(self, name: _Text) -> Any: ... | ||
def __setattr__(self, name: _Text, value: Any) -> None: ... | ||
def __contains__(self, key: str) -> bool: ... | ||
|
||
class FileType: | ||
_mode: _Text | ||
_bufsize: int | ||
_encoding: Optional[_Text] |
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.
Do these exist in Python 2? The construction signatures below suggests they don't.
Addressed all the comments. |
* Add _AttributeHolder and _ActionsContainer classes to argparse. * Add Action subclasses to argparse. * Add _UNRECOGNIZED_ARGS_ATTR, _ensure_value, _get_action_name to argparse. * Fill in remaining _ActionsContainer attributes. * Fill in missing argparse.ArgumentParser attributes. * Fill in missing argparse.HelpFormatter attributes. * Fill in remaining missing attributes on argparse classes. * Rename TypeVar _ActionVar to _ActionT * Add a version check for FileType attributes * Add '# undocumented' where appropriate * Add more # undocumented comments * Make arguments to _ActionsContainer.add_argument more precise.
These attributes are sometimes used despite being private, especially the Action subclasses.
Based on https://github.com/python/cpython/blob/master/Lib/argparse.py.