From 472e37652da6e0c81a8ce9317df295d5f518a9cf Mon Sep 17 00:00:00 2001 From: Rebecca Chen Date: Wed, 28 Feb 2018 17:33:32 -0800 Subject: [PATCH 01/13] Add _AttributeHolder and _ActionsContainer classes to argparse. --- stdlib/2and3/argparse.pyi | 75 +++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 39 deletions(-) diff --git a/stdlib/2and3/argparse.pyi b/stdlib/2and3/argparse.pyi index bfa930c0ceea..c79239baa08f 100644 --- a/stdlib/2and3/argparse.pyi +++ b/stdlib/2and3/argparse.pyi @@ -1,12 +1,13 @@ # Stubs for argparse (Python 2.7 and 3.4) from typing import ( - Any, Callable, Iterable, List, IO, Optional, Sequence, Tuple, Type, Union, - TypeVar, overload + Any, Callable, Dict, Iterable, List, IO, NoReturn, Optional, Sequence, + Tuple, Type, Union, TypeVar, overload ) import sys _T = TypeVar('_T') +_ActionVar = TypeVar('_ActionVar', bound='Action') if sys.version_info >= (3,): _Text = str @@ -22,7 +23,32 @@ ZERO_OR_MORE = ... # type: str class ArgumentError(Exception): ... -class ArgumentParser: +class _AttributeHolder: + def _get_kwargs(self) -> List[Tuple[str, Any]]: ... + def _get_args(self) -> List[Any]: ... + +class _ActionsContainer: + def __init__(self, description: Optional[_Text], prefix_chars: _Text, + argument_default: Optional[_Text], conflict_handler: _Text) -> None: ... + def register(self, registry_name: _Text, value: Any, object: Any) -> None: ... + 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: ... + def add_argument_group(self, *args: Any, **kwargs: Any) -> _ArgumentGroup: ... + def add_mutually_exclusive_group(self, **kwargs: Any) -> _MutuallyExclusiveGroup: ... + def _add_action(self, action: _ActionVar) -> _ActionVar: ... + def _remove_action(self, action: Action) -> None: ... + def _add_container_actions(self, container: _ActionsContainer) -> None: ... + def _get_positional_kwargs(self, dest: _Text, **kwargs: Any) -> Dict[str, Any]: ... + def _get_optional_kwargs(self, *args: Any, **kwargs: Any) -> Dict[str, Any]: ... + def _pop_action_class(self, kwargs: Any, default: Optional[Type[Action]] = ...) -> Type[Action]: ... + def _get_handler(self) -> Callable[[Action, Iterable[Tuple[_Text, Action]]], Any]: ... + def _check_conflict(self, action: Action) -> None: ... + def _handle_conflict_error(self, action: Action, conflicting_actions: Iterable[Tuple[_Text, Action]]) -> NoReturn: ... + def _handle_conflict_resolve(self, action: Action, conflicting_actions: Iterable[Tuple[_Text, Action]]) -> None: ... + +class ArgumentParser(_AttributeHolder, _ActionsContainer): prog = ... # type: _Text usage = ... # type: Optional[_Text] description = ... # type: Optional[_Text] @@ -64,19 +90,6 @@ class ArgumentParser: argument_default: Optional[_Text] = ..., conflict_handler: _Text = ..., add_help: bool = ...) -> None: ... - def add_argument(self, - *name_or_flags: Union[_Text, Sequence[_Text]], - action: Union[_Text, Type[Action]] = ..., - nargs: Union[int, _Text] = ..., - const: Any = ..., - default: Any = ..., - type: Union[Callable[[str], _T], FileType] = ..., - choices: Iterable[_T] = ..., - required: bool = ..., - help: _Text = ..., - metavar: Union[_Text, Tuple[_Text, ...]] = ..., - dest: _Text = ..., - version: _Text = ...) -> None: ... # weirdly documented def parse_args(self, args: Optional[Sequence[_Text]] = ..., namespace: Optional[Namespace] = ...) -> Namespace: ... def add_subparsers(self, title: _Text = ..., @@ -88,11 +101,6 @@ class ArgumentParser: dest: Optional[_Text] = ..., help: Optional[_Text] = ..., metavar: Optional[_Text] = ...) -> _SubParsersAction: ... - def add_argument_group(self, title: Optional[_Text] = ..., - description: Optional[_Text] = ...) -> _ArgumentGroup: ... - def add_mutually_exclusive_group(self, required: bool = ...) -> _MutuallyExclusiveGroup: ... - def set_defaults(self, **kwargs: Any) -> None: ... - def get_default(self, dest: _Text) -> Any: ... def print_usage(self, file: Optional[IO[str]] = ...) -> None: ... def print_help(self, file: Optional[IO[str]] = ...) -> None: ... def format_usage(self) -> str: ... @@ -114,7 +122,7 @@ class ArgumentDefaultsHelpFormatter(HelpFormatter): ... if sys.version_info >= (3,): class MetavarTypeHelpFormatter(HelpFormatter): ... -class Action: +class Action(_AttributeHolder): option_strings: Sequence[_Text] dest: _Text nargs: Optional[Union[int, _Text]] @@ -127,7 +135,7 @@ class Action: metavar: Optional[Union[_Text, Tuple[_Text, ...]]] def __init__(self, option_strings: Sequence[_Text], - dest: _Text = ..., + dest: _Text, nargs: Optional[Union[int, _Text]] = ..., const: Any = ..., default: Any = ..., @@ -140,7 +148,7 @@ class Action: values: Union[_Text, Sequence[Any], None], option_string: Optional[_Text] = ...) -> None: ... -class Namespace: +class Namespace(_AttributeHolder): def __init__(self, **kwargs: Any) -> None: ... def __getattr__(self, name: _Text) -> Any: ... def __setattr__(self, name: _Text, value: Any) -> None: ... @@ -159,21 +167,10 @@ class FileType: mode: _Text = ..., bufsize: Optional[int] = ...) -> None: ... def __call__(self, string: _Text) -> IO[Any]: ... -class _ArgumentGroup: - def add_argument(self, - *name_or_flags: Union[_Text, Sequence[_Text]], - action: Union[_Text, Type[Action]] = ..., - nargs: Union[int, _Text] = ..., - const: Any = ..., - default: Any = ..., - type: Union[Callable[[str], _T], FileType] = ..., - choices: Iterable[_T] = ..., - required: bool = ..., - help: _Text = ..., - metavar: Union[_Text, Tuple[_Text, ...]] = ..., - dest: _Text = ..., - version: _Text = ...) -> None: ... - def add_mutually_exclusive_group(self, required: bool = ...) -> _MutuallyExclusiveGroup: ... +class _ArgumentGroup(_ActionsContainer): + def __init__(self, container: _ActionsContainer, + title: Optional[_Text] = ..., + description: Optional[_Text] = ..., **kwargs: Any) -> None: ... class _MutuallyExclusiveGroup(_ArgumentGroup): ... From b134a5101184ae8af43545e6394bc90fbd55c527 Mon Sep 17 00:00:00 2001 From: Rebecca Chen Date: Wed, 28 Feb 2018 17:56:28 -0800 Subject: [PATCH 02/13] Add Action subclasses to argparse. --- stdlib/2and3/argparse.pyi | 65 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/stdlib/2and3/argparse.pyi b/stdlib/2and3/argparse.pyi index c79239baa08f..8ad8b40f7715 100644 --- a/stdlib/2and3/argparse.pyi +++ b/stdlib/2and3/argparse.pyi @@ -174,7 +174,70 @@ class _ArgumentGroup(_ActionsContainer): class _MutuallyExclusiveGroup(_ArgumentGroup): ... -class _SubParsersAction: +class _StoreAction(Action): ... + +class _StoreConstAction(Action): + def __init__(self, + option_strings: Sequence[_Text], + dest: _Text, + const: Any, + default: Any = ..., + required: bool = ..., + help: Optional[_Text] = ..., + metavar: Optional[Union[_Text, Tuple[_Text, ...]]] = ...) -> None: ... + +class _StoreTrueAction(_StoreConstAction): + def __init__(self, + option_strings: Sequence[_Text], + dest: _Text, + default: bool = ..., + required: bool = ..., + help: Optional[_Text] = ...) -> None: ... + +class _StoreFalseAction(_StoreConstAction): + def __init__(self, + option_strings: Sequence[_Text], + dest: _Text, + default: bool = ..., + required: bool = ..., + help: Optional[_Text] = ...) -> None: ... + +class _AppendAction(Action): ... + +class _AppendConstAction(Action): + def __init__(self, + option_strings: Sequence[_Text], + dest: _Text, + const: Any, + default: Any = ..., + required: bool = ..., + help: Optional[_Text] = ..., + metavar: Optional[Union[_Text, Tuple[_Text, ...]]] = ...) -> None: ... + +class _CountAction(Action): + def __init__(self, + option_strings: Sequence[_Text], + dest: _Text, + default: Any = ..., + required: bool = ..., + help: Optional[_Text] = ...) -> None: ... + +class _HelpAction(Action): + def __init__(self, + option_strings: Sequence[_Text], + dest: _Text = ..., + default: _Text = ..., + help: Optional[_Text] = ...) -> None: ... + +class _VersionAction(Action): + def __init__(self, + option_strings: Sequence[_Text], + version: Optional[_Text] = ..., + dest: _Text = ..., + default: _Text = ..., + help: _Text = ...) -> None: ... + +class _SubParsersAction(Action): # TODO: Type keyword args properly. def add_parser(self, name: _Text, **kwargs: Any) -> ArgumentParser: ... From 70b32f0407b1b4471e951ac1629a0eaf56b64422 Mon Sep 17 00:00:00 2001 From: Rebecca Chen Date: Wed, 28 Feb 2018 18:17:07 -0800 Subject: [PATCH 03/13] Add _UNRECOGNIZED_ARGS_ATTR, _ensure_value, _get_action_name to argparse. --- stdlib/2and3/argparse.pyi | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/stdlib/2and3/argparse.pyi b/stdlib/2and3/argparse.pyi index 8ad8b40f7715..7024daf1cd45 100644 --- a/stdlib/2and3/argparse.pyi +++ b/stdlib/2and3/argparse.pyi @@ -14,12 +14,13 @@ if sys.version_info >= (3,): else: _Text = Union[str, unicode] -ONE_OR_MORE = ... # type: str -OPTIONAL = ... # type: str -PARSER = ... # type: str -REMAINDER = ... # type: str -SUPPRESS = ... # type: str -ZERO_OR_MORE = ... # type: str +ONE_OR_MORE: str +OPTIONAL: str +PARSER: str +REMAINDER: str +SUPPRESS: str +ZERO_OR_MORE: str +_UNRECOGNIZED_ARGS_ATTR: str class ArgumentError(Exception): ... @@ -243,3 +244,8 @@ class _SubParsersAction(Action): # not documented class ArgumentTypeError(Exception): ... + +if sys.version_info <= (3, 6): + def _ensure_value(namespace: Namespace, name: _Text, value: Any) -> Any: ... + +def _get_action_name(argument: Optional[Action]) -> Optional[str]: ... From f453f126e2f0e8c245bdb4244bad90e70b627fc6 Mon Sep 17 00:00:00 2001 From: Rebecca Chen Date: Wed, 28 Feb 2018 18:39:57 -0800 Subject: [PATCH 04/13] Fill in remaining _ActionsContainer attributes. --- stdlib/2and3/argparse.pyi | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/stdlib/2and3/argparse.pyi b/stdlib/2and3/argparse.pyi index 7024daf1cd45..03af7a2f73b0 100644 --- a/stdlib/2and3/argparse.pyi +++ b/stdlib/2and3/argparse.pyi @@ -1,8 +1,8 @@ # Stubs for argparse (Python 2.7 and 3.4) from typing import ( - Any, Callable, Dict, Iterable, List, IO, NoReturn, Optional, Sequence, - Tuple, Type, Union, TypeVar, overload + Any, Callable, Dict, Iterable, List, IO, NoReturn, Optional, Pattern, + Sequence, Tuple, Type, Union, TypeVar, overload ) import sys @@ -29,6 +29,20 @@ class _AttributeHolder: def _get_args(self) -> List[Any]: ... class _ActionsContainer: + description: Optional[_Text] + prefix_chars: _Text + argument_default: Optional[_Text] + conflict_handler: _Text + + _registries: Dict[_Text, Dict[Any, Any]] + _actions: List[Action] + _option_string_actions: Dict[_Text, Action] + _action_groups: List[_ArgumentGroup] + _mutually_exclusive_groups: List[_MutuallyExclusiveGroup] + _defaults: Dict[str, Any] + _negative_number_matcher: Pattern[str] + _has_negative_number_optionals: List[bool] + def __init__(self, description: Optional[_Text], prefix_chars: _Text, argument_default: Optional[_Text], conflict_handler: _Text) -> None: ... def register(self, registry_name: _Text, value: Any, object: Any) -> None: ... @@ -50,19 +64,15 @@ class _ActionsContainer: def _handle_conflict_resolve(self, action: Action, conflicting_actions: Iterable[Tuple[_Text, Action]]) -> None: ... class ArgumentParser(_AttributeHolder, _ActionsContainer): - prog = ... # type: _Text - usage = ... # type: Optional[_Text] - description = ... # type: Optional[_Text] - epilog = ... # type: Optional[_Text] - formatter_class = ... # type: Type[HelpFormatter] - prefix_chars = ... # type: _Text - fromfile_prefix_chars = ... # type: Optional[_Text] - argument_default = ... # type: Optional[_Text] - conflict_handler = ... # type: _Text - add_help = ... # type: bool + prog: _Text + usage: Optional[_Text] + epilog: Optional[_Text] + formatter_class: Type[HelpFormatter] + fromfile_prefix_chars: Optional[_Text] + add_help: bool if sys.version_info >= (3, 5): - allow_abbrev = ... # type: bool + allow_abbrev: bool if sys.version_info >= (3, 5): def __init__(self, From 6c8939872612c0bcc8385cd59b98dd6a1839b8e0 Mon Sep 17 00:00:00 2001 From: Rebecca Chen Date: Thu, 1 Mar 2018 11:43:16 -0800 Subject: [PATCH 05/13] Fill in missing argparse.ArgumentParser attributes. --- stdlib/2and3/argparse.pyi | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/stdlib/2and3/argparse.pyi b/stdlib/2and3/argparse.pyi index 03af7a2f73b0..64e5761ecf57 100644 --- a/stdlib/2and3/argparse.pyi +++ b/stdlib/2and3/argparse.pyi @@ -74,6 +74,10 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer): if sys.version_info >= (3, 5): allow_abbrev: bool + _positionals: _ArgumentGroup + _optionals: _ArgumentGroup + _subparsers: Optional[_ArgumentGroup] + if sys.version_info >= (3, 5): def __init__(self, prog: Optional[str] = ..., @@ -121,6 +125,26 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer): def convert_arg_line_to_args(self, arg_line: _Text) -> List[str]: ... def exit(self, status: int = ..., message: Optional[_Text] = ...) -> None: ... def error(self, message: _Text) -> None: ... + if sys.version_info >= (3, 7): + def parse_intermixed_args(self, args: Optional[Sequence[_Text]] = ..., + namespace: Optional[Namespace] = ...) -> Namespace: ... + def parse_known_intermixed_args(self, + args: Optional[Sequence[_Text]] = ..., + namespace: Optional[Namespace] = ...) -> Tuple[Namespace, List[str]]: ... + def _get_optional_actions(self) -> List[Action]: ... + def _get_positional_actions(self) -> List[Action]: ... + def _parse_known_args(self, arg_strings: List[_Text], namespace: Namespace) -> Tuple[Namespace, List[str]]: ... + def _read_args_from_files(self, arg_strings: List[_Text]) -> List[_Text]: ... + def _match_argument(self, action: Action, arg_strings_pattern: _Text) -> int: ... + def _match_arguments_partial(self, actions: Sequence[Action], arg_strings_pattern: _Text) -> List[int]: ... + def _parse_optional(self, arg_string: _Text) -> Optional[Tuple[Optional[Action], _Text, Optional[_Text]]]: ... + def _get_option_tuples(self, option_string: _Text) -> List[Tuple[Action, _Text, Optional[_Text]]]: ... + def _get_nargs_pattern(self, action: Action) -> _Text: ... + def _get_values(self, action: Action, arg_strings: List[_Text]) -> Any: ... + def _get_value(self, action: Action, arg_string: _Text) -> Any: ... + def _check_value(self, action: Action, value: Any) -> None: ... + def _get_formatter(self) -> HelpFormatter: ... + def _print_message(self, message: str, file: Optional[IO[str]] = ...) -> None: ... class HelpFormatter: # not documented From 013c6629aaeafcfc55f066ecd293bd9ce96f4640 Mon Sep 17 00:00:00 2001 From: Rebecca Chen Date: Thu, 1 Mar 2018 15:12:15 -0800 Subject: [PATCH 06/13] Fill in missing argparse.HelpFormatter attributes. --- stdlib/2and3/argparse.pyi | 42 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/stdlib/2and3/argparse.pyi b/stdlib/2and3/argparse.pyi index 64e5761ecf57..c541c61c4631 100644 --- a/stdlib/2and3/argparse.pyi +++ b/stdlib/2and3/argparse.pyi @@ -1,8 +1,8 @@ # Stubs for argparse (Python 2.7 and 3.4) from typing import ( - Any, Callable, Dict, Iterable, List, IO, NoReturn, Optional, Pattern, - Sequence, Tuple, Type, Union, TypeVar, overload + Any, Callable, Dict, Generator, Iterable, List, IO, NoReturn, Optional, + Pattern, Sequence, Tuple, Type, Union, TypeVar, overload ) import sys @@ -148,9 +148,47 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer): class HelpFormatter: # not documented + _prog: _Text + _indent_increment: int + _max_help_position: int + _width: int + _current_indent: int + _level: int + _action_max_length: int + _root_section: Any + _current_section: Any + _whitespace_matcher: Pattern[str] + _long_break_matcher: Pattern[str] + _Section: Type[Any] # Nested class def __init__(self, prog: _Text, indent_increment: int = ..., max_help_position: int = ..., width: Optional[int] = ...) -> None: ... + def _indent(self) -> None: ... + def _dedent(self) -> None: ... + def _add_item(self, func: Callable[..., _Text], args: Iterable[Any]) -> None: ... + def start_section(self, heading: Optional[_Text]) -> None: ... + def end_section(self) -> None: ... + def add_text(self, text: Optional[_Text]) -> None: ... + def add_usage(self, usage: _Text, actions: Iterable[Action], groups: Iterable[_ArgumentGroup], prefix: Optional[_Text] = ...) -> None: ... + def add_argument(self, action: Action) -> None: ... + def add_arguments(self, actions: Iterable[Action]) -> None: ... + def format_help(self) -> _Text: ... + def _join_parts(self, part_strings: Iterable[_Text]) -> _Text: ... + def _format_usage(self, usage: _Text, actions: Iterable[Action], groups: Iterable[_ArgumentGroup], prefix: Optional[_Text]) -> _Text: ... + def _format_actions_usage(self, actions: Iterable[Action], groups: Iterable[_ArgumentGroup]) -> _Text: ... + def _format_text(self, text: _Text) -> _Text: ... + def _format_action(self, action: Action) -> _Text: ... + def _format_action_invocation(self, action: Action) -> _Text: ... + def _metavar_formatter(self, action: Action, default_metavar: _Text) -> Callable[[int], Tuple[_Text, ...]]: ... + def _format_args(self, action: Action, default_metavar: _Text) -> _Text: ... + def _expand_help(self, action: Action) -> _Text: ... + def _iter_indented_subactions(self, action: Action) -> Generator[Action, None, None]: ... + def _split_lines(self, text: _Text, width: int) -> List[_Text]: ... + def _fill_text(self, text: _Text, width: int, indent: int) -> _Text: ... + def _get_help_string(self, action: Action) -> Optional[_Text]: ... + def _get_default_metavar_for_optional(self, action: Action) -> _Text: ... + def _get_default_metavar_for_positional(self, action: Action) -> _Text: ... + class RawDescriptionHelpFormatter(HelpFormatter): ... class RawTextHelpFormatter(HelpFormatter): ... class ArgumentDefaultsHelpFormatter(HelpFormatter): ... From c334e87de3a5a1c086334cd0ac73bf01c66e1ada Mon Sep 17 00:00:00 2001 From: Rebecca Chen Date: Thu, 1 Mar 2018 15:39:24 -0800 Subject: [PATCH 07/13] Fill in remaining missing attributes on argparse classes. --- stdlib/2and3/argparse.pyi | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/stdlib/2and3/argparse.pyi b/stdlib/2and3/argparse.pyi index c541c61c4631..a426ccf12350 100644 --- a/stdlib/2and3/argparse.pyi +++ b/stdlib/2and3/argparse.pyi @@ -228,6 +228,10 @@ class Namespace(_AttributeHolder): def __contains__(self, key: str) -> bool: ... class FileType: + _mode: _Text + _bufsize: int + _encoding: Optional[_Text] + _errors: Optional[_Text] if sys.version_info >= (3, 4): def __init__(self, mode: _Text = ..., bufsize: int = ..., encoding: Optional[_Text] = ..., @@ -241,11 +245,16 @@ class FileType: def __call__(self, string: _Text) -> IO[Any]: ... class _ArgumentGroup(_ActionsContainer): + title: Optional[_Text] + _group_actions: List[Action] def __init__(self, container: _ActionsContainer, title: Optional[_Text] = ..., description: Optional[_Text] = ..., **kwargs: Any) -> None: ... -class _MutuallyExclusiveGroup(_ArgumentGroup): ... +class _MutuallyExclusiveGroup(_ArgumentGroup): + required: bool + _container: _ActionsContainer + def __init__(self, container: _ActionsContainer, required: bool = ...) -> None: ... class _StoreAction(Action): ... @@ -303,6 +312,7 @@ class _HelpAction(Action): help: Optional[_Text] = ...) -> None: ... class _VersionAction(Action): + version: Optional[_Text] def __init__(self, option_strings: Sequence[_Text], version: Optional[_Text] = ..., @@ -311,8 +321,22 @@ class _VersionAction(Action): help: _Text = ...) -> None: ... class _SubParsersAction(Action): + _ChoicesPseudoAction: Type[Any] # nested class + _prog_prefix: _Text + _parser_class: Type[ArgumentParser] + _name_parser_map: Dict[_Text, ArgumentParser] + _choices_actions: List[Action] + def __init__(self, + option_strings: Sequence[_Text], + prog: _Text, + parser_class: Type[ArgumentParser], + dest: _Text = ..., + required: bool = ..., + help: Optional[_Text] = ..., + metavar: Optional[Union[_Text, Tuple[_Text, ...]]] = ...) -> None: ... # TODO: Type keyword args properly. def add_parser(self, name: _Text, **kwargs: Any) -> ArgumentParser: ... + def _get_subactions(self) -> List[Action]: ... # not documented class ArgumentTypeError(Exception): ... From 405c174b2c59cc053cdf3ad3f2c4e77c1a5cb5d9 Mon Sep 17 00:00:00 2001 From: Rebecca Chen Date: Thu, 1 Mar 2018 16:37:47 -0800 Subject: [PATCH 08/13] Make flake8 happy. --- stdlib/2and3/argparse.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/2and3/argparse.pyi b/stdlib/2and3/argparse.pyi index a426ccf12350..e5a6cf368f66 100644 --- a/stdlib/2and3/argparse.pyi +++ b/stdlib/2and3/argparse.pyi @@ -341,7 +341,7 @@ class _SubParsersAction(Action): # not documented class ArgumentTypeError(Exception): ... -if sys.version_info <= (3, 6): +if sys.version_info < (3, 7): def _ensure_value(namespace: Namespace, name: _Text, value: Any) -> Any: ... def _get_action_name(argument: Optional[Action]) -> Optional[str]: ... From ba120c30302968b97cdfb2d2e796e3904f9013a3 Mon Sep 17 00:00:00 2001 From: Rebecca Chen Date: Wed, 21 Mar 2018 13:24:06 -0700 Subject: [PATCH 09/13] Rename TypeVar _ActionVar to _ActionT --- stdlib/2and3/argparse.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/2and3/argparse.pyi b/stdlib/2and3/argparse.pyi index e5a6cf368f66..4e2802cc6ca8 100644 --- a/stdlib/2and3/argparse.pyi +++ b/stdlib/2and3/argparse.pyi @@ -7,7 +7,7 @@ from typing import ( import sys _T = TypeVar('_T') -_ActionVar = TypeVar('_ActionVar', bound='Action') +_ActionT = TypeVar('_ActionT', bound='Action') if sys.version_info >= (3,): _Text = str @@ -52,7 +52,7 @@ class _ActionsContainer: def add_argument(self, *args: Any, **kwargs: Any) -> Action: ... def add_argument_group(self, *args: Any, **kwargs: Any) -> _ArgumentGroup: ... def add_mutually_exclusive_group(self, **kwargs: Any) -> _MutuallyExclusiveGroup: ... - def _add_action(self, action: _ActionVar) -> _ActionVar: ... + def _add_action(self, action: _ActionT) -> _ActionT: ... def _remove_action(self, action: Action) -> None: ... def _add_container_actions(self, container: _ActionsContainer) -> None: ... def _get_positional_kwargs(self, dest: _Text, **kwargs: Any) -> Dict[str, Any]: ... From 82547334b55b10f05846458574525fc5d48b4c84 Mon Sep 17 00:00:00 2001 From: Rebecca Chen Date: Wed, 21 Mar 2018 13:27:49 -0700 Subject: [PATCH 10/13] Add a version check for FileType attributes --- stdlib/2and3/argparse.pyi | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/stdlib/2and3/argparse.pyi b/stdlib/2and3/argparse.pyi index 4e2802cc6ca8..adb2a4a58a74 100644 --- a/stdlib/2and3/argparse.pyi +++ b/stdlib/2and3/argparse.pyi @@ -230,8 +230,9 @@ class Namespace(_AttributeHolder): class FileType: _mode: _Text _bufsize: int - _encoding: Optional[_Text] - _errors: Optional[_Text] + if sys.version_info >= (3, 4): + _encoding: Optional[_Text] + _errors: Optional[_Text] if sys.version_info >= (3, 4): def __init__(self, mode: _Text = ..., bufsize: int = ..., encoding: Optional[_Text] = ..., From 554f9382281db0615649d796579a9b5f9e794cd7 Mon Sep 17 00:00:00 2001 From: Rebecca Chen Date: Wed, 21 Mar 2018 14:55:02 -0700 Subject: [PATCH 11/13] Add '# undocumented' where appropriate --- stdlib/2and3/argparse.pyi | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/stdlib/2and3/argparse.pyi b/stdlib/2and3/argparse.pyi index adb2a4a58a74..0f5f4831f5cf 100644 --- a/stdlib/2and3/argparse.pyi +++ b/stdlib/2and3/argparse.pyi @@ -20,14 +20,16 @@ PARSER: str REMAINDER: str SUPPRESS: str ZERO_OR_MORE: str -_UNRECOGNIZED_ARGS_ATTR: str +_UNRECOGNIZED_ARGS_ATTR: str # undocumented class ArgumentError(Exception): ... +# undocumented class _AttributeHolder: def _get_kwargs(self) -> List[Tuple[str, Any]]: ... def _get_args(self) -> List[Any]: ... +# undocumented class _ActionsContainer: description: Optional[_Text] prefix_chars: _Text @@ -74,6 +76,7 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer): if sys.version_info >= (3, 5): allow_abbrev: bool + # undocumented _positionals: _ArgumentGroup _optionals: _ArgumentGroup _subparsers: Optional[_ArgumentGroup] @@ -131,6 +134,7 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer): def parse_known_intermixed_args(self, args: Optional[Sequence[_Text]] = ..., namespace: Optional[Namespace] = ...) -> Tuple[Namespace, List[str]]: ... + # undocumented def _get_optional_actions(self) -> List[Action]: ... def _get_positional_actions(self) -> List[Action]: ... def _parse_known_args(self, arg_strings: List[_Text], namespace: Namespace) -> Tuple[Namespace, List[str]]: ... @@ -147,7 +151,7 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer): def _print_message(self, message: str, file: Optional[IO[str]] = ...) -> None: ... class HelpFormatter: - # not documented + # undocumented _prog: _Text _indent_increment: int _max_help_position: int @@ -228,6 +232,7 @@ class Namespace(_AttributeHolder): def __contains__(self, key: str) -> bool: ... class FileType: + # undocumented _mode: _Text _bufsize: int if sys.version_info >= (3, 4): @@ -339,10 +344,12 @@ class _SubParsersAction(Action): def add_parser(self, name: _Text, **kwargs: Any) -> ArgumentParser: ... def _get_subactions(self) -> List[Action]: ... -# not documented +# undocumented class ArgumentTypeError(Exception): ... if sys.version_info < (3, 7): + # undocumented def _ensure_value(namespace: Namespace, name: _Text, value: Any) -> Any: ... +# undocumented def _get_action_name(argument: Optional[Action]) -> Optional[str]: ... From 6938ee58253d68c4afca15d236dd379fe86792cd Mon Sep 17 00:00:00 2001 From: Rebecca Chen Date: Wed, 21 Mar 2018 16:14:24 -0700 Subject: [PATCH 12/13] Add more # undocumented comments --- stdlib/2and3/argparse.pyi | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/stdlib/2and3/argparse.pyi b/stdlib/2and3/argparse.pyi index 0f5f4831f5cf..dde124bf28e5 100644 --- a/stdlib/2and3/argparse.pyi +++ b/stdlib/2and3/argparse.pyi @@ -250,6 +250,7 @@ class FileType: mode: _Text = ..., bufsize: Optional[int] = ...) -> None: ... def __call__(self, string: _Text) -> IO[Any]: ... +# undocumented class _ArgumentGroup(_ActionsContainer): title: Optional[_Text] _group_actions: List[Action] @@ -257,13 +258,16 @@ class _ArgumentGroup(_ActionsContainer): title: Optional[_Text] = ..., description: Optional[_Text] = ..., **kwargs: Any) -> None: ... +# undocumented class _MutuallyExclusiveGroup(_ArgumentGroup): required: bool _container: _ActionsContainer def __init__(self, container: _ActionsContainer, required: bool = ...) -> None: ... +# undocumented class _StoreAction(Action): ... +# undocumented class _StoreConstAction(Action): def __init__(self, option_strings: Sequence[_Text], @@ -274,6 +278,7 @@ class _StoreConstAction(Action): help: Optional[_Text] = ..., metavar: Optional[Union[_Text, Tuple[_Text, ...]]] = ...) -> None: ... +# undocumented class _StoreTrueAction(_StoreConstAction): def __init__(self, option_strings: Sequence[_Text], @@ -282,6 +287,7 @@ class _StoreTrueAction(_StoreConstAction): required: bool = ..., help: Optional[_Text] = ...) -> None: ... +# undocumented class _StoreFalseAction(_StoreConstAction): def __init__(self, option_strings: Sequence[_Text], @@ -290,8 +296,10 @@ class _StoreFalseAction(_StoreConstAction): required: bool = ..., help: Optional[_Text] = ...) -> None: ... +# undocumented class _AppendAction(Action): ... +# undocumented class _AppendConstAction(Action): def __init__(self, option_strings: Sequence[_Text], @@ -302,6 +310,7 @@ class _AppendConstAction(Action): help: Optional[_Text] = ..., metavar: Optional[Union[_Text, Tuple[_Text, ...]]] = ...) -> None: ... +# undocumented class _CountAction(Action): def __init__(self, option_strings: Sequence[_Text], @@ -310,6 +319,7 @@ class _CountAction(Action): required: bool = ..., help: Optional[_Text] = ...) -> None: ... +# undocumented class _HelpAction(Action): def __init__(self, option_strings: Sequence[_Text], @@ -317,6 +327,7 @@ class _HelpAction(Action): default: _Text = ..., help: Optional[_Text] = ...) -> None: ... +# undocumented class _VersionAction(Action): version: Optional[_Text] def __init__(self, @@ -326,6 +337,7 @@ class _VersionAction(Action): default: _Text = ..., help: _Text = ...) -> None: ... +# undocumented class _SubParsersAction(Action): _ChoicesPseudoAction: Type[Any] # nested class _prog_prefix: _Text From 145328462208ac4a63ac3f776d0c2d5e07b50d80 Mon Sep 17 00:00:00 2001 From: Rebecca Chen Date: Thu, 29 Mar 2018 14:45:14 -0700 Subject: [PATCH 13/13] Make arguments to _ActionsContainer.add_argument more precise. --- stdlib/2and3/argparse.pyi | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/stdlib/2and3/argparse.pyi b/stdlib/2and3/argparse.pyi index dde124bf28e5..7bd539c2d38b 100644 --- a/stdlib/2and3/argparse.pyi +++ b/stdlib/2and3/argparse.pyi @@ -51,7 +51,19 @@ class _ActionsContainer: 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: ... + def add_argument(self, + *name_or_flags: _Text, + action: Union[_Text, Type[Action]] = ..., + nargs: Union[int, _Text] = ..., + const: Any = ..., + default: Any = ..., + type: Union[Callable[[_Text], _T], FileType] = ..., + choices: Iterable[_T] = ..., + required: bool = ..., + help: _Text = ..., + metavar: Union[_Text, Tuple[_Text, ...]] = ..., + dest: _Text = ..., + version: _Text = ...) -> Action: ... def add_argument_group(self, *args: Any, **kwargs: Any) -> _ArgumentGroup: ... def add_mutually_exclusive_group(self, **kwargs: Any) -> _MutuallyExclusiveGroup: ... def _add_action(self, action: _ActionT) -> _ActionT: ...