Skip to content

CLN: Update old string formatting to f-string #30631

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 3 commits into from
Jan 3, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 6 additions & 8 deletions pandas/core/ops/array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,17 +246,16 @@ def comparison_op(
res_values = comp_method_OBJECT_ARRAY(op, lvalues, rvalues)

else:
op_name = "__{op}__".format(op=op.__name__)
op_name = f"__{op.__name__}__"
method = getattr(lvalues, op_name)
with np.errstate(all="ignore"):
res_values = method(rvalues)

if res_values is NotImplemented:
res_values = invalid_comparison(lvalues, rvalues, op)
if is_scalar(res_values):
raise TypeError(
"Could not compare {typ} type with Series".format(typ=type(rvalues))
)
typ = type(rvalues)
raise TypeError(f"Could not compare {typ} type with Series")

return res_values

Expand Down Expand Up @@ -293,11 +292,10 @@ def na_logical_op(x: np.ndarray, y, op):
OverflowError,
NotImplementedError,
):
typ = type(y).__name__
raise TypeError(
"Cannot perform '{op}' with a dtyped [{dtype}] array "
"and scalar of type [{typ}]".format(
op=op.__name__, dtype=x.dtype, typ=type(y).__name__
)
f"Cannot perform '{op.__name__}' with a dtyped [{x.dtype}] array "
Copy link
Contributor

Choose a reason for hiding this comment

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

i agree with @TomAugspurger here - i would simply in-line this rather than creating a temporary

temporary only if it’s a really complicated expr or has quoting itself (eg a .join(..)

f"and scalar of type [{typ}]"
)

return result
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/ops/dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,10 @@ def not_implemented(*args, **kwargs):

if method == "__call__" and op_name in special and kwargs.get("out") is None:
if isinstance(inputs[0], type(self)):
name = "__{}__".format(op_name)
name = f"__{op_name}__"
return getattr(self, name, not_implemented)(inputs[1])
else:
name = flipped.get(op_name, "__r{}__".format(op_name))
name = flipped.get(op_name, f"__r{op_name}__")
return getattr(self, name, not_implemented)(inputs[0])
else:
return NotImplemented
13 changes: 4 additions & 9 deletions pandas/core/ops/invalid.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,8 @@ def invalid_comparison(left, right, op):
elif op is operator.ne:
res_values = np.ones(left.shape, dtype=bool)
else:
raise TypeError(
"Invalid comparison between dtype={dtype} and {typ}".format(
dtype=left.dtype, typ=type(right).__name__
)
)
typ = type(right).__name__
raise TypeError(f"Invalid comparison between dtype={left.dtype} and {typ}")
return res_values


Expand All @@ -52,10 +49,8 @@ def make_invalid_op(name: str):
"""

def invalid_op(self, other=None):
raise TypeError(
"cannot perform {name} with this index type: "
"{typ}".format(name=name, typ=type(self).__name__)
)
typ = type(self).__name__
raise TypeError(f"cannot perform {name} with this index type: {typ}")

invalid_op.__name__ = name
return invalid_op
5 changes: 3 additions & 2 deletions pandas/core/ops/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ def f(self, other):

return self

f.__name__ = "__i{name}__".format(name=method.__name__.strip("__"))
name = method.__name__.strip("__")
f.__name__ = f"__i{name}__"
return f

new_methods.update(
Expand Down Expand Up @@ -214,7 +215,7 @@ def _create_methods(cls, arith_method, comp_method, bool_method, special):
)

if special:
dunderize = lambda x: "__{name}__".format(name=x.strip("_"))
dunderize = lambda x: f"__{x.strip('_')}__"
else:
dunderize = lambda x: x
new_methods = {dunderize(k): v for k, v in new_methods.items()}
Expand Down
5 changes: 2 additions & 3 deletions pandas/core/ops/roperator.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ def rmod(left, right):
# formatting operation; this is a TypeError
# otherwise perform the op
if isinstance(right, str):
raise TypeError(
"{typ} cannot perform the operation mod".format(typ=type(left).__name__)
)
typ = type(left).__name__
raise TypeError(f"{typ} cannot perform the operation mod")

return right % left

Expand Down