zarr-developers / zarr-python

An implementation of chunked, compressed, N-dimensional arrays for Python.
https://zarr.readthedocs.io
MIT License
1.53k stars 286 forks source link

Remove useless `{...!s}` #2471

Closed DimitriPapadopoulos closed 2 weeks ago

DimitriPapadopoulos commented 2 weeks ago

Remove useless !s in f"...{...!s}...".

d-v-b commented 2 weeks ago

how do you know they are useless?

DimitriPapadopoulos commented 2 weeks ago

Isn't str() the default conversion?

d-v-b commented 2 weeks ago

I don't actually know, but I do know that officially !s is redundant: https://peps.python.org/pep-0498/#s-r-and-a-are-redundant, so if we do want to be sure we are calling str() in the fstring we should directly call str.

But I'd like to know why those !s were added before we remove them

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.