-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Conversation
… a % is not 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.
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'
Pull Request Test Coverage Report for Build 2480190206
💛 - Coveralls |
consider-using-f-string
consider-using-f-string
…value it can't be a str
🤖 Effect of this PR on checked open source code: 🤖 Effect on pandas:
This comment was generated for commit a94e214 |
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.
Thank you @d1gl3, great fix !
@@ -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( |
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.
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.
…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
* 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
Type of Changes
Description
Fixed a false positive in
consider-using-f-string
if the left side of a%
is not a string.Closes False positive for
consider-using-f-string
when using modulo operator on a string #6689