@@ -262,6 +262,18 @@ class BlackFormatter(Formatter):
262
262
formatters = {"ruff" : RuffFormatter (), "black" : BlackFormatter ()}
263
263
264
264
265
+ def removeprefix (text : str , prefix : str ) -> str :
266
+ if text .startswith (prefix ):
267
+ return text [len (prefix ) :]
268
+ return text
269
+
270
+
271
+ def removesuffix (text : str , suffix : str ) -> str :
272
+ if suffix and text .endswith (suffix ):
273
+ return text [: - len (suffix )]
274
+ return text
275
+
276
+
265
277
def format_signature (signature : str , config : dict , signature_formatter : str ) -> str :
266
278
"""Formats signature using ruff or black if either is available."""
267
279
as_func = f"def { signature .strip ()} :\n pass"
@@ -270,9 +282,14 @@ def format_signature(signature: str, config: dict, signature_formatter: str) ->
270
282
if formatter .is_installed :
271
283
try :
272
284
return (
273
- formatter .format (as_func , line_length = line_length )
274
- .removeprefix ("def " )
275
- .removesuffix (":\n pass" )
285
+ # TODO: replace with str.removeprefix and str.removesuffix
286
+ # once Python 3.8 support is no longer required
287
+ removesuffix (
288
+ removeprefix (
289
+ formatter .format (as_func , line_length = line_length ), "def "
290
+ ),
291
+ ":\n pass" ,
292
+ )
276
293
)
277
294
except subprocess .CalledProcessError as e :
278
295
log .warning ("Signature formatter failed %s" , e )
0 commit comments