Skip to content

STY: Tweak _page.py #3370

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

STY: Tweak _page.py #3370

wants to merge 1 commit into from

Conversation

j-t-1
Copy link
Contributor

@j-t-1 j-t-1 commented Jul 10, 2025

Use modern formatting.

Use modern formatting.
Copy link

codecov bot commented Jul 10, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.83%. Comparing base (a8016c9) to head (6c3fc65).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3370      +/-   ##
==========================================
- Coverage   96.83%   96.83%   -0.01%     
==========================================
  Files          54       54              
  Lines        9098     9097       -1     
  Branches     1664     1664              
==========================================
- Hits         8810     8809       -1     
  Misses        172      172              
  Partials      116      116              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@@ -522,7 +521,7 @@ def hash_bin(self) -> int:

def hash_value_data(self) -> bytes:
data = super().hash_value_data()
data += b"%d" % id(self)
data += f"{id(self)}".encode()
Copy link
Collaborator

Choose a reason for hiding this comment

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

I do not see a benefit of using f-strings with a workaround here to make them compatible with bytes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is more modern, and more readable. %d is decimal but not quick to understand at a glance.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I do not see where this is more readable. What is harder to understand about %d?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Its not widely known that %d is decimal.

F-Strings:

Not only are they more readable, more concise, and less prone to error than other ways of formatting, but they are also faster!

https://github.com/ikamensh/flynt

Copy link
Collaborator

Choose a reason for hiding this comment

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

Its not widely known that %d is decimal.

I have doubts about this. This is the standard specifier for both the old and new string formatting (although it might be omitted with the new one) as well as for loggin.

The quote you are giving is talking about plain strings. We are talking about bytes here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The string is implicitly converted to bytes? So it starts off as a string?

Copy link
Collaborator

Choose a reason for hiding this comment

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

The old string formatting supports bytes as well, possibly due to Python 2. With b"%d" % id(self), the integer value id(self) is converted to a binary sequence while using the %d format for the value. We do not need to know (or do not care) what Python does internally to get something like b"42" as the result.

Using f"{id(self)}".encode() or f"{id(self):d}".encode() (both of which are equivalent as well for this specific case) might return the same result, but will involve an extra conversion from string to bytes due to the .encode() call in the worst case.

Copy link
Contributor Author

@j-t-1 j-t-1 Jul 11, 2025

Choose a reason for hiding this comment

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

Agree do not need to care about how its done modulo performance. Possibly no diffence in performance, or current method may be better.

For the numeric codes, the only difference between str and bytes (or bytearray) interpolation is that the results from these codes will be ASCII-encoded text, not unicode. In other words, for any numeric formatting code %x:

b"%x" % val

is equivalent to:

("%x" % val).encode("ascii")

PEP 461 – Adding % formatting to bytes and bytearray

Is this okay for us here? Need to check if we decode using ASCII as well.

Copy link
Collaborator

Choose a reason for hiding this comment

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

As we are talking about serializing an integer value here, the encoding should not matter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are correct, good call.

Although this PR would not change functionality, it is a newer style and provides more consistency in our codebase.

def hash_value_data(self) -> bytes:
return f"{self}".encode()

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