Skip to content

Add type hints for cli.py #1479

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

Merged
merged 3 commits into from
May 31, 2025
Merged
Changes from all 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
37 changes: 30 additions & 7 deletions commitizen/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 # comma-separated string, later parsed as list[int]
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:
Expand All @@ -607,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 (isinstance(e, SystemExit) and e.code == 2):
except SystemExit as e:
if e.code == 2:
raise NoCommandFoundError()
raise e

Expand All @@ -638,6 +660,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:
Expand Down