More f-strings (#1317)

This commit is contained in:
Hynek Schlawack 2024-07-31 13:37:38 +02:00 committed by GitHub
parent c24b8ad6f7
commit a365cdc5fe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 11 deletions

View File

@ -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 "<SmartClass(a=%d)>" % (self.a,)
... return f"<SmartClass(a={self.a})>"
>>> SmartClass(1, 2)
<SmartClass(a=1)>
```

View File

@ -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,
)