22
33import os
44import os .path
5+ from collections .abc import Generator
56from difflib import SequenceMatcher
67from operator import itemgetter
78from pathlib import Path
8- from typing import Callable , cast
99
1010from commitizen import changelog , defaults , factory , git , out
1111from commitizen .changelog_formats import get_changelog_format
@@ -32,9 +32,10 @@ def __init__(self, config: BaseConfig, args):
3232 if not git .is_git_project ():
3333 raise NotAGitProjectError ()
3434
35- self .config : BaseConfig = config
36- changelog_file_name = args .get ("file_name" ) or cast (
37- str , self .config .settings .get ("changelog_file" )
35+ self .config = config
36+
37+ changelog_file_name = args .get ("file_name" ) or self .config .settings .get (
38+ "changelog_file"
3839 )
3940 if not isinstance (changelog_file_name , str ):
4041 raise NotAllowed (
@@ -114,28 +115,28 @@ def _find_incremental_rev(self, latest_version: str, tags: list[GitTag]) -> str:
114115 on our experience.
115116 """
116117 SIMILARITY_THRESHOLD = 0.89
117- tag_ratio = map (
118- lambda tag : (
119- SequenceMatcher (
118+ scores_and_tag_names : Generator [tuple [float , str ]] = (
119+ (
120+ score ,
121+ tag .name ,
122+ )
123+ for tag in tags
124+ if (
125+ score := SequenceMatcher (
120126 None , latest_version , strip_local_version (tag .name )
121- ).ratio (),
122- tag ,
123- ),
124- tags ,
127+ ).ratio ()
128+ )
129+ >= SIMILARITY_THRESHOLD
125130 )
126131 try :
127- score , tag = max (tag_ratio , key = itemgetter (0 ))
132+ _ , start_rev = max (scores_and_tag_names , key = itemgetter (0 ))
128133 except ValueError :
129134 raise NoRevisionError ()
130- if score < SIMILARITY_THRESHOLD :
131- raise NoRevisionError ()
132- start_rev = tag .name
133135 return start_rev
134136
135137 def write_changelog (
136138 self , changelog_out : str , lines : list [str ], changelog_meta : changelog .Metadata
137139 ):
138- changelog_hook : Callable | None = self .cz .changelog_hook
139140 with smart_open (self .file_name , "w" , encoding = self .encoding ) as changelog_file :
140141 partial_changelog : str | None = None
141142 if self .incremental :
@@ -145,8 +146,8 @@ def write_changelog(
145146 changelog_out = "" .join (new_lines )
146147 partial_changelog = changelog_out
147148
148- if changelog_hook :
149- changelog_out = changelog_hook (changelog_out , partial_changelog )
149+ if self . cz . changelog_hook :
150+ changelog_out = self . cz . changelog_hook (changelog_out , partial_changelog )
150151
151152 changelog_file .write (changelog_out )
152153
@@ -221,14 +222,12 @@ def __call__(self):
221222 extras .update (self .extras )
222223 changelog_out = changelog .render_changelog (
223224 tree , loader = self .cz .template_loader , template = self .template , ** extras
224- )
225- changelog_out = changelog_out .lstrip ("\n " )
225+ ).lstrip ("\n " )
226226
227227 # Dry_run is executed here to avoid checking and reading the files
228228 if self .dry_run :
229- changelog_hook : Callable | None = self .cz .changelog_hook
230- if changelog_hook :
231- changelog_out = changelog_hook (changelog_out , "" )
229+ if self .cz .changelog_hook :
230+ changelog_out = self .cz .changelog_hook (changelog_out , "" )
232231 out .write (changelog_out )
233232 raise DryRunExit ()
234233
0 commit comments