-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
base: main
Are you sure you want to change the base?
STY: Tweak _page.py #3370
Conversation
Use modern formatting.
Codecov ReportAll modified and coverable lines are covered by tests ✅
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. 🚀 New features to boost your workflow:
|
@@ -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() |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Lines 76 to 77 in 7e1140d
def hash_value_data(self) -> bytes: | |
return f"{self}".encode() |
Use modern formatting.