Skip to content

More f-strings, less format() #10505

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 1 commit into from
Jul 5, 2025
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
7 changes: 3 additions & 4 deletions xarray/backends/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,11 +560,10 @@ def _infer_dtype(array, name=None):

native_dtypes = set(np.vectorize(type, otypes=[object])(array.ravel()))
if len(native_dtypes) > 1 and native_dtypes != {bytes, str}:
native_dtype_names = ", ".join(x.__name__ for x in native_dtypes)
raise ValueError(
"unable to infer dtype on variable {!r}; object array "
"contains mixed native types: {}".format(
name, ", ".join(x.__name__ for x in native_dtypes)
)
f"unable to infer dtype on variable {name!r}; object array "
f"contains mixed native types: {native_dtype_names}"
)

element = array[(0,) * array.ndim]
Expand Down
9 changes: 7 additions & 2 deletions xarray/computation/apply_ufunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,13 @@ def __repr__(self):
return f"{type(self).__name__}({list(self.input_core_dims)!r}, {list(self.output_core_dims)!r})"

def __str__(self):
lhs = ",".join("({})".format(",".join(dims)) for dims in self.input_core_dims)
rhs = ",".join("({})".format(",".join(dims)) for dims in self.output_core_dims)
comma_separated = ",".join
lhs = comma_separated(
Copy link
Collaborator

Choose a reason for hiding this comment

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

not sure this got simpler, but ok!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure either.

The only benefit might be that the intent shows clearly: separate with commas.

f"({comma_separated(dims)})" for dims in self.input_core_dims
)
rhs = comma_separated(
f"({comma_separated(dims)})" for dims in self.output_core_dims
)
return f"{lhs}->{rhs}"

def to_gufunc_string(self, exclude_dims=frozenset()):
Expand Down
2 changes: 1 addition & 1 deletion xarray/computation/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def __repr__(self) -> str:
"""provide a nice str repr of our rolling object"""

attrs = ",".join(
"{k}->{v}{c}".format(k=k, v=w, c="(center)" if c else "")
f"{k}->{w}{'(center)' if c else ''}"
for k, w, c in zip(self.dim, self.window, self.center, strict=True)
)
return f"{self.__class__.__name__} [{attrs}]"
Expand Down
8 changes: 4 additions & 4 deletions xarray/core/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,17 +340,17 @@ def summarize_variable(
first_col = pretty_print(first_col, col_width)

if variable.dims:
dims_str = "({}) ".format(", ".join(map(str, variable.dims)))
dims_str = ", ".join(map(str, variable.dims))
dims_str = f"({dims_str}) "
else:
dims_str = ""

nbytes_str = f" {render_human_readable_nbytes(variable.nbytes)}"
front_str = f"{first_col}{dims_str}{variable.dtype}{nbytes_str} "
front_str = f"{first_col}{dims_str}{variable.dtype} {render_human_readable_nbytes(variable.nbytes)} "

values_width = max_width - len(front_str)
values_str = inline_variable_array_repr(variable, values_width)

return front_str + values_str
return f"{front_str}{values_str}"


def summarize_attr(key, value, col_width=None):
Expand Down
Loading