Skip to content

Commit d3fb269

Browse files
refactor out define_options
1 parent 24f29bc commit d3fb269

File tree

2 files changed

+32
-31
lines changed

2 files changed

+32
-31
lines changed

docs/source/html_builder.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from sphinx.builders.html import StandaloneHTMLBuilder
1212
from sphinx.environment import BuildEnvironment
1313

14-
from mypy.main import process_options
14+
from mypy.main import define_options
1515

1616

1717
class MypyHTMLBuilder(StandaloneHTMLBuilder):
@@ -28,8 +28,8 @@ def write_doc(self, docname: str, doctree: document) -> None:
2828
self._ref_to_doc.update({_id: docname for _id in doctree.ids})
2929

3030
def _add_strict_list(self) -> None:
31-
strict_flags: list[str] = []
32-
process_options(["-c", "pass"], list_to_fill_with_strict_flags=strict_flags)
31+
strict_flags: list[str]
32+
_, strict_flags, _ = define_options()
3333
strict_part = ", ".join(f":option:`{s} <mypy {s}>`" for s in strict_flags)
3434
if (
3535
not strict_part

mypy/main.py

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -461,32 +461,12 @@ def __call__(
461461
parser._print_message(formatter.format_help(), self.stdout)
462462
parser.exit()
463463

464-
465-
def process_options(
466-
args: list[str],
467-
stdout: TextIO | None = None,
468-
stderr: TextIO | None = None,
469-
require_targets: bool = True,
470-
server_options: bool = False,
471-
fscache: FileSystemCache | None = None,
472-
program: str = "mypy",
473-
header: str = HEADER,
474-
list_to_fill_with_strict_flags: list[str] | None = None,
475-
) -> tuple[list[BuildSource], Options]:
476-
"""Parse command line arguments.
477-
478-
If a FileSystemCache is passed in, and package_root options are given,
479-
call fscache.set_package_root() to set the cache's package root.
480-
481-
Returns a tuple of: a list of source files, an Options collected from flags.
482-
483-
If list_to_fill_with_strict_flags is provided and not none,
484-
then that list will be extended with the computed list of flags that --strict enables
485-
(as a sort of secret return option).
486-
"""
487-
stdout = stdout or sys.stdout
488-
stderr = stderr or sys.stderr
489-
464+
def define_options(program: str = "mypy", header: str = HEADER, stdout: TextIO = sys.stdout, stderr: TextIO = sys.stderr, server_options: bool = False) -> tuple[CapturableArgumentParser, list[str], list[tuple[str, bool]]]:
465+
"""Define the options in the parser (by calling a bunch of methods that express/build our desired command-line flags).
466+
Returns a tuple of:
467+
a parser object, that can parse command line arguments to mypy (expected consumer: main's process_options),
468+
a list of what flags are strict (expected consumer: docs' html_builder's _add_strict_list),
469+
strict_flag_assignments (expected consumer: main's process_options)."""
490470
parser = CapturableArgumentParser(
491471
prog=program,
492472
usage=header,
@@ -1328,6 +1308,29 @@ def add_invertible_flag(
13281308
dest="special-opts:files",
13291309
help="Type-check given files or directories",
13301310
)
1311+
return parser, strict_flag_names, strict_flag_assignments
1312+
1313+
def process_options(
1314+
args: list[str],
1315+
stdout: TextIO | None = None,
1316+
stderr: TextIO | None = None,
1317+
require_targets: bool = True,
1318+
server_options: bool = False,
1319+
fscache: FileSystemCache | None = None,
1320+
program: str = "mypy",
1321+
header: str = HEADER,
1322+
) -> tuple[list[BuildSource], Options]:
1323+
"""Parse command line arguments.
1324+
1325+
If a FileSystemCache is passed in, and package_root options are given,
1326+
call fscache.set_package_root() to set the cache's package root.
1327+
1328+
Returns a tuple of: a list of source files, an Options collected from flags.
1329+
"""
1330+
stdout = stdout if stdout is not None else sys.stdout
1331+
stderr = stderr if stderr is not None else sys.stderr
1332+
1333+
parser, _, strict_flag_assignments = define_options(header, program, stdout, stderr, server_options)
13311334

13321335
# Parse arguments once into a dummy namespace so we can get the
13331336
# filename for the config file and know if the user requested all strict options.
@@ -1520,8 +1523,6 @@ def set_strict_flags() -> None:
15201523
# exceptions of different types.
15211524
except InvalidSourceList as e2:
15221525
fail(str(e2), stderr, options)
1523-
if list_to_fill_with_strict_flags is not None:
1524-
list_to_fill_with_strict_flags.extend(strict_flag_names)
15251526
return targets, options
15261527

15271528

0 commit comments

Comments
 (0)