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

False positive for consider-using-f-string when using modulo operator on a string #6689

Closed
bgurney-rh opened this issue May 24, 2022 · 1 comment · Fixed by #6914
Closed
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code
Milestone

Comments

@bgurney-rh
Copy link

bgurney-rh commented May 24, 2022

Bug description

#!/usr/bin/python3

"""
Test unittest 1.
"""

import unittest

# pylint: disable=pointless-statement
class ModuloFailTestCase(unittest.TestCase):
    """Test modulo operator failure"""

    def test_int_str_modulo_fail(self):
        """Test int and str modulo operator failure"""
        with self.assertRaises(TypeError):
            1 % "str"

Command used

pylint unittestprog1.py

Pylint output

************* Module unittestprog1
unittestprog1.py:16:16: C0209: Formatting a regular string which could be a f-string (consider-using-f-string)

Expected behavior

Do not flag a modulo (%) operation on a string with the consider-using-f-string lint.

(I discovered this issue while working on converting the justbytes module [ https://github.com/mulkieran/justbytes ] to formatted strings:)

class ModTestCase(unittest.TestCase):
    """Test mod."""

    def test_exceptions(self):
        """Test that exceptions are thrown."""
        # pylint: disable=expression-not-assigned
        with self.assertRaises(RangeNonsensicalBinOpError):
            2048 % Range(12, B)
        with self.assertRaises(RangeNonsensicalBinOpError):
            Range(12) % "str"
        with self.assertRaises(RangeNonsensicalBinOpValueError):
            Range(12) % Range(0)
        with self.assertRaises(RangeNonsensicalBinOpValueError):
            Range(12) % 0
        with self.assertRaises(RangeNonsensicalBinOpError):
            Range(12) % Decimal("NaN")

Pylint version

pylint 2.13.9
astroid 2.11.5
Python 3.10.4 (main, Mar 25 2022, 00:00:00) [GCC 12.0.1 20220308 (Red Hat 12.0.1-0)]

(This also occurs in the Fedora 36 package "pylint-2.13.7-1.fc36.noarch" .)

OS / Environment

Fedora 36

@bgurney-rh bgurney-rh added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label May 24, 2022
@bgurney-rh
Copy link
Author

Apologies; the description is supposed to be a reproducer program that can be executed via python3 -m unittest -v unittestprog1.py after saving the output to a file named "unittestprog1.py".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants