Skip to content

Add overloads for argparse functions that take a namespace to return a typevar bound to the input namespace #10307

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

Merged
merged 6 commits into from
Jun 12, 2023
27 changes: 19 additions & 8 deletions stdlib/argparse.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
add_help: bool = True,
allow_abbrev: bool = True,
) -> None: ...
# Ignore errors about overlapping overloads

@overload
def parse_args(self, args: Sequence[str] | None = None, namespace: None = None) -> Namespace: ... # type: ignore[misc]
@overload
Expand Down Expand Up @@ -209,16 +209,27 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
def print_help(self, file: IO[str] | None = None) -> None: ...
def format_usage(self) -> str: ...
def format_help(self) -> str: ...
def parse_known_args(
self, args: Sequence[str] | None = None, namespace: Namespace | None = None
) -> tuple[Namespace, list[str]]: ...
@overload
def parse_known_args(self, args: Sequence[str] | None = None, namespace: None = None) -> tuple[Namespace, list[str]]: ... # type: ignore[misc]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry for the churn, but could you try re-adding Namespace here?

Suggested change
def parse_known_args(self, args: Sequence[str] | None = None, namespace: None = None) -> tuple[Namespace, list[str]]: ... # type: ignore[misc]
def parse_known_args(self, args: Sequence[str] | None = None, namespace: Namespace | None = None) -> tuple[Namespace, list[str]]: ... # type: ignore[misc]

I know I asked you previously to remove it, but it seems that mypy has its problems with that in cases like this: https://github.com/pytest-dev/pytest/blob/2d824329ebf5455aaf4c6f8aa29afff401abe315/src/_pytest/config/argparsing.py#LL439C37-L439C53

I just want to see if that works better. (If it does, we should add Namespace to all the overloaded functions.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just pushed this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the pytest errors in primer have been resolved.

@overload
def parse_known_args(self, args: Sequence[str] | None, namespace: _N) -> tuple[_N, list[str]]: ...
@overload
def parse_known_args(self, *, namespace: _N) -> tuple[_N, list[str]]: ...
def convert_arg_line_to_args(self, arg_line: str) -> list[str]: ...
def exit(self, status: int = 0, message: str | None = None) -> NoReturn: ...
def error(self, message: str) -> NoReturn: ...
def parse_intermixed_args(self, args: Sequence[str] | None = None, namespace: Namespace | None = None) -> Namespace: ...
def parse_known_intermixed_args(
self, args: Sequence[str] | None = None, namespace: Namespace | None = None
) -> tuple[Namespace, list[str]]: ...
@overload
def parse_intermixed_args(self, args: Sequence[str] | None = None, namespace: None = None) -> Namespace: ... # type: ignore[misc]
@overload
def parse_intermixed_args(self, args: Sequence[str] | None, namespace: _N) -> _N: ...
@overload
def parse_intermixed_args(self, *, namespace: _N) -> _N: ...
@overload
def parse_known_intermixed_args(self, args: Sequence[str] | None = None, namespace: None = None) -> tuple[Namespace, list[str]]: ... # type: ignore[misc]
@overload
def parse_known_intermixed_args(self, args: Sequence[str] | None, namespace: _N) -> tuple[_N, list[str]]: ...
@overload
def parse_known_intermixed_args(self, *, namespace: _N) -> tuple[_N, list[str]]: ...
# undocumented
def _get_optional_actions(self) -> list[Action]: ...
def _get_positional_actions(self) -> list[Action]: ...
Expand Down