Skip to content

Commit c0aa284

Browse files
begin to enforce rules, although it's surprisingly failing
1 parent 350fd38 commit c0aa284

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

mypy/main.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,19 +369,44 @@ def infer_python_executable(options: Options, special_opts: argparse.Namespace)
369369
Define MYPYPATH for additional module search path entries.
370370
Define MYPY_CACHE_DIR to override configuration cache_dir path."""
371371

372+
def is_terminal_punctuation(char: str) -> bool:
373+
return char in (".", "?", "!")
372374

373375
class CapturableArgumentParser(argparse.ArgumentParser):
374376
"""Override ArgumentParser methods that use sys.stdout/sys.stderr directly.
375377
376378
This is needed because hijacking sys.std* is not thread-safe,
377379
yet output must be captured to properly support mypy.api.run.
380+
381+
Also enforces our style guides for groups and flags (ie, capitalization).
378382
"""
379383

380384
def __init__(self, *args: Any, **kwargs: Any) -> None:
381385
self.stdout = kwargs.pop("stdout", sys.stdout)
382386
self.stderr = kwargs.pop("stderr", sys.stderr)
383387
super().__init__(*args, **kwargs)
384388

389+
# =====================
390+
# Enforce style guide
391+
# =====================
392+
# We just hard fail on these, as CI will ensure the runtime errors never get to users.
393+
def add_argument_group(
394+
self,
395+
title: str,
396+
description: str | None = None,
397+
**kwargs,
398+
) -> argparse._ArgumentGroup:
399+
if title not in ["positional arguments", "options"]: # These are built-in names, ignore them.
400+
if not title[0].isupper():
401+
raise ValueError(f"CLI documentation style error: Title of group {title} must start with a capital letter. (Currently, '{title[0]}'.)")
402+
if description and not description[0].isupper():
403+
raise ValueError(f"CLI documentation style error: Description of group {title} must start with a capital letter. (Currently, '{description[0]}'.)")
404+
if is_terminal_punctuation(title[-1]):
405+
raise ValueError(f"CLI documentation style error: Title of group {title} must NOT end with terminal punction. (Currently, '{title[-1]}'.)")
406+
if description and not is_terminal_punctuation(title[-1]):
407+
raise ValueError(f"CLI documentation style error: Description of group {title} must end with terminal punction. (Currently, '{description[-1]}'.)")
408+
return super().add_argument_group(title, description, **kwargs)
409+
385410
# =====================
386411
# Help-printing methods
387412
# =====================

0 commit comments

Comments
 (0)