Skip to content

Commit

Permalink
Add redundant-u-string-prefix checker
Browse files Browse the repository at this point in the history
This adds a checker for u-prefixes for strings, as used in Python 2.
Closes pylint-dev#4102
  • Loading branch information
DanielNoord committed Aug 5, 2021
1 parent 7d84a32 commit 64729a2
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
6 changes: 5 additions & 1 deletion ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,14 @@ Release date: TBA

Closes #626

* Add ``disable-next`` option: allows using `# pylint: disable-next=msgid` to disable a message for the following line
* Added ``disable-next`` option: allows using `# pylint: disable-next=msgid` to disable a message for the following line

Closes #1682

* Added ``redundant-u-string-prefix`` checker: Emitted when the u prefix is added to a string

Closes #4102


What's New in Pylint 2.9.6?
===========================
Expand Down
6 changes: 5 additions & 1 deletion doc/whatsnew/2.10.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,14 @@ Other Changes

Closes #626

* Add ``disable-next`` option: allows using `# pylint: disable-next=msgid` to disable a message for the following line
* Added ``disable-next`` option: allows using `# pylint: disable-next=msgid` to disable a message for the following line

Closes #1682

* Added ``format-string-without-interpolation`` checker: Emitted when formatting is applied to a string without any variables to be replaced

Closes #4042

* Added ``redundant-u-string-prefix`` checker: Emitted when the u prefix is added to a string

Closes #4102
23 changes: 23 additions & 0 deletions pylint/checkers/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,13 @@ class StringConstantChecker(BaseTokenChecker):
"Quote delimiters are not used consistently throughout a module "
"(with allowances made for avoiding unnecessary escaping).",
),
"W1406": (
"The u prefix for strings is no longer necessary in Python >=3.0",
"redundant-u-string-prefix",
"Used when we detect a string with a u prefix. These prefixes were necessary "
"in Python 2 to indicate a string was Unicode, but since Python 3.0 strings "
"are Unicode by default.",
),
}
options = (
(
Expand Down Expand Up @@ -905,6 +912,22 @@ def process_non_raw_string_token(
# character can never be the start of a new backslash escape.
index += 2

@check_messages("redundant-u-string-prefix")
def visit_const(self, node: astroid.Const):
if node.pytype() == "builtins.str" and not isinstance(
node.parent, astroid.JoinedStr
):
self._detect_u_string_prefix(node)

def _detect_u_string_prefix(self, node: astroid.Const):
"""Check whether strings include a 'u' prefix like u'String'"""
if node.kind == "u":
self.add_message(
"redundant-u-string-prefix",
line=node.lineno,
col_offset=node.col_offset,
)


def register(linter):
"""required method to auto register this checker"""
Expand Down
14 changes: 14 additions & 0 deletions tests/functional/r/redundant_u_string_prefix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
""""Checks for redundant u-prefixes for strings"""
# pylint: disable=missing-function-docstring

def print_good():
print("String")
print(f"String{1 + 1}")


def print_bad():
print(u"String") # [redundant-u-string-prefix]
print(u'String') # [redundant-u-string-prefix]
print([u"String", u"String2"]) # [redundant-u-string-prefix, redundant-u-string-prefix]
print((u"String", u"String2")) # [redundant-u-string-prefix, redundant-u-string-prefix]
print({1: u"String"}) # [redundant-u-string-prefix]
7 changes: 7 additions & 0 deletions tests/functional/r/redundant_u_string_prefix.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
redundant-u-string-prefix:10:10::The u prefix for strings is no longer necessary in Python >=3.0:HIGH
redundant-u-string-prefix:11:10::The u prefix for strings is no longer necessary in Python >=3.0:HIGH
redundant-u-string-prefix:12:11::The u prefix for strings is no longer necessary in Python >=3.0:HIGH
redundant-u-string-prefix:12:22::The u prefix for strings is no longer necessary in Python >=3.0:HIGH
redundant-u-string-prefix:13:11::The u prefix for strings is no longer necessary in Python >=3.0:HIGH
redundant-u-string-prefix:13:22::The u prefix for strings is no longer necessary in Python >=3.0:HIGH
redundant-u-string-prefix:14:14::The u prefix for strings is no longer necessary in Python >=3.0:HIGH

0 comments on commit 64729a2

Please sign in to comment.