pydata / xarray

N-D labeled arrays and datasets in Python
https://xarray.dev
Apache License 2.0
3.63k stars 1.09k forks source link

Discard useless `!s` conversion in f-string #9752

Closed DimitriPapadopoulos closed 1 week ago

DimitriPapadopoulos commented 2 weeks ago

From PEP 3101:

Explicit Conversion Flag

The explicit conversion flag is used to transform the format field value before it is formatted. This can be used to override the type-specific formatting behavior, and format the value as if it were a more generic type.

In the absence of an explicit conversion flag, the value is simply formatted. The default formatting converts to a str:

Controlling Formatting on a Per-Type Basis

[...] The object.__format__ method is the simplest: It simply converts the object to a string, and then calls format again:

class object:
    def __format__(self, format_spec):
        return format(str(self), format_spec)

I don't think !s was added for a reason here, the default str conversion in the __format__ can be used instead.