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

Fix logging-fstring-interpolation false positive #7846

Merged
merged 5 commits into from Nov 26, 2022

Conversation

clavedeluna
Copy link
Collaborator

Type of Changes

Type
βœ“ πŸ› Bug fix

Description

logging-fstring-interpolation should not be raised if logging with an f-str that also has %s formatting.

Closes #4984

@Pierre-Sassoulas Pierre-Sassoulas added False Positive 🦟 A message is emitted but nothing is wrong with the code backport maintenance/2.15.x labels Nov 25, 2022
elif isinstance(node.args[format_pos], nodes.JoinedStr):
elif isinstance(format_arg, nodes.JoinedStr):
if any(
"%s" in val.value
Copy link
Member

Choose a reason for hiding this comment

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

There's a lot of potential other formatting here %r %d, %0.3f, %10s... Maybe this isn't the right approach.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah that's what I assumed I'd hear back. I figured '%s' is the most common, maybe d, r, and we could at least get rid of that FP. Any better way to detect %x formatting?

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure but I think it's not easy to solve. @DanielNoord you worked a lot on f-string could you confirm ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah this seems to open a can of worms, although the same could be said for the original issue.

I don't know, we might just try this and see if other requests pop up.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Unless we think the perf hit is egregious (which I don't since this is in a very specific code section), having this code or maybe I could refactor to a well named method _check_str_formatting or something, and can add a few more formatting types like %d or %r which should cover probably 95% of what we'd ever see in this pattern.

Copy link
Member

Choose a reason for hiding this comment

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

+1 for checking only the most common one. The primer already show that this is useful. Let's do %s, %d, %f and %r. Probably something like is '%' in val.value and any(x in val.value for x in [%s, %d, %f, %r]). Not sure if it's the best performance wise but probably better than any(x in val.value for x in [%s, %d, %f, %r])

@coveralls
Copy link

coveralls commented Nov 25, 2022

Pull Request Test Coverage Report for Build 3553960859

  • 12 of 12 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.007%) to 95.435%

Totals Coverage Status
Change from base Build 3546254739: 0.007%
Covered Lines: 17559
Relevant Lines: 18399

πŸ’› - Coveralls

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Co-authored-by: Mark Byrne <31762852+mbyrnepr2@users.noreply.github.com>
@github-actions

This comment has been minimized.

mbyrnepr2
mbyrnepr2 previously approved these changes Nov 26, 2022
@github-actions
Copy link
Contributor

πŸ€– Effect of this PR on checked open source code: πŸ€–

Effect on django:
The following messages are no longer emitted:

  1. logging-fstring-interpolation:
    Use lazy % formatting in logging functions
    https://github.com/django/django/blob/64b3c413da011f55469165256261f406a277e822/django/test/testcases.py#L1542
  2. logging-fstring-interpolation:
    Use lazy % formatting in logging functions
    https://github.com/django/django/blob/64b3c413da011f55469165256261f406a277e822/django/db/backends/base/base.py#L754
  3. logging-fstring-interpolation:
    Use lazy % formatting in logging functions
    https://github.com/django/django/blob/64b3c413da011f55469165256261f406a277e822/django/db/backends/base/base.py#L772

This comment was generated for commit d4403c3

@Pierre-Sassoulas Pierre-Sassoulas merged commit 978d1ab into pylint-dev:main Nov 26, 2022
@Pierre-Sassoulas Pierre-Sassoulas changed the title Fix logging-fstring-interpolation FP Fix logging-fstring-interpolation false positive Nov 26, 2022
github-actions bot pushed a commit that referenced this pull request Nov 26, 2022
Co-authored-by: Mark Byrne <31762852+mbyrnepr2@users.noreply.github.com>
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
(cherry picked from commit 978d1ab)
@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.15.7 milestone Nov 26, 2022
Pierre-Sassoulas pushed a commit that referenced this pull request Nov 27, 2022
Co-authored-by: Mark Byrne <31762852+mbyrnepr2@users.noreply.github.com>
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
(cherry picked from commit 978d1ab)

Co-authored-by: Dani Alcala <112832187+clavedeluna@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backported False Positive 🦟 A message is emitted but nothing is wrong with the code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

False positive for logging-fstring-interpolation
5 participants