@@ -34,7 +34,12 @@ class UnidiffParseError(Exception): ...
3434
3535open_file = open
3636make_str = str
37- implements_to_string = lambda x : x
37+
38+
39+ def implements_to_string (x ):
40+ return x
41+
42+
3843unicode = str
3944basestring = str
4045
@@ -82,7 +87,8 @@ class UnidiffParseError(Exception): ...
8287RE_BINARY_DIFF = re .compile (
8388 r"^Binary files? "
8489 r"(?P<source_filename>[^\t]+?)(?:\t(?P<source_timestamp>[\s0-9:\+-]+))?"
85- r"(?: and (?P<target_filename>[^\t]+?)(?:\t(?P<target_timestamp>[\s0-9:\+-]+))?)? (differ|has changed)"
90+ r"(?: and (?P<target_filename>[^\t]+?)(?:\t(?P<target_timestamp>[\s0-9:\+-]+))?)?"
91+ r" (differ|has changed)"
8692)
8793
8894DEFAULT_ENCODING = "UTF-8"
@@ -122,7 +128,7 @@ def __repr__(self):
122128
123129 def __str__ (self ):
124130 # type: () -> str
125- return "%s%s" % ( self .line_type , self .value )
131+ return f" { self .line_type } { self .value } "
126132
127133 def __eq__ (self , other ):
128134 # type: (Line) -> bool
@@ -162,7 +168,7 @@ class PatchInfo(list):
162168
163169 def __repr__ (self ):
164170 # type: () -> str
165- value = "<PatchInfo: %s>" % self [0 ].strip ()
171+ value = f "<PatchInfo: { self [0 ].strip ()} >"
166172 return make_str (value )
167173
168174 def __str__ (self ):
@@ -193,24 +199,19 @@ def __init__(
193199
194200 def __repr__ (self ):
195201 # type: () -> str
196- value = "<Hunk: @@ %d,%d %d,%d @@ %s>" % (
197- self .source_start ,
198- self .source_length ,
199- self .target_start ,
200- self .target_length ,
201- self .section_header ,
202+ value = (
203+ f"<Hunk: @@ { self .source_start } ,{ self .source_length } { self .target_start } ,"
204+ f"{ self .target_length } @@ { self .section_header } >"
202205 )
203206 return make_str (value )
204207
205208 def __str__ (self ):
206209 # type: () -> str
207210 # section header is optional and thus we output it only if it's present
208- head = "@@ -%d,%d +%d,%d @@%s\n " % (
209- self .source_start ,
210- self .source_length ,
211- self .target_start ,
212- self .target_length ,
213- " " + self .section_header if self .section_header else "" ,
211+ section_hdr = f" { self .section_header } " if self .section_header else ""
212+ head = (
213+ f"@@ -{ self .source_start } ,{ self .source_length } "
214+ f"+{ self .target_start } ,{ self .target_length } @@{ section_hdr } \n "
214215 )
215216 content = "" .join (unicode (line ) for line in self )
216217 return head + content
@@ -252,22 +253,22 @@ def is_valid(self):
252253 def source_lines (self ):
253254 # type: () -> Iterable[Line]
254255 """Hunk lines from source file (generator)."""
255- return (l for l in self if l .is_context or l .is_removed )
256+ return (line for line in self if line .is_context or line .is_removed )
256257
257258 @property
258259 def source (self ):
259260 # type: () -> Iterable[str]
260- return [str (l ) for l in self .source_lines ()]
261+ return [str (line ) for line in self .source_lines ()]
261262
262263 def target_lines (self ):
263264 # type: () -> Iterable[Line]
264265 """Hunk lines from target file (generator)."""
265- return (l for l in self if l .is_context or l .is_added )
266+ return (line for line in self if line .is_context or line .is_added )
266267
267268 @property
268269 def target (self ):
269270 # type: () -> Iterable[str]
270- return [str (l ) for l in self .target_lines ()]
271+ return [str (line ) for line in self .target_lines ()]
271272
272273
273274class PatchedFile (list ):
@@ -302,18 +303,16 @@ def __str__(self):
302303 # patch info is optional
303304 info = "" if self .patch_info is None else str (self .patch_info )
304305 if not self .is_binary_file and self :
305- source = "--- %s%s\n " % (
306- self .source_file ,
307- "\t " + self .source_timestamp if self .source_timestamp else "" ,
308- )
309- target = "+++ %s%s\n " % (
310- self .target_file ,
311- "\t " + self .target_timestamp if self .target_timestamp else "" ,
312- )
306+ source_ts = f"\t { self .source_timestamp } " if self .source_timestamp else ""
307+ source = f"--- { self .source_file } { source_ts } \n "
308+
309+ target_ts = f"\t { self .target_timestamp } " if self .target_timestamp else ""
310+ target = f"+++ { self .target_file } { target_ts } \n "
311+
313312 hunks = "" .join (unicode (hunk ) for hunk in self )
314313 return info + source + target + hunks
315314
316- def _parse_hunk (self , header , diff , encoding , metadata_only ):
315+ def _parse_hunk (self , header , diff , encoding , metadata_only ): # noqa: C901
317316 # type: (str, enumerate[str], Optional[str], bool) -> None
318317 """Parse hunk details."""
319318 header_info = RE_HUNK_HEADER .match (header )
@@ -340,7 +339,7 @@ def _parse_hunk(self, header, diff, encoding, metadata_only):
340339 LINE_TYPE_CONTEXT ,
341340 LINE_TYPE_NO_NEWLINE ,
342341 ):
343- raise UnidiffParseError ("Hunk diff line expected: %s" % line )
342+ raise UnidiffParseError (f "Hunk diff line expected: { line } " )
344343
345344 if line_type == LINE_TYPE_ADDED :
346345 target_line_no += 1
@@ -362,7 +361,7 @@ def _parse_hunk(self, header, diff, encoding, metadata_only):
362361 valid_line = RE_HUNK_EMPTY_BODY_LINE .match (line )
363362
364363 if not valid_line :
365- raise UnidiffParseError ("Hunk diff line expected: %s" % line )
364+ raise UnidiffParseError (f "Hunk diff line expected: { line } " )
366365
367366 line_type = valid_line .group ("line_type" )
368367 if line_type == LINE_TYPE_EMPTY :
@@ -410,7 +409,7 @@ def _parse_hunk(self, header, diff, encoding, metadata_only):
410409 raise UnidiffParseError ("Hunk is shorter than expected" )
411410
412411 if metadata_only :
413- # HACK: set fixed calculated values when metadata_only is enabled
412+ # set fixed calculated values when metadata_only is enabled
414413 hunk ._added = added
415414 hunk ._removed = removed
416415
@@ -530,7 +529,7 @@ def __str__(self):
530529 # type: () -> str
531530 return "" .join (unicode (patched_file ) for patched_file in self )
532531
533- def _parse (self , diff , encoding , metadata_only ):
532+ def _parse (self , diff , encoding , metadata_only ): # noqa: C901
534533 # type: (StringIO, Optional[str], bool) -> None
535534 current_file = None
536535 patch_info = None
@@ -561,7 +560,7 @@ def _parse(self, diff, encoding, metadata_only):
561560 is_diff_git_new_file = RE_DIFF_GIT_NEW_FILE .match (line )
562561 if is_diff_git_new_file :
563562 if current_file is None or patch_info is None :
564- raise UnidiffParseError ("Unexpected new file found: %s" % line )
563+ raise UnidiffParseError (f "Unexpected new file found: { line } " )
565564 current_file .source_file = DEV_NULL
566565 patch_info .append (line )
567566 continue
@@ -570,7 +569,7 @@ def _parse(self, diff, encoding, metadata_only):
570569 is_diff_git_deleted_file = RE_DIFF_GIT_DELETED_FILE .match (line )
571570 if is_diff_git_deleted_file :
572571 if current_file is None or patch_info is None :
573- raise UnidiffParseError ("Unexpected deleted file found: %s" % line )
572+ raise UnidiffParseError (f "Unexpected deleted file found: { line } " )
574573 current_file .target_file = DEV_NULL
575574 patch_info .append (line )
576575 continue
@@ -598,7 +597,7 @@ def _parse(self, diff, encoding, metadata_only):
598597 if current_file is not None and not (
599598 current_file .target_file == target_file
600599 ):
601- raise UnidiffParseError ("Target without source: %s" % line )
600+ raise UnidiffParseError (f "Target without source: { line } " )
602601 if current_file is None :
603602 # add current file to PatchSet
604603 current_file = PatchedFile (
@@ -619,15 +618,15 @@ def _parse(self, diff, encoding, metadata_only):
619618 if is_hunk_header :
620619 patch_info = None
621620 if current_file is None :
622- raise UnidiffParseError ("Unexpected hunk found: %s" % line )
621+ raise UnidiffParseError (f "Unexpected hunk found: { line } " )
623622 current_file ._parse_hunk (line , diff , encoding , metadata_only )
624623 continue
625624
626625 # check for no newline marker
627626 is_no_newline = RE_NO_NEWLINE_MARKER .match (line )
628627 if is_no_newline :
629628 if current_file is None :
630- raise UnidiffParseError ("Unexpected marker: %s" % line )
629+ raise UnidiffParseError (f "Unexpected marker: { line } " )
631630 current_file ._add_no_newline_marker_to_last_hunk ()
632631 continue
633632
0 commit comments