@@ -262,6 +262,18 @@ class BlackFormatter(Formatter):
262262formatters = {"ruff" : RuffFormatter (), "black" : BlackFormatter ()}
263263
264264
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+
265277def format_signature (signature : str , config : dict , signature_formatter : str ) -> str :
266278 """Formats signature using ruff or black if either is available."""
267279 as_func = f"def { signature .strip ()} :\n pass"
@@ -270,9 +282,14 @@ def format_signature(signature: str, config: dict, signature_formatter: str) ->
270282 if formatter .is_installed :
271283 try :
272284 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+ )
276293 )
277294 except subprocess .CalledProcessError as e :
278295 log .warning ("Signature formatter failed %s" , e )
0 commit comments