-
Notifications
You must be signed in to change notification settings - Fork 192
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
Replace old format string interpolation with f-strings #4400
Conversation
Codecov Report
@@ Coverage Diff @@
## develop #4400 +/- ##
===========================================
- Coverage 79.24% 79.22% -0.01%
===========================================
Files 475 475
Lines 34826 34831 +5
===========================================
- Hits 27594 27593 -1
- Misses 7232 7238 +6
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
First of all, thanks @sphuber for taking on the effort to clean this up! Hehe ;-) To me, the question is more how to best review this PR. P.S. This is just out of curiosity: Do you know why test coverage decreased (very mildly), despite f-strings being less verbose? |
Sure, I sure as hell didn't do this manually but used the
Probably not. These kinds of changes are always a bit risky since we are relying purely on the tests but many of these lines may not actually be covered.
I have no idea, but honestly this happens all the time and we always just ignore it. |
c3575aa
to
00c8797
Compare
Simple, because there're fewer lines covered 😄, e.g. - "AttributeError: '{}' is not a valid attribute of the object "
- "'{}'".format(attr, self.__class__.__name__)
+ f"AttributeError: '{attr}' is not a valid attribute of the object '{self.__class__.__name__}'" So now
You might not have to after #4413 😉 |
For what its worth, I took a quick parse over the diff, and didn't notice any errors (my vision did start to blur a bit after the first hundred diffs though lol) Note, flynt has a pre-commit hook: https://github.com/ikamensh/flynt#pre-commit-hook, should we consider adding it? |
00c8797
to
166ad37
Compare
I thought about this yeah, but decided not to. But to be honest, I am not sure anymore why I thought it best not to add it. I have now added it. Let's see how that goes |
TBH I don't see why always using f-strings should be enforced. Something like the following still makes sense in my book: some_dict = {'a': 1, 'b': 2}
<...>
string = 'values: a={a}, b={b}'.format(**some_dict) |
Yeh but I don't think flynt would convert that? |
Flynt won't touch these though. There are indeed some cases where it is better to use implicit conversion with Edit: just need to figure out how to call it in the pre-commit hook. Apparently |
Alright, in that case it's fine. Haven't tried |
Right, my misunderstanding came from the assumption that what mattered was the number of uncovered lines, but we are actually triggering on the percentage of uncovered lines. Naively, when making a PR what I actually care about is whether/how much I'm increasing the total number of uncovered lines. |
Ideally (IMO), the coverage criteria would be:
Of course the problem is that there's no way of distinguishing between the two cases, so we just fall back on the second (less strict) requirement. But it's a red flag if the number of missed lines increases by a substantial number. |
Fully agree, and it would be nice to be able to trigger on this (the codecov.yml reference does not seem to offer this option (?)). The first priority, however, is probably still to get the random fluctuations under control. You may have noticed that, some time ago, codecov started adding comments to missed lines in the "Files" tab, which is helpful. Edit: It seems like the comments are added to all missed lines that are touched in the PR (whether they were missed before or not). I will need to figure out how to narrow them down to the 7 new misses that were reported or perhaps @sphuber remembers whether he added some extra lines somewhere? |
166ad37
to
6dea8e0
Compare
5695149
to
6530471
Compare
Since Python 3.5 is no longer supported, format string interpolations can now be replaced by f-strings, introduced in Python 3.6, which are more readable, require less characters and are more efficient. Note that `pylint` issues a warning when using f-strings for log messages, just as it does for format interpolated strings. The reasoning is that this is slightly inefficient as the strings are always interpolated even if the log is discarded, but also by not passing the formatting parameters as arguments, the available metadata is reduced. I feel these inefficiencies are premature optimizations as they are really minimal and don't weigh up against the improved readability and maintainability of using f-strings. That is why the `pylint` config is update to ignore the warning `logging-fstring-interpolation` which replaces `logging-format-interpolation` that was ignored before. The majority of the conversions were done automatically with the linting tool `flynt` which is also added as a pre-commit hook. It is added before the `yapf` step because since `flynt` will touch formatting, `yapf` will then get a chance to check it.
6530471
to
0698e19
Compare
Fixes #4394
Since Python 3.5 is no longer supported, format string interpolations
can now be replaced by f-strings, introduced in Python 3.6, which are
more readable, require less characters and are more efficient.
Note that
pylint
issues a warning when using f-strings for logmessages, just as it does for format interpolated strings. The reasoning
is that this is slightly inefficient as the strings are always
interpolated even if the log is discarded, but also by not passing the
formatting parameters as arguments, the available metadata is reduced.
I feel these inefficiencies are premature optimizations as they are
really minimal and don't weigh up against the improved readability and
maintainability of using f-strings. That is why the
pylint
config isupdate to ignore the warning
logging-fstring-interpolation
whichreplaces
logging-format-interpolation
that was ignored before.