From a365cdc5fe17cb8147042e3c554f4b920f4bbd9c Mon Sep 17 00:00:00 2001 From: Hynek Schlawack Date: Wed, 31 Jul 2024 13:37:38 +0200 Subject: [PATCH] More f-strings (#1317) --- docs/why.md | 2 +- src/attr/_make.py | 18 ++++++++---------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/docs/why.md b/docs/why.md index ffbeb9b6..0d4d443e 100644 --- a/docs/why.md +++ b/docs/why.md @@ -294,7 +294,7 @@ You can freely choose which features you want and disable those that you want mo ... b: int ... ... def __repr__(self): -... return "" % (self.a,) +... return f"" >>> SmartClass(1, 2) ``` diff --git a/src/attr/_make.py b/src/attr/_make.py index d7828f95..68ddbf15 100644 --- a/src/attr/_make.py +++ b/src/attr/_make.py @@ -2591,12 +2591,10 @@ def _attrs_to_init_script( args = ", ".join(args) pre_init_args = args if kw_only_args: - args += "%s*, %s" % ( - ", " if args else "", # leading comma - ", ".join(kw_only_args), # kw_only args - ) + # leading comma & kw_only args + args += f"{', ' if args else ''}*, {', '.join(kw_only_args)}" pre_init_kw_only_args = ", ".join( - ["%s=%s" % (kw_arg, kw_arg) for kw_arg in kw_only_args] + [f"{kw_arg}={kw_arg}" for kw_arg in kw_only_args] ) pre_init_args += ( ", " if pre_init_args else "" @@ -2607,12 +2605,12 @@ def _attrs_to_init_script( # If pre init method has arguments, pass same arguments as `__init__` lines[0] = f"self.__attrs_pre_init__({pre_init_args})" + # Python 3.7 doesn't allow backslashes in f strings. + NL = "\n " return ( - f"def {method_name}(self, %s):\n %s\n" - % ( - args, - "\n ".join(lines) if lines else "pass", - ), + f"""def {method_name}(self, {args}): + {NL.join(lines) if lines else 'pass'} +""", names_for_globals, annotations, )