Skip to content

CLN: Update old .format to f-string #30547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 30, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions pandas/_config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,17 +461,20 @@ def register_option(key: str, defval: object, doc="", validator=None, cb=None):
raise ValueError(f"{k} is a python keyword")

cursor = _global_config
msg = "Path prefix to option '{option}' is already an option"

for i, p in enumerate(path[:-1]):
if not isinstance(cursor, dict):
raise OptionError(msg.format(option=".".join(path[:i])))
raise OptionError(
f"Path prefix to option '{'.'.join(path[:i])}' is already an option"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @jbrockmendel prefers to define expressions like this in a variable first, instead of using them directly in the f-string.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes i do. thanks for keeping an eye out

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my preference would be to leave the .format syntax when message used more than once to help ensure consistency of error messages.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted to the original .format syntax, but will keep the earlier comments in mind for the future!

)
if p not in cursor:
cursor[p] = {}
cursor = cursor[p]

if not isinstance(cursor, dict):
raise OptionError(msg.format(option=".".join(path[:-1])))
raise OptionError(
f"Path prefix to option '{'.'.join(path[:-1])}' is already an option"
)

cursor[path[-1]] = defval # initialize

Expand Down
11 changes: 5 additions & 6 deletions pandas/core/arrays/boolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -755,9 +755,8 @@ def logical_method(self, other):

if other_is_scalar and not (other is libmissing.NA or lib.is_bool(other)):
raise TypeError(
"'other' should be pandas.NA or a bool. Got {} instead.".format(
type(other).__name__
)
f"'other' should be pandas.NA or a bool.\
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We avoid backslashes in general, you can close the string here, and start it again in the next line, and they'll concatenate. Just leave a space at the end of this first string, and just make the second a f-string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know, done!

Got {type(other).__name__} instead."
)

if not other_is_scalar and len(self) != len(other):
Expand All @@ -772,7 +771,7 @@ def logical_method(self, other):

return BooleanArray(result, mask)

name = "__{name}__".format(name=op.__name__)
name = f"__{op.__name__}__"
return set_function_name(logical_method, name, cls)

@classmethod
Expand Down Expand Up @@ -819,7 +818,7 @@ def cmp_method(self, other):

return BooleanArray(result, mask, copy=False)

name = "__{name}__".format(name=op.__name__)
name = f"__{op.__name__}"
return set_function_name(cmp_method, name, cls)

def _reduce(self, name, skipna=True, **kwargs):
Expand Down Expand Up @@ -922,7 +921,7 @@ def boolean_arithmetic_method(self, other):

return self._maybe_mask_result(result, mask, other, op_name)

name = "__{name}__".format(name=op_name)
name = f"__{op_name}__"
return set_function_name(boolean_arithmetic_method, name, cls)


Expand Down