From 993a8e3601605170eda90b94101e7e91eda8032c Mon Sep 17 00:00:00 2001 From: gbaian10 Date: Sat, 31 May 2025 11:08:12 +0800 Subject: [PATCH 1/3] refactor(cli.py): add type hints --- commitizen/cli.py | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/commitizen/cli.py b/commitizen/cli.py index deb5644d27..04a99e8c37 100644 --- a/commitizen/cli.py +++ b/commitizen/cli.py @@ -8,7 +8,7 @@ from functools import partial from pathlib import Path from types import TracebackType -from typing import Any +from typing import TYPE_CHECKING, Any, cast import argcomplete from decli import cli @@ -596,8 +596,33 @@ def parse_no_raise(comma_separated_no_raise: str) -> list[int]: return no_raise_codes +if TYPE_CHECKING: + + class Args(argparse.Namespace): + config: str | None = None + debug: bool = False + name: str | None = None + no_raise: str | None = None + report: bool = False + project: bool = False + commitizen: bool = False + verbose: bool = False + func: type[ + commands.Init # init + | commands.Commit # commit (c) + | commands.ListCz # ls + | commands.Example # example + | commands.Info # info + | commands.Schema # schema + | commands.Bump # bump + | commands.Changelog # changelog (ch) + | commands.Check # check + | commands.Version # version + ] + + def main(): - parser = cli(data) + parser: argparse.ArgumentParser = cli(data) argcomplete.autocomplete(parser) # Show help if no arg provided if len(sys.argv) == 1: @@ -611,7 +636,7 @@ def main(): # https://github.com/commitizen-tools/commitizen/issues/429 # argparse raises TypeError when non exist command is provided on Python < 3.9 # but raise SystemExit with exit code == 2 on Python 3.9 - if isinstance(e, TypeError) or (isinstance(e, SystemExit) and e.code == 2): + if isinstance(e, TypeError) or e.code == 2: raise NoCommandFoundError() raise e @@ -638,6 +663,7 @@ def main(): arguments["extra_cli_args"] = extra_args conf = config.read_cfg(args.config) + args = cast("Args", args) if args.name: conf.update({"name": args.name}) elif not conf.path: From d427396d94a7b97a1bca3ed6a8426e729aba2fff Mon Sep 17 00:00:00 2001 From: gbaian10 Date: Sat, 31 May 2025 13:55:34 +0800 Subject: [PATCH 2/3] refactor: add comment clarifying `no_raise` parsing to `list[int]` --- commitizen/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commitizen/cli.py b/commitizen/cli.py index 04a99e8c37..861ba578a9 100644 --- a/commitizen/cli.py +++ b/commitizen/cli.py @@ -602,7 +602,7 @@ class Args(argparse.Namespace): config: str | None = None debug: bool = False name: str | None = None - no_raise: str | None = None + no_raise: str | None = None # comma-separated string, later parsed as list[int] report: bool = False project: bool = False commitizen: bool = False From b5b76a0647394c82cab9a13435ca29d9a1252859 Mon Sep 17 00:00:00 2001 From: gbaian10 Date: Sat, 31 May 2025 14:24:07 +0800 Subject: [PATCH 3/3] refactor: remove `TypeError` handling since `Python >=3.9` is required Since the project requires Python >=3.9, argparse only raises SystemExit when non-existent commands are provided, making TypeError handling unnecessary. --- commitizen/cli.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/commitizen/cli.py b/commitizen/cli.py index 861ba578a9..d245f731e3 100644 --- a/commitizen/cli.py +++ b/commitizen/cli.py @@ -632,11 +632,8 @@ def main(): # This is for the command required constraint in 2.0 try: args, unknown_args = parser.parse_known_args() - except (TypeError, SystemExit) as e: - # https://github.com/commitizen-tools/commitizen/issues/429 - # argparse raises TypeError when non exist command is provided on Python < 3.9 - # but raise SystemExit with exit code == 2 on Python 3.9 - if isinstance(e, TypeError) or e.code == 2: + except SystemExit as e: + if e.code == 2: raise NoCommandFoundError() raise e