Skip to content

Commit ab2859b

Browse files
committed
helpconfig: improve typing in HelpAction
1 parent 349bfd9 commit ab2859b

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/_pytest/helpconfig.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,23 @@
33

44
from __future__ import annotations
55

6-
from argparse import Action
6+
import argparse
77
from collections.abc import Generator
8+
from collections.abc import Sequence
89
import os
910
import sys
11+
from typing import Any
1012

1113
from _pytest.config import Config
1214
from _pytest.config import ExitCode
1315
from _pytest.config import PrintHelp
16+
from _pytest.config.argparsing import MyOptionParser
1417
from _pytest.config.argparsing import Parser
1518
from _pytest.terminal import TerminalReporter
1619
import pytest
1720

1821

19-
class HelpAction(Action):
22+
class HelpAction(argparse.Action):
2023
"""An argparse Action that will raise an exception in order to skip the
2124
rest of the argument parsing when --help is passed.
2225
@@ -26,20 +29,29 @@ class HelpAction(Action):
2629
implemented by raising SystemExit.
2730
"""
2831

29-
def __init__(self, option_strings, dest=None, default=False, help=None):
32+
def __init__(
33+
self, option_strings: Sequence[str], dest: str, *, help: str | None = None
34+
) -> None:
3035
super().__init__(
3136
option_strings=option_strings,
3237
dest=dest,
33-
const=True,
34-
default=default,
3538
nargs=0,
39+
const=True,
40+
default=False,
3641
help=help,
3742
)
3843

39-
def __call__(self, parser, namespace, values, option_string=None):
44+
def __call__(
45+
self,
46+
parser: argparse.ArgumentParser,
47+
namespace: argparse.Namespace,
48+
values: str | Sequence[Any] | None,
49+
option_string: str | None = None,
50+
) -> None:
4051
setattr(namespace, self.dest, self.const)
4152

4253
# We should only skip the rest of the parsing after preparse is done.
54+
assert isinstance(parser, MyOptionParser)
4355
if getattr(parser._parser, "after_preparse", False):
4456
raise PrintHelp
4557

0 commit comments

Comments
 (0)