From fb25fe4814f190cdeb87643be6ac2283821bf20d Mon Sep 17 00:00:00 2001 From: wyattscarpenter Date: Thu, 8 May 2025 23:25:45 -0700 Subject: [PATCH 01/17] failed attempt, very regex-heavy --- docs/source/html_builder.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/docs/source/html_builder.py b/docs/source/html_builder.py index ea3594e0617b..731d2178f629 100644 --- a/docs/source/html_builder.py +++ b/docs/source/html_builder.py @@ -2,6 +2,7 @@ import json import os +import re import textwrap from pathlib import Path from typing import Any @@ -11,6 +12,7 @@ from sphinx.builders.html import StandaloneHTMLBuilder from sphinx.environment import BuildEnvironment +from mypy.api import run class MypyHTMLBuilder(StandaloneHTMLBuilder): def __init__(self, app: Sphinx, env: BuildEnvironment) -> None: @@ -21,6 +23,28 @@ def write_doc(self, docname: str, doctree: document) -> None: super().write_doc(docname, doctree) self._ref_to_doc.update({_id: docname for _id in doctree.ids}) + def _add_strict_to_doc(self) -> None: + target_filename = "command_line.html" + p = Path(self.outdir) / target_filename + text = p.read_bytes() + lead_in = b"over time." + c = text.count(lead_in) + complaint = f"Expected '{lead_in}' in {target_filename}, so I could add the strict flags after it, but " + if c < 1: + raise ValueError(complaint+"it was not there.") + elif c > 1: + raise ValueError(complaint+"it occurred in multiple locations, so I don't know what to do.") + help_text = run(['--help'])[0] + strict_regex = r"Strict mode; enables the following flags: (.*?)\r?\n --" + strict_part = re.match(strict_regex, help_text, re.DOTALL) + if strict_part is None: + print(help_text) + raise ValueError(f"Needed to match the regex r'{strict_regex}' in mypy --help, to enhance the docs, but it was not there.") + strict_part = strict_part[0] + if not strict_part or strict_part.isspace() or len(strict_part) < 20 or len(strict_part) > 2000: + raise ValueError(f"List of strict flags in the output is {strict_part}, which doesn't look right") + p.write_bytes(text.replace(lead_in, lead_in+b" The current list is: " + bytes(strict_part, encoding="ascii"))) + def _verify_error_codes(self) -> None: from mypy.errorcodes import error_codes @@ -55,6 +79,7 @@ def _write_ref_redirector(self) -> None: def finish(self) -> None: super().finish() self._write_ref_redirector() + self._add_strict_to_doc() def setup(app: Sphinx) -> dict[str, Any]: From e5e45e5bfbcbcec71fb0e82ea3b18af18fd7f635 Mon Sep 17 00:00:00 2001 From: wyattscarpenter Date: Thu, 8 May 2025 23:51:21 -0700 Subject: [PATCH 02/17] ok I mean it works now but it doubles up insertions --- docs/source/html_builder.py | 13 ++++--------- mypy/main.py | 6 ++---- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/docs/source/html_builder.py b/docs/source/html_builder.py index 731d2178f629..f556e6443e30 100644 --- a/docs/source/html_builder.py +++ b/docs/source/html_builder.py @@ -12,7 +12,7 @@ from sphinx.builders.html import StandaloneHTMLBuilder from sphinx.environment import BuildEnvironment -from mypy.api import run +from mypy.main import process_options class MypyHTMLBuilder(StandaloneHTMLBuilder): def __init__(self, app: Sphinx, env: BuildEnvironment) -> None: @@ -34,15 +34,10 @@ def _add_strict_to_doc(self) -> None: raise ValueError(complaint+"it was not there.") elif c > 1: raise ValueError(complaint+"it occurred in multiple locations, so I don't know what to do.") - help_text = run(['--help'])[0] - strict_regex = r"Strict mode; enables the following flags: (.*?)\r?\n --" - strict_part = re.match(strict_regex, help_text, re.DOTALL) - if strict_part is None: - print(help_text) - raise ValueError(f"Needed to match the regex r'{strict_regex}' in mypy --help, to enhance the docs, but it was not there.") - strict_part = strict_part[0] + help_text = process_options(['-c', 'pass']) + strict_part = help_text[2].split(": ")[1] if not strict_part or strict_part.isspace() or len(strict_part) < 20 or len(strict_part) > 2000: - raise ValueError(f"List of strict flags in the output is {strict_part}, which doesn't look right") + raise ValueError(f"{strict_part=}, which doesn't look right.") p.write_bytes(text.replace(lead_in, lead_in+b" The current list is: " + bytes(strict_part, encoding="ascii"))) def _verify_error_codes(self) -> None: diff --git a/mypy/main.py b/mypy/main.py index 7bd7215bbe2a..c1c02220b4e4 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -471,7 +471,7 @@ def process_options( fscache: FileSystemCache | None = None, program: str = "mypy", header: str = HEADER, -) -> tuple[list[BuildSource], Options]: +) -> tuple[list[BuildSource], Options, str]: """Parse command line arguments. If a FileSystemCache is passed in, and package_root options are given, @@ -1502,11 +1502,9 @@ def set_strict_flags() -> None: targets.extend(p_targets) for m in special_opts.modules: targets.append(BuildSource(None, m, None)) - return targets, options elif special_opts.command: options.build_type = BuildType.PROGRAM_TEXT targets = [BuildSource(None, None, "\n".join(special_opts.command))] - return targets, options else: try: targets = create_source_list(special_opts.files, options, fscache) @@ -1515,7 +1513,7 @@ def set_strict_flags() -> None: # exceptions of different types. except InvalidSourceList as e2: fail(str(e2), stderr, options) - return targets, options + return targets, options, strict_help def process_package_roots( From 13787f899869e7a266875af4b588b63611576532 Mon Sep 17 00:00:00 2001 From: wyattscarpenter Date: Fri, 9 May 2025 00:36:41 -0700 Subject: [PATCH 03/17] works perfectly --- docs/source/command_line.rst | 5 ++++- docs/source/html_builder.py | 23 ++++++----------------- mypy/main.py | 6 ++++-- 3 files changed, 14 insertions(+), 20 deletions(-) diff --git a/docs/source/command_line.rst b/docs/source/command_line.rst index b455e287017e..1787645ae122 100644 --- a/docs/source/command_line.rst +++ b/docs/source/command_line.rst @@ -764,7 +764,10 @@ of the above sections. :option:`mypy --help` output. Note: the exact list of flags enabled by running :option:`--strict` may change - over time. + over time. For the current version of mypy, the list is: + + .. include:: strict_list.rst + .. option:: --disable-error-code diff --git a/docs/source/html_builder.py b/docs/source/html_builder.py index f556e6443e30..80322c8e4d67 100644 --- a/docs/source/html_builder.py +++ b/docs/source/html_builder.py @@ -2,7 +2,6 @@ import json import os -import re import textwrap from pathlib import Path from typing import Any @@ -18,27 +17,18 @@ class MypyHTMLBuilder(StandaloneHTMLBuilder): def __init__(self, app: Sphinx, env: BuildEnvironment) -> None: super().__init__(app, env) self._ref_to_doc = {} + self._add_strict_list() def write_doc(self, docname: str, doctree: document) -> None: super().write_doc(docname, doctree) self._ref_to_doc.update({_id: docname for _id in doctree.ids}) - def _add_strict_to_doc(self) -> None: - target_filename = "command_line.html" - p = Path(self.outdir) / target_filename - text = p.read_bytes() - lead_in = b"over time." - c = text.count(lead_in) - complaint = f"Expected '{lead_in}' in {target_filename}, so I could add the strict flags after it, but " - if c < 1: - raise ValueError(complaint+"it was not there.") - elif c > 1: - raise ValueError(complaint+"it occurred in multiple locations, so I don't know what to do.") - help_text = process_options(['-c', 'pass']) - strict_part = help_text[2].split(": ")[1] + def _add_strict_list(self) -> None: + p = Path(self.outdir).parent.parent / "source" / "strict_list.rst" + strict_part = ", ".join(f":option:`{s} `" for s in process_options(['-c', 'pass'])[2]) if not strict_part or strict_part.isspace() or len(strict_part) < 20 or len(strict_part) > 2000: - raise ValueError(f"{strict_part=}, which doesn't look right.") - p.write_bytes(text.replace(lead_in, lead_in+b" The current list is: " + bytes(strict_part, encoding="ascii"))) + raise ValueError(f"{strict_part=}, which doesn't look right (by a simple heuristic).") + p.write_text(strict_part) def _verify_error_codes(self) -> None: from mypy.errorcodes import error_codes @@ -74,7 +64,6 @@ def _write_ref_redirector(self) -> None: def finish(self) -> None: super().finish() self._write_ref_redirector() - self._add_strict_to_doc() def setup(app: Sphinx) -> dict[str, Any]: diff --git a/mypy/main.py b/mypy/main.py index c1c02220b4e4..ffc53b4dfff2 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -471,11 +471,13 @@ def process_options( fscache: FileSystemCache | None = None, program: str = "mypy", header: str = HEADER, -) -> tuple[list[BuildSource], Options, str]: +) -> tuple[list[BuildSource], Options, list[str]]: """Parse command line arguments. If a FileSystemCache is passed in, and package_root options are given, call fscache.set_package_root() to set the cache's package root. + + Returns a tuple of: a list of source file, an Options collected from flags, and the computed list of strict flags. """ stdout = stdout or sys.stdout stderr = stderr or sys.stderr @@ -1513,7 +1515,7 @@ def set_strict_flags() -> None: # exceptions of different types. except InvalidSourceList as e2: fail(str(e2), stderr, options) - return targets, options, strict_help + return targets, options, strict_flag_names def process_package_roots( From f9e20e68381c6c32bb944b9134c3bddc1603222c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 9 May 2025 07:41:42 +0000 Subject: [PATCH 04/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- docs/source/html_builder.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/docs/source/html_builder.py b/docs/source/html_builder.py index 80322c8e4d67..9d2bc025980b 100644 --- a/docs/source/html_builder.py +++ b/docs/source/html_builder.py @@ -13,6 +13,7 @@ from mypy.main import process_options + class MypyHTMLBuilder(StandaloneHTMLBuilder): def __init__(self, app: Sphinx, env: BuildEnvironment) -> None: super().__init__(app, env) @@ -25,9 +26,16 @@ def write_doc(self, docname: str, doctree: document) -> None: def _add_strict_list(self) -> None: p = Path(self.outdir).parent.parent / "source" / "strict_list.rst" - strict_part = ", ".join(f":option:`{s} `" for s in process_options(['-c', 'pass'])[2]) - if not strict_part or strict_part.isspace() or len(strict_part) < 20 or len(strict_part) > 2000: - raise ValueError(f"{strict_part=}, which doesn't look right (by a simple heuristic).") + strict_part = ", ".join( + f":option:`{s} `" for s in process_options(["-c", "pass"])[2] + ) + if ( + not strict_part + or strict_part.isspace() + or len(strict_part) < 20 + or len(strict_part) > 2000 + ): + raise ValueError(f"{strict_part=}, which doesn't look right (by a simple heuristic).") p.write_text(strict_part) def _verify_error_codes(self) -> None: From 9de7d65205128c6fca136f77b40c134f499d7b38 Mon Sep 17 00:00:00 2001 From: wyattscarpenter Date: Fri, 9 May 2025 00:59:32 -0700 Subject: [PATCH 05/17] get the strict flags another way, to make all the tests pass --- docs/source/html_builder.py | 4 +++- mypy/main.py | 13 ++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/docs/source/html_builder.py b/docs/source/html_builder.py index 9d2bc025980b..6ab6476cc712 100644 --- a/docs/source/html_builder.py +++ b/docs/source/html_builder.py @@ -26,8 +26,10 @@ def write_doc(self, docname: str, doctree: document) -> None: def _add_strict_list(self) -> None: p = Path(self.outdir).parent.parent / "source" / "strict_list.rst" + strict_flags: list[str] = [] + process_options(["-c", "pass"], list_to_fill_with_strict_flags=strict_flags) strict_part = ", ".join( - f":option:`{s} `" for s in process_options(["-c", "pass"])[2] + f":option:`{s} `" for s in strict_flags ) if ( not strict_part diff --git a/mypy/main.py b/mypy/main.py index ffc53b4dfff2..7eeba2623410 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -471,13 +471,18 @@ def process_options( fscache: FileSystemCache | None = None, program: str = "mypy", header: str = HEADER, -) -> tuple[list[BuildSource], Options, list[str]]: + list_to_fill_with_strict_flags: list[str] | None = None +) -> tuple[list[BuildSource], Options]: """Parse command line arguments. If a FileSystemCache is passed in, and package_root options are given, call fscache.set_package_root() to set the cache's package root. - Returns a tuple of: a list of source file, an Options collected from flags, and the computed list of strict flags. + Returns a tuple of: a list of source files, an Options collected from flags. + + If list_to_fill_with_strict_flags is provided and not none, + then that list will be extended with the computed list of flags that --strict enables + (as a sort of secret return option). """ stdout = stdout or sys.stdout stderr = stderr or sys.stderr @@ -1515,7 +1520,9 @@ def set_strict_flags() -> None: # exceptions of different types. except InvalidSourceList as e2: fail(str(e2), stderr, options) - return targets, options, strict_flag_names + if list_to_fill_with_strict_flags is not None: + list_to_fill_with_strict_flags.extend(strict_flag_names) + return targets, options def process_package_roots( From 80dc22d7fed7fca096d6b7af014b6db045e40503 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 9 May 2025 08:00:57 +0000 Subject: [PATCH 06/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- docs/source/html_builder.py | 4 +--- mypy/main.py | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/docs/source/html_builder.py b/docs/source/html_builder.py index 6ab6476cc712..b5980190a707 100644 --- a/docs/source/html_builder.py +++ b/docs/source/html_builder.py @@ -28,9 +28,7 @@ def _add_strict_list(self) -> None: p = Path(self.outdir).parent.parent / "source" / "strict_list.rst" strict_flags: list[str] = [] process_options(["-c", "pass"], list_to_fill_with_strict_flags=strict_flags) - strict_part = ", ".join( - f":option:`{s} `" for s in strict_flags - ) + strict_part = ", ".join(f":option:`{s} `" for s in strict_flags) if ( not strict_part or strict_part.isspace() diff --git a/mypy/main.py b/mypy/main.py index 7eeba2623410..bbcdbe8c8f13 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -471,7 +471,7 @@ def process_options( fscache: FileSystemCache | None = None, program: str = "mypy", header: str = HEADER, - list_to_fill_with_strict_flags: list[str] | None = None + list_to_fill_with_strict_flags: list[str] | None = None, ) -> tuple[list[BuildSource], Options]: """Parse command line arguments. From 0bd84f9807d216197696879b3064b09f5a27e501 Mon Sep 17 00:00:00 2001 From: wyattscarpenter Date: Fri, 9 May 2025 01:14:33 -0700 Subject: [PATCH 07/17] generate the strict list in the build dir, so there won't be any funky path trouble --- docs/source/command_line.rst | 2 +- docs/source/html_builder.py | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/source/command_line.rst b/docs/source/command_line.rst index 1787645ae122..04968a7530a8 100644 --- a/docs/source/command_line.rst +++ b/docs/source/command_line.rst @@ -764,7 +764,7 @@ of the above sections. :option:`mypy --help` output. Note: the exact list of flags enabled by running :option:`--strict` may change - over time. For the current version of mypy, the list is: + over time. For the this version of mypy, the list is: .. include:: strict_list.rst diff --git a/docs/source/html_builder.py b/docs/source/html_builder.py index b5980190a707..9d0a7e84e18c 100644 --- a/docs/source/html_builder.py +++ b/docs/source/html_builder.py @@ -15,9 +15,11 @@ class MypyHTMLBuilder(StandaloneHTMLBuilder): + strict_file: Path def __init__(self, app: Sphinx, env: BuildEnvironment) -> None: super().__init__(app, env) self._ref_to_doc = {} + self.strict_file = Path(self.outdir) / "strict_list.rst" self._add_strict_list() def write_doc(self, docname: str, doctree: document) -> None: @@ -25,7 +27,6 @@ def write_doc(self, docname: str, doctree: document) -> None: self._ref_to_doc.update({_id: docname for _id in doctree.ids}) def _add_strict_list(self) -> None: - p = Path(self.outdir).parent.parent / "source" / "strict_list.rst" strict_flags: list[str] = [] process_options(["-c", "pass"], list_to_fill_with_strict_flags=strict_flags) strict_part = ", ".join(f":option:`{s} `" for s in strict_flags) @@ -36,7 +37,7 @@ def _add_strict_list(self) -> None: or len(strict_part) > 2000 ): raise ValueError(f"{strict_part=}, which doesn't look right (by a simple heuristic).") - p.write_text(strict_part) + self.strict_file.write_text(strict_part) def _verify_error_codes(self) -> None: from mypy.errorcodes import error_codes @@ -72,6 +73,7 @@ def _write_ref_redirector(self) -> None: def finish(self) -> None: super().finish() self._write_ref_redirector() + self.strict_file.unlink() def setup(app: Sphinx) -> dict[str, Any]: From 6f47aee9f8400eb4fa5fd59877d78d3353ccdf63 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 9 May 2025 08:16:49 +0000 Subject: [PATCH 08/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- docs/source/html_builder.py | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/source/html_builder.py b/docs/source/html_builder.py index 9d0a7e84e18c..3791e17a867d 100644 --- a/docs/source/html_builder.py +++ b/docs/source/html_builder.py @@ -16,6 +16,7 @@ class MypyHTMLBuilder(StandaloneHTMLBuilder): strict_file: Path + def __init__(self, app: Sphinx, env: BuildEnvironment) -> None: super().__init__(app, env) self._ref_to_doc = {} From 18a695ee66ee6a4a2292d7a3b939ca4165a920f7 Mon Sep 17 00:00:00 2001 From: wyattscarpenter Date: Fri, 9 May 2025 01:32:37 -0700 Subject: [PATCH 09/17] um, I meant srcdir. Sure. --- docs/source/command_line.rst | 2 +- docs/source/html_builder.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/source/command_line.rst b/docs/source/command_line.rst index 04968a7530a8..37ba83bccbc1 100644 --- a/docs/source/command_line.rst +++ b/docs/source/command_line.rst @@ -764,7 +764,7 @@ of the above sections. :option:`mypy --help` output. Note: the exact list of flags enabled by running :option:`--strict` may change - over time. For the this version of mypy, the list is: + over time. For this version of mypy, the list is: .. include:: strict_list.rst diff --git a/docs/source/html_builder.py b/docs/source/html_builder.py index 3791e17a867d..38800872c0cb 100644 --- a/docs/source/html_builder.py +++ b/docs/source/html_builder.py @@ -15,12 +15,12 @@ class MypyHTMLBuilder(StandaloneHTMLBuilder): - strict_file: Path + strict_file: Sphinx._StrPath def __init__(self, app: Sphinx, env: BuildEnvironment) -> None: super().__init__(app, env) self._ref_to_doc = {} - self.strict_file = Path(self.outdir) / "strict_list.rst" + self.strict_file = Path(self.srcdir) / "strict_list.rst" self._add_strict_list() def write_doc(self, docname: str, doctree: document) -> None: From 24f29bc1e0b62a1586ccafab0a23b2caba788c14 Mon Sep 17 00:00:00 2001 From: wyattscarpenter Date: Fri, 9 May 2025 01:33:42 -0700 Subject: [PATCH 10/17] bruh --- docs/source/html_builder.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/html_builder.py b/docs/source/html_builder.py index 38800872c0cb..26d1e63db940 100644 --- a/docs/source/html_builder.py +++ b/docs/source/html_builder.py @@ -15,7 +15,7 @@ class MypyHTMLBuilder(StandaloneHTMLBuilder): - strict_file: Sphinx._StrPath + strict_file: Path def __init__(self, app: Sphinx, env: BuildEnvironment) -> None: super().__init__(app, env) From d3fb269293f8142d74c4b5f042b53a18abb93a2c Mon Sep 17 00:00:00 2001 From: wyattscarpenter Date: Fri, 9 May 2025 22:18:16 -0700 Subject: [PATCH 11/17] refactor out define_options --- docs/source/html_builder.py | 6 ++-- mypy/main.py | 57 +++++++++++++++++++------------------ 2 files changed, 32 insertions(+), 31 deletions(-) diff --git a/docs/source/html_builder.py b/docs/source/html_builder.py index 26d1e63db940..71d48c985c4e 100644 --- a/docs/source/html_builder.py +++ b/docs/source/html_builder.py @@ -11,7 +11,7 @@ from sphinx.builders.html import StandaloneHTMLBuilder from sphinx.environment import BuildEnvironment -from mypy.main import process_options +from mypy.main import define_options class MypyHTMLBuilder(StandaloneHTMLBuilder): @@ -28,8 +28,8 @@ def write_doc(self, docname: str, doctree: document) -> None: self._ref_to_doc.update({_id: docname for _id in doctree.ids}) def _add_strict_list(self) -> None: - strict_flags: list[str] = [] - process_options(["-c", "pass"], list_to_fill_with_strict_flags=strict_flags) + strict_flags: list[str] + _, strict_flags, _ = define_options() strict_part = ", ".join(f":option:`{s} `" for s in strict_flags) if ( not strict_part diff --git a/mypy/main.py b/mypy/main.py index bbcdbe8c8f13..b56ea2c62a6e 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -461,32 +461,12 @@ def __call__( parser._print_message(formatter.format_help(), self.stdout) parser.exit() - -def process_options( - args: list[str], - stdout: TextIO | None = None, - stderr: TextIO | None = None, - require_targets: bool = True, - server_options: bool = False, - fscache: FileSystemCache | None = None, - program: str = "mypy", - header: str = HEADER, - list_to_fill_with_strict_flags: list[str] | None = None, -) -> tuple[list[BuildSource], Options]: - """Parse command line arguments. - - If a FileSystemCache is passed in, and package_root options are given, - call fscache.set_package_root() to set the cache's package root. - - Returns a tuple of: a list of source files, an Options collected from flags. - - If list_to_fill_with_strict_flags is provided and not none, - then that list will be extended with the computed list of flags that --strict enables - (as a sort of secret return option). - """ - stdout = stdout or sys.stdout - stderr = stderr or sys.stderr - +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]]]: + """Define the options in the parser (by calling a bunch of methods that express/build our desired command-line flags). + Returns a tuple of: + a parser object, that can parse command line arguments to mypy (expected consumer: main's process_options), + a list of what flags are strict (expected consumer: docs' html_builder's _add_strict_list), + strict_flag_assignments (expected consumer: main's process_options).""" parser = CapturableArgumentParser( prog=program, usage=header, @@ -1328,6 +1308,29 @@ def add_invertible_flag( dest="special-opts:files", help="Type-check given files or directories", ) + return parser, strict_flag_names, strict_flag_assignments + +def process_options( + args: list[str], + stdout: TextIO | None = None, + stderr: TextIO | None = None, + require_targets: bool = True, + server_options: bool = False, + fscache: FileSystemCache | None = None, + program: str = "mypy", + header: str = HEADER, +) -> tuple[list[BuildSource], Options]: + """Parse command line arguments. + + If a FileSystemCache is passed in, and package_root options are given, + call fscache.set_package_root() to set the cache's package root. + + Returns a tuple of: a list of source files, an Options collected from flags. + """ + stdout = stdout if stdout is not None else sys.stdout + stderr = stderr if stderr is not None else sys.stderr + + parser, _, strict_flag_assignments = define_options(header, program, stdout, stderr, server_options) # Parse arguments once into a dummy namespace so we can get the # filename for the config file and know if the user requested all strict options. @@ -1520,8 +1523,6 @@ def set_strict_flags() -> None: # exceptions of different types. except InvalidSourceList as e2: fail(str(e2), stderr, options) - if list_to_fill_with_strict_flags is not None: - list_to_fill_with_strict_flags.extend(strict_flag_names) return targets, options From c07f095e1ac50cbb3d5d27c28b4d26aae860da9d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 10 May 2025 05:23:15 +0000 Subject: [PATCH 12/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mypy/main.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/mypy/main.py b/mypy/main.py index b56ea2c62a6e..ac7045bb238b 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -461,7 +461,14 @@ def __call__( parser._print_message(formatter.format_help(), self.stdout) parser.exit() -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]]]: + +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]]]: """Define the options in the parser (by calling a bunch of methods that express/build our desired command-line flags). Returns a tuple of: a parser object, that can parse command line arguments to mypy (expected consumer: main's process_options), @@ -1310,6 +1317,7 @@ def add_invertible_flag( ) return parser, strict_flag_names, strict_flag_assignments + def process_options( args: list[str], stdout: TextIO | None = None, @@ -1330,7 +1338,9 @@ def process_options( stdout = stdout if stdout is not None else sys.stdout stderr = stderr if stderr is not None else sys.stderr - parser, _, strict_flag_assignments = define_options(header, program, stdout, stderr, server_options) + parser, _, strict_flag_assignments = define_options( + header, program, stdout, stderr, server_options + ) # Parse arguments once into a dummy namespace so we can get the # filename for the config file and know if the user requested all strict options. From 3ed20883a9c11d53fc79bbb368afc78fe29a5d03 Mon Sep 17 00:00:00 2001 From: wyattscarpenter Date: Sat, 10 May 2025 02:23:10 -0700 Subject: [PATCH 13/17] my arguments were in the wrong order --- mypy/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy/main.py b/mypy/main.py index ac7045bb238b..f785020b38a0 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -1339,7 +1339,7 @@ def process_options( stderr = stderr if stderr is not None else sys.stderr parser, _, strict_flag_assignments = define_options( - header, program, stdout, stderr, server_options + program, header, stdout, stderr, server_options ) # Parse arguments once into a dummy namespace so we can get the From 5a6ad43b082c63ce75d83dbf8f9fc40a02bc29c1 Mon Sep 17 00:00:00 2001 From: wyattscarpenter Date: Sat, 10 May 2025 05:15:07 -0700 Subject: [PATCH 14/17] Move the start of the list to the generation code I remembered that sometimes this documentation gets processed into other formats, like a man page. I'm not sure what the process is there, but let's save them a little potential embarassment of introducing but not having a list --- docs/source/command_line.rst | 2 +- docs/source/html_builder.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/source/command_line.rst b/docs/source/command_line.rst index 37ba83bccbc1..0a8ed2c2a828 100644 --- a/docs/source/command_line.rst +++ b/docs/source/command_line.rst @@ -764,7 +764,7 @@ of the above sections. :option:`mypy --help` output. Note: the exact list of flags enabled by running :option:`--strict` may change - over time. For this version of mypy, the list is: + over time. .. include:: strict_list.rst diff --git a/docs/source/html_builder.py b/docs/source/html_builder.py index 71d48c985c4e..cf3e3131826d 100644 --- a/docs/source/html_builder.py +++ b/docs/source/html_builder.py @@ -38,7 +38,7 @@ def _add_strict_list(self) -> None: or len(strict_part) > 2000 ): raise ValueError(f"{strict_part=}, which doesn't look right (by a simple heuristic).") - self.strict_file.write_text(strict_part) + self.strict_file.write_text("For this version of mypy, the list of flags enabled by :option:`--strict` is: "+strict_part) def _verify_error_codes(self) -> None: from mypy.errorcodes import error_codes From b6b3863adecb439bba9dee650c6f109da6d4b831 Mon Sep 17 00:00:00 2001 From: wyattscarpenter Date: Sat, 10 May 2025 05:17:12 -0700 Subject: [PATCH 15/17] remove syntax that causes annoying error message during generation without loss of generality --- docs/source/html_builder.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/source/html_builder.py b/docs/source/html_builder.py index cf3e3131826d..387f7f13b4c2 100644 --- a/docs/source/html_builder.py +++ b/docs/source/html_builder.py @@ -38,7 +38,9 @@ def _add_strict_list(self) -> None: or len(strict_part) > 2000 ): raise ValueError(f"{strict_part=}, which doesn't look right (by a simple heuristic).") - self.strict_file.write_text("For this version of mypy, the list of flags enabled by :option:`--strict` is: "+strict_part) + self.strict_file.write_text( + "For this version of mypy, the list of flags enabled by strict is: " + strict_part + ) def _verify_error_codes(self) -> None: from mypy.errorcodes import error_codes From 5c16af08f98bc533d41903729405891aa2867ea5 Mon Sep 17 00:00:00 2001 From: wyattscarpenter Date: Mon, 12 May 2025 11:38:29 -0700 Subject: [PATCH 16/17] add comment about autogeneration --- docs/source/command_line.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/source/command_line.rst b/docs/source/command_line.rst index 0a8ed2c2a828..9e6b1569b50d 100644 --- a/docs/source/command_line.rst +++ b/docs/source/command_line.rst @@ -767,6 +767,11 @@ of the above sections. over time. .. include:: strict_list.rst + .. + The above file is autogenerated and included during html generation. + (That's an include directive, and this is a comment.) + It would be fine to generate it at some other time instead, + theoreticaly, but we already had a convenient hook during html gen. .. option:: --disable-error-code From ef5226270ded1a179659fcaa36e982cad5e86743 Mon Sep 17 00:00:00 2001 From: wyattscarpenter Date: Mon, 12 May 2025 15:40:17 -0700 Subject: [PATCH 17/17] theoreticaly ==> theoretically thanks codespell! --- docs/source/command_line.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/command_line.rst b/docs/source/command_line.rst index 9e6b1569b50d..496a112fc0d2 100644 --- a/docs/source/command_line.rst +++ b/docs/source/command_line.rst @@ -771,7 +771,7 @@ of the above sections. The above file is autogenerated and included during html generation. (That's an include directive, and this is a comment.) It would be fine to generate it at some other time instead, - theoreticaly, but we already had a convenient hook during html gen. + theoretically, but we already had a convenient hook during html gen. .. option:: --disable-error-code