Skip to content

[docs] Include a real listing of the flags strict enables in the online documentation #19062

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

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/source/command_line.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 this version of mypy, the list is:

.. include:: strict_list.rst


.. option:: --disable-error-code

Expand Down
20 changes: 20 additions & 0 deletions docs/source/html_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,35 @@
from sphinx.builders.html import StandaloneHTMLBuilder
from sphinx.environment import BuildEnvironment

from mypy.main import process_options


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.srcdir) / "strict_list.rst"
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_list(self) -> None:
strict_flags: list[str] = []
process_options(["-c", "pass"], list_to_fill_with_strict_flags=strict_flags)
strict_part = ", ".join(f":option:`{s} <mypy {s}>`" for s in strict_flags)
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).")
self.strict_file.write_text(strict_part)

def _verify_error_codes(self) -> None:
from mypy.errorcodes import error_codes

Expand Down Expand Up @@ -55,6 +74,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]:
Expand Down
13 changes: 10 additions & 3 deletions mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,11 +471,18 @@ def process_options(
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
Expand Down Expand Up @@ -1502,11 +1509,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)
Expand All @@ -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
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(
Expand Down