Skip to content

Commit a365cdc

Browse files
authored
More f-strings (#1317)
1 parent c24b8ad commit a365cdc

2 files changed

Lines changed: 9 additions & 11 deletions

File tree

docs/why.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ You can freely choose which features you want and disable those that you want mo
294294
... b: int
295295
...
296296
... def __repr__(self):
297-
... return "<SmartClass(a=%d)>" % (self.a,)
297+
... return f"<SmartClass(a={self.a})>"
298298
>>> SmartClass(1, 2)
299299
<SmartClass(a=1)>
300300
```

src/attr/_make.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2591,12 +2591,10 @@ def _attrs_to_init_script(
25912591
args = ", ".join(args)
25922592
pre_init_args = args
25932593
if kw_only_args:
2594-
args += "%s*, %s" % (
2595-
", " if args else "", # leading comma
2596-
", ".join(kw_only_args), # kw_only args
2597-
)
2594+
# leading comma & kw_only args
2595+
args += f"{', ' if args else ''}*, {', '.join(kw_only_args)}"
25982596
pre_init_kw_only_args = ", ".join(
2599-
["%s=%s" % (kw_arg, kw_arg) for kw_arg in kw_only_args]
2597+
[f"{kw_arg}={kw_arg}" for kw_arg in kw_only_args]
26002598
)
26012599
pre_init_args += (
26022600
", " if pre_init_args else ""
@@ -2607,12 +2605,12 @@ def _attrs_to_init_script(
26072605
# If pre init method has arguments, pass same arguments as `__init__`
26082606
lines[0] = f"self.__attrs_pre_init__({pre_init_args})"
26092607

2608+
# Python 3.7 doesn't allow backslashes in f strings.
2609+
NL = "\n "
26102610
return (
2611-
f"def {method_name}(self, %s):\n %s\n"
2612-
% (
2613-
args,
2614-
"\n ".join(lines) if lines else "pass",
2615-
),
2611+
f"""def {method_name}(self, {args}):
2612+
{NL.join(lines) if lines else 'pass'}
2613+
""",
26162614
names_for_globals,
26172615
annotations,
26182616
)

0 commit comments

Comments
 (0)