Skip to content

Commit 7d545e4

Browse files
committed
Add no-dict-subscript checker
1 parent 73b166e commit 7d545e4

File tree

3 files changed

+36
-21
lines changed

3 files changed

+36
-21
lines changed

doc/whatsnew/2/2.15/index.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@ Summary -- Release highlights
1515
New checkers
1616
============
1717

18-
1918
Removed checkers
2019
================
2120

2221

2322
Extensions
2423
==========
2524

25+
* ``NoDictSubscriptChecker``
26+
27+
* Added optional extension ``no-dict-subscript`` to emit messages when the ``[]`` operator is used
28+
to access a dictionary value.
2629

2730
False positives fixed
2831
=====================

pylint/checkers/misc.py

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
import tokenize
1111
from typing import TYPE_CHECKING
1212

13-
from astroid import Assign, Dict, nodes
13+
from astroid import nodes
1414

15-
from pylint.checkers import BaseChecker, BaseRawFileChecker, BaseTokenChecker, utils
15+
from pylint.checkers import BaseRawFileChecker, BaseTokenChecker
1616
from pylint.typing import ManagedMessage
1717
from pylint.utils.pragma_parser import OPTION_PO, PragmaParserError, parse_pragma
1818

@@ -176,24 +176,6 @@ def process_tokens(self, tokens: list[tokenize.TokenInfo]) -> None:
176176
)
177177

178178

179-
class NoDictDirectAccessChecker(BaseChecker):
180-
name = "no-dict-direct-access"
181-
msgs = {
182-
"W0001": (
183-
"Uses dict operator []",
184-
"dict-direct-access",
185-
"Used to warn when subscripting a dictionary instead of using the get() function",
186-
),
187-
}
188-
189-
def visit_subscript(self, node: nodes.Subscript) -> None:
190-
if isinstance(utils.safe_infer(node.value), Dict) and not isinstance(
191-
node.parent, Assign
192-
):
193-
self.add_message("dict-direct-access", node=node)
194-
195-
196179
def register(linter: PyLinter) -> None:
197180
linter.register_checker(EncodingChecker(linter))
198181
linter.register_checker(ByIdManagedMessagesChecker(linter))
199-
linter.register_checker(NoDictDirectAccessChecker(linter))
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
2+
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
3+
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
4+
5+
"""Check for dictionary read-access via subscript"""
6+
7+
from astroid import nodes, Dict, Assign
8+
9+
from pylint.checkers import BaseChecker, utils
10+
from pylint.lint import PyLinter
11+
12+
13+
class NoDictSubscriptChecker(BaseChecker):
14+
name = "no-dict-subscript"
15+
msgs = {
16+
"W0001": (
17+
"Uses dict operator []",
18+
"dict-subscript",
19+
"Used to warn when subscripting a dictionary instead of using the get() function",
20+
),
21+
}
22+
23+
def visit_subscript(self, node: nodes.Subscript) -> None:
24+
if isinstance(utils.safe_infer(node.value), Dict) and not isinstance(
25+
node.parent, Assign
26+
):
27+
self.add_message("dict-subscript", node=node)
28+
29+
def register(linter: PyLinter) -> None:
30+
linter.register_checker(NoDictSubscriptChecker(linter))

0 commit comments

Comments
 (0)