Skip to content

Fixed misinterpreted modulo sign for consider-using-f-string #6914

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

Merged
merged 3 commits into from
Jun 11, 2022

Conversation

d1gl3
Copy link
Contributor

@d1gl3 d1gl3 commented Jun 11, 2022

Type of Changes

Type
🐛 Bug fix

Description

@d1gl3 d1gl3 marked this pull request as ready for review June 11, 2022 10:19
@jacobtylerwalls jacobtylerwalls added this to the 2.14.2 milestone Jun 11, 2022
@jacobtylerwalls jacobtylerwalls added Needs backport Needs to be cherry-picked on the current patch version by a pylint's maintainer False Positive 🦟 A message is emitted but nothing is wrong with the code labels Jun 11, 2022
Copy link
Member

@jacobtylerwalls jacobtylerwalls left a comment

Choose a reason for hiding this comment

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

Thanks for the fix! It needs just a bit of hardening against some possible crashes:

(1, 2) % 'garbage'

pylint crashed with a AstroidError and with the following stacktrace:

Traceback (most recent call last):
  File "/Users/.../pylint/pylint/checkers/refactoring/recommendation_checker.py", line 398, in _detect_replacable_format_call
    if not isinstance(node.parent.left.value, str):
  File "/Users/.../astroid/astroid/bases.py", line 119, in __getattr__
    return getattr(self._proxied, name)
AttributeError: 'ClassDef' object has no attribute 'value'

@coveralls
Copy link

coveralls commented Jun 11, 2022

Pull Request Test Coverage Report for Build 2480190206

  • 2 of 2 (100.0%) changed or added relevant lines in 1 file are covered.
  • 8 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+0.003%) to 95.522%

Files with Coverage Reduction New Missed Lines %
pylint/checkers/base/basic_checker.py 8 97.96%
Totals Coverage Status
Change from base Build 2479160162: 0.003%
Covered Lines: 16384
Relevant Lines: 17152

💛 - Coveralls

@jacobtylerwalls jacobtylerwalls changed the title Fixed missinterpreted modulo sign for consider-using-f-string Fixed misinterpreted modulo sign for consider-using-f-string Jun 11, 2022
@github-actions
Copy link
Contributor

🤖 Effect of this PR on checked open source code: 🤖

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

  1. consider-using-f-string:
    Formatting a regular string which could be a f-string
    https://github.com/pandas-dev/pandas/blob/main/pandas/core/generic.py#L2256
  2. consider-using-f-string:
    Formatting a regular string which could be a f-string
    https://github.com/pandas-dev/pandas/blob/main/pandas/core/generic.py#L2871
  3. consider-using-f-string:
    Formatting a regular string which could be a f-string
    https://github.com/pandas-dev/pandas/blob/main/pandas/core/frame.py#L2546
  4. consider-using-f-string:
    Formatting a regular string which could be a f-string
    https://github.com/pandas-dev/pandas/blob/main/pandas/core/frame.py#L2998
  5. consider-using-f-string:
    Formatting a regular string which could be a f-string
    https://github.com/pandas-dev/pandas/blob/main/pandas/io/pickle.py#L25
  6. consider-using-f-string:
    Formatting a regular string which could be a f-string
    https://github.com/pandas-dev/pandas/blob/main/pandas/io/pickle.py#L117
  7. consider-using-f-string:
    Formatting a regular string which could be a f-string
    https://github.com/pandas-dev/pandas/blob/main/pandas/io/common.py#L282
  8. consider-using-f-string:
    Formatting a regular string which could be a f-string
    https://github.com/pandas-dev/pandas/blob/main/pandas/io/common.py#L528
  9. consider-using-f-string:
    Formatting a regular string which could be a f-string
    https://github.com/pandas-dev/pandas/blob/main/pandas/io/common.py#L629
  10. consider-using-f-string:
    Formatting a regular string which could be a f-string
    https://github.com/pandas-dev/pandas/blob/main/pandas/io/stata.py#L2169
  11. consider-using-f-string:
    Formatting a regular string which could be a f-string
    https://github.com/pandas-dev/pandas/blob/main/pandas/io/xml.py#L51
  12. consider-using-f-string:
    Formatting a regular string which could be a f-string
    https://github.com/pandas-dev/pandas/blob/main/pandas/io/xml.py#L962
  13. consider-using-f-string:
    Formatting a regular string which could be a f-string
    https://github.com/pandas-dev/pandas/blob/main/pandas/io/formats/xml.py#L35
  14. consider-using-f-string:
    Formatting a regular string which could be a f-string
    https://github.com/pandas-dev/pandas/blob/main/pandas/io/json/_json.py#L318

This comment was generated for commit a94e214

Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

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

Thank you @d1gl3, great fix !

@Pierre-Sassoulas Pierre-Sassoulas merged commit ca80f03 into pylint-dev:main Jun 11, 2022
@d1gl3 d1gl3 deleted the fix/issue-6689 branch June 11, 2022 19:58
@@ -394,6 +394,12 @@ def _detect_replacable_format_call(self, node: nodes.Const) -> None:
if "\\" in node.parent.right.as_string():
return

# If % applied to another type than str, it's modulo and can't be replaced by formatting
if not hasattr(node.parent.left, "value") or not isinstance(
Copy link
Collaborator

Choose a reason for hiding this comment

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

In the future I think it is better to test for the class of left with an isinstance. That's a little more robust and explicit about what we are checking for here.

@Pierre-Sassoulas Pierre-Sassoulas added Backported and removed Needs backport Needs to be cherry-picked on the current patch version by a pylint's maintainer labels Jun 15, 2022
Pierre-Sassoulas pushed a commit to Pierre-Sassoulas/pylint that referenced this pull request Jun 15, 2022
…int-dev#6914)

* Fixed a false positive in consider-using-f-string if the left side of a % is not a string.

* consider-using-f-string: if left side of a \% does not have the attr value it can't be a str
Pierre-Sassoulas pushed a commit that referenced this pull request Jun 15, 2022
* Fixed a false positive in consider-using-f-string if the left side of a % is not a string.

* consider-using-f-string: if left side of a \% does not have the attr value it can't be a str
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 consider-using-f-string when using modulo operator on a string
5 participants