Skip to content

4.8.3 candidate #1457

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 77 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
38f4438
refactor: improve readability and fix typos
bearomorphism May 15, 2025
529a6d3
refactor(version_scheme): cleanup
bearomorphism May 17, 2025
db885e7
refactor(commit): simplify call
bearomorphism May 17, 2025
292e7b9
test(commit): when nothing is added to commit
bearomorphism May 17, 2025
e8c7b0e
refactor(git): code cleanup and better test coverage
bearomorphism May 15, 2025
62183c7
test(git): add test for from_rev_and_commit
bearomorphism May 17, 2025
c10a95a
docs(git): from_rev_and_commit docstring
bearomorphism May 17, 2025
86323d7
refactor(EOLType): add eol enum back and reorganize methods
bearomorphism May 18, 2025
bdc82ae
refactor(git): refactor get_tag_names
bearomorphism May 18, 2025
d74e4fd
refactor(changelog): minor cleanup
bearomorphism May 19, 2025
3cd3b35
refactor(BaseConfig): use setter
bearomorphism May 20, 2025
d620aaa
build(poetry): upgrade mypy version to ^1.15.0
bearomorphism May 20, 2025
bb19549
refactor(bump): add type for out, replace function with re escape
bearomorphism May 17, 2025
dc2fd32
refactor(bump): clean up
bearomorphism May 16, 2025
11ddbfd
test(bump): improve test coverage
bearomorphism May 16, 2025
c27560c
fix(defaults): add non-capitalized default constants back and depreca…
bearomorphism May 22, 2025
e67d92b
refactor: misc cleanup
bearomorphism May 17, 2025
53e1edf
refactor(git): extract _create_commit_cmd_string
bearomorphism May 19, 2025
3897e1b
test(test_git): mock os
bearomorphism May 23, 2025
7c5c08b
refactor(cli): early return and improve test coverage
bearomorphism May 23, 2025
fea0fa0
build(termcolor): remove termcolor <3 restriction
bearomorphism May 23, 2025
40f3eb5
build: specify importlib-metadata version to fix unit tests
bearomorphism May 25, 2025
636013e
refactor(changelog): better typing, yield
bearomorphism May 24, 2025
b43498e
build(deps-dev): bump mypy to 1.16.0
gbaian10 May 31, 2025
8b9f60f
refactor(mypy): remove `unused-ignore`
gbaian10 May 31, 2025
e838ddf
refactor(cli.py): add type hints
gbaian10 May 31, 2025
aa9e1ab
refactor: add comment clarifying `no_raise` parsing to `list[int]`
gbaian10 May 31, 2025
c83ed87
refactor: remove `TypeError` handling since `Python >=3.9` is required
gbaian10 May 31, 2025
f21bf17
fix(deprecated): mark deprecate in v5
bearomorphism Jun 1, 2025
dcd8eb5
build(deps-dev): bump ruff
gbaian10 May 31, 2025
0b7f8ff
style(ruff): resolve F401
gbaian10 May 31, 2025
6abfb50
build(ruff): add RUF022 & RUF100 rules
gbaian10 May 31, 2025
76cd0c3
style(ruff): run `ruff check --fix`
gbaian10 May 31, 2025
1a0d288
refactor(BaseCommitizen): remove unused process_commit
bearomorphism May 30, 2025
7540bcf
style(tags): improve types
bearomorphism May 29, 2025
93c98b0
perf(tags): use set
bearomorphism May 29, 2025
e36febf
refactor: fix mypy output and better type
bearomorphism May 29, 2025
0ac3217
refactor(conventional_commits): remove unnecessary checks
bearomorphism May 29, 2025
ec976c1
refactor: make methods protected, better type
bearomorphism May 30, 2025
9406b55
style: type untyped methods
bearomorphism May 30, 2025
5c5f005
style: remove Union
bearomorphism May 30, 2025
23830f1
style: better type
bearomorphism May 30, 2025
d4ee14a
style: add `-> None` to __init__
bearomorphism May 30, 2025
af1b9c2
build(ruff,mypy): more strict rules
bearomorphism May 30, 2025
5d72c64
fix(BaseConfig): mypy error
bearomorphism May 31, 2025
8ab84d8
build(mypy): remove disallow_untyped_defs because it's already done b…
bearomorphism May 31, 2025
9223231
refactor(bump): TypedDict for bump argument
bearomorphism May 30, 2025
8f6d386
refactor(changelog): type untyped arguments
bearomorphism May 31, 2025
b840c45
refactor(check): remove unused argument
bearomorphism May 31, 2025
8a18460
style(bump): rename class for consistency
bearomorphism May 31, 2025
3fef02f
refactor(check): type CheckArgs arguments
bearomorphism May 31, 2025
1c30e7e
refactor(commit): type commit args
bearomorphism May 31, 2025
d273c02
refactor(commands): remove unused args, type version command args
bearomorphism May 31, 2025
8b3d17c
style(cli): shorten arg type
bearomorphism May 31, 2025
a22d028
docs(bump): comment on a stupid looking pattern
bearomorphism May 31, 2025
ffacc6e
fix(Check): make parameters backward compatiable
bearomorphism May 31, 2025
a710d6d
style(changelog): rename parameter for consistency
bearomorphism May 31, 2025
f67ad4c
refactor(bump): improve readability and still bypass mypy check
bearomorphism May 31, 2025
4aaddad
refactor: remove unnecessary bool() and remove Any type from TypedDic…
bearomorphism May 31, 2025
a512174
style(changelog): add TODO to fixable type ignores
bearomorphism Jun 4, 2025
f9d0834
style(cli): more specific type ignore
bearomorphism Jun 4, 2025
a440bbc
style(cli): rename kwarg to values
bearomorphism Jun 4, 2025
e5452e7
refactor(bump): use any to replace 'or' chain
bearomorphism Jun 6, 2025
a9cd957
perf(bump): avoid unnecessary list construction and rename variable t…
bearomorphism Jun 5, 2025
52be917
refactor(bump): eliminate similar patterns in code
bearomorphism Jun 7, 2025
542d46b
refactor(git): simplify tag logic
bearomorphism Jun 5, 2025
ca0f250
refactor(git): retype get_commits parameter to make it more friendly …
bearomorphism Jun 5, 2025
6faa8a8
refactor(bump): simplify nested if
bearomorphism Jun 5, 2025
b31e0cc
fix(commit): emit deprecated warning of cz commit -s
bearomorphism Jun 1, 2025
9320f7e
fix(cli): update description for deprecate warning
bearomorphism Jun 1, 2025
626e67c
refactor(questions): type questions with TypedDict
bearomorphism May 31, 2025
a272305
refactor(check): compile once and rename variable
bearomorphism May 30, 2025
a92dbe3
refactor: do not guess if changelog format is provided
bearomorphism May 30, 2025
965f34e
refactor(conventional_commits): make schema_pattern more readable
bearomorphism May 30, 2025
6cbe580
refactor(conventional_commits): use TypedDict for answers
bearomorphism May 30, 2025
56b113c
refactor(jira): refactor message
bearomorphism May 30, 2025
d968f26
style: replace dict with Mapping
bearomorphism May 30, 2025
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
34 changes: 17 additions & 17 deletions commitizen/bump.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import re
from collections import OrderedDict
from collections.abc import Iterable
from glob import iglob
from logging import getLogger
from string import Template
Expand Down Expand Up @@ -61,7 +62,7 @@ def find_increment(
def update_version_in_files(
current_version: str,
new_version: str,
files: list[str],
files: Iterable[str],
*,
check_consistency: bool = False,
encoding: str = ENCODING,
Expand All @@ -76,7 +77,7 @@ def update_version_in_files(
"""
# TODO: separate check step and write step
updated = []
for path, regex in files_and_regexs(files, current_version):
for path, regex in _files_and_regexes(files, current_version):
current_version_found, version_file = _bump_with_regex(
path,
current_version,
Expand All @@ -99,21 +100,22 @@ def update_version_in_files(
return updated


def files_and_regexs(patterns: list[str], version: str) -> list[tuple[str, str]]:
def _files_and_regexes(patterns: Iterable[str], version: str) -> list[tuple[str, str]]:
"""
Resolve all distinct files with their regexp from a list of glob patterns with optional regexp
"""
out = []
out: set[tuple[str, str]] = set()
for pattern in patterns:
drive, tail = os.path.splitdrive(pattern)
path, _, regex = tail.partition(":")
filepath = drive + path
if not regex:
regex = _version_to_regex(version)
regex = re.escape(version)

for path in iglob(filepath):
out.append((path, regex))
return sorted(list(set(out)))
for file in iglob(filepath):
out.add((file, regex))

return sorted(out)


def _bump_with_regex(
Expand All @@ -128,18 +130,16 @@ def _bump_with_regex(
pattern = re.compile(regex)
with open(version_filepath, encoding=encoding) as f:
for line in f:
if pattern.search(line):
bumped_line = line.replace(current_version, new_version)
if bumped_line != line:
current_version_found = True
lines.append(bumped_line)
else:
if not pattern.search(line):
lines.append(line)
return current_version_found, "".join(lines)
continue

bumped_line = line.replace(current_version, new_version)
if bumped_line != line:
current_version_found = True
lines.append(bumped_line)

def _version_to_regex(version: str) -> str:
return version.replace(".", r"\.").replace("+", r"\+")
return current_version_found, "".join(lines)


def create_commit_message(
Expand Down
49 changes: 26 additions & 23 deletions commitizen/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@

import re
from collections import OrderedDict, defaultdict
from collections.abc import Iterable
from collections.abc import Generator, Iterable, Mapping, Sequence
from dataclasses import dataclass
from datetime import date
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Any

from jinja2 import (
BaseLoader,
Expand Down Expand Up @@ -63,7 +63,7 @@ class Metadata:
latest_version_position: int | None = None
latest_version_tag: str | None = None

def __post_init__(self):
def __post_init__(self) -> None:
if self.latest_version and not self.latest_version_tag:
# Test syntactic sugar
# latest version tag is optional if same as latest version
Expand All @@ -84,7 +84,7 @@ def generate_tree_from_commits(
changelog_message_builder_hook: MessageBuilderHook | None = None,
changelog_release_hook: ChangelogReleaseHook | None = None,
rules: TagRules | None = None,
) -> Iterable[dict]:
) -> Generator[dict[str, Any], None, None]:
pat = re.compile(changelog_pattern)
map_pat = re.compile(commit_parser, re.MULTILINE)
body_map_pat = re.compile(commit_parser, re.MULTILINE | re.DOTALL)
Expand Down Expand Up @@ -169,8 +169,8 @@ def process_commit_message(
commit: GitCommit,
changes: dict[str | None, list],
change_type_map: dict[str, str] | None = None,
):
message: dict = {
) -> None:
message: dict[str, Any] = {
"sha1": commit.rev,
"parents": commit.parents,
"author": commit.author,
Expand All @@ -187,24 +187,27 @@ def process_commit_message(
changes[change_type].append(msg)


def order_changelog_tree(tree: Iterable, change_type_order: list[str]) -> Iterable:
def generate_ordered_changelog_tree(
tree: Iterable[Mapping[str, Any]], change_type_order: list[str]
) -> Generator[dict[str, Any], None, None]:
if len(set(change_type_order)) != len(change_type_order):
raise InvalidConfigurationError(
f"Change types contain duplicates types ({change_type_order})"
f"Change types contain duplicated types ({change_type_order})"
)

sorted_tree = []
for entry in tree:
ordered_change_types = change_type_order + sorted(
set(entry["changes"].keys()) - set(change_type_order)
)
changes = [
(ct, entry["changes"][ct])
for ct in ordered_change_types
if ct in entry["changes"]
]
sorted_tree.append({**entry, **{"changes": OrderedDict(changes)}})
return sorted_tree
yield {
**entry,
"changes": _calculate_sorted_changes(change_type_order, entry["changes"]),
}


def _calculate_sorted_changes(
change_type_order: list[str], changes: Mapping[str, Any]
) -> OrderedDict[str, Any]:
remaining_change_types = set(changes.keys()) - set(change_type_order)
sorted_change_types = change_type_order + sorted(remaining_change_types)
return OrderedDict((ct, changes[ct]) for ct in sorted_change_types if ct in changes)


def get_changelog_template(loader: BaseLoader, template: str) -> Template:
Expand All @@ -222,7 +225,7 @@ def render_changelog(
tree: Iterable,
loader: BaseLoader,
template: str,
**kwargs,
**kwargs: Any,
) -> str:
jinja_template = get_changelog_template(loader, template)
changelog: str = jinja_template.render(tree=tree, **kwargs)
Expand Down Expand Up @@ -279,7 +282,7 @@ def incremental_build(


def get_smart_tag_range(
tags: list[GitTag], newest: str, oldest: str | None = None
tags: Sequence[GitTag], newest: str, oldest: str | None = None
) -> list[GitTag]:
"""Smart because it finds the N+1 tag.

Expand All @@ -305,10 +308,10 @@ def get_smart_tag_range(


def get_oldest_and_newest_rev(
tags: list[GitTag],
tags: Sequence[GitTag],
version: str,
rules: TagRules,
) -> tuple[str | None, str | None]:
) -> tuple[str | None, str]:
"""Find the tags for the given version.

`version` may come in different formats:
Expand Down
19 changes: 12 additions & 7 deletions commitizen/changelog_formats/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import sys
from typing import ClassVar, Protocol
from typing import Callable, ClassVar, Protocol

if sys.version_info >= (3, 10):
from importlib import metadata
Expand All @@ -25,7 +25,7 @@ class ChangelogFormat(Protocol):

config: BaseConfig

def __init__(self, config: BaseConfig):
def __init__(self, config: BaseConfig) -> None:
self.config = config

@property
Expand Down Expand Up @@ -64,18 +64,17 @@ def get_changelog_format(
:raises FormatUnknown: if a non-empty name is provided but cannot be found in the known formats
"""
name: str | None = config.settings.get("changelog_format")
format: type[ChangelogFormat] | None = guess_changelog_format(filename)

if name and name in KNOWN_CHANGELOG_FORMATS:
format = KNOWN_CHANGELOG_FORMATS[name]
format = (
name and KNOWN_CHANGELOG_FORMATS.get(name) or _guess_changelog_format(filename)
)

if not format:
raise ChangelogFormatUnknown(f"Unknown changelog format '{name}'")

return format(config)


def guess_changelog_format(filename: str | None) -> type[ChangelogFormat] | None:
def _guess_changelog_format(filename: str | None) -> type[ChangelogFormat] | None:
"""
Try guessing the file format from the filename.

Expand All @@ -91,3 +90,9 @@ def guess_changelog_format(filename: str | None) -> type[ChangelogFormat] | None
if filename.endswith(f".{alt_extension}"):
return format
return None


def __getattr__(name: str) -> Callable[[str], type[ChangelogFormat] | None]:
if name == "guess_changelog_format":
return _guess_changelog_format
raise AttributeError(f"module {__name__} has no attribute {name}")
2 changes: 1 addition & 1 deletion commitizen/changelog_formats/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class BaseFormat(ChangelogFormat, metaclass=ABCMeta):
extension: ClassVar[str] = ""
alternative_extensions: ClassVar[set[str]] = set()

def __init__(self, config: BaseConfig):
def __init__(self, config: BaseConfig) -> None:
# Constructor needs to be redefined because `Protocol` prevent instantiation by default
# See: https://bugs.python.org/issue44807
self.config = config
Expand Down
Loading