Skip to content
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

Remove useless {...!s} #2471

Merged
merged 1 commit into from
Nov 7, 2024
Merged

Conversation

DimitriPapadopoulos
Copy link
Contributor

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

@DimitriPapadopoulos DimitriPapadopoulos marked this pull request as ready for review November 7, 2024 17:51
@d-v-b
Copy link
Contributor

d-v-b commented Nov 7, 2024

how do you know they are useless?

@DimitriPapadopoulos
Copy link
Contributor Author

Isn't str() the default conversion?

@d-v-b
Copy link
Contributor

d-v-b commented Nov 7, 2024

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
Copy link
Contributor Author

DimitriPapadopoulos commented Nov 7, 2024

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.

Copy link
Contributor

@d-v-b d-v-b left a comment

Choose a reason for hiding this comment

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

lgtm

@d-v-b d-v-b merged commit 498cb78 into zarr-developers:main Nov 7, 2024
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants