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

Add new checkers use-list-literal and use-dict-literal #4769

Merged
merged 7 commits into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ Release date: TBA

Closes #3878

* Added ``use-list-literal``: Emitted when ``list()`` is called with no arguments instead of using ``[]``

Closes #4365

* Added ``use-dict-literal``: Emitted when ``dict()`` is called with no arguments instead of using ``{}``

Closes #4365


DanielNoord marked this conversation as resolved.
Show resolved Hide resolved
What's New in Pylint 2.9.6?
Expand Down
8 changes: 8 additions & 0 deletions doc/whatsnew/2.10.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ New checkers

Closes #3826

* Added ``use-list-literal``: Emitted when ``list()`` is called with no arguments instead of using ``[]``

Closes #4365

* Added ``use-dict-literal``: Emitted when ``dict()`` is called with no arguments instead of using ``{}``

Closes #4365


Other Changes
=============
Expand Down
29 changes: 29 additions & 0 deletions pylint/checkers/refactoring/refactoring_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,18 @@ class RefactoringChecker(checkers.BaseTokenChecker):
"value by index lookup. "
"The value can be accessed directly instead.",
),
"R1734": (
"Consider using [] instead of list()",
"use-list-literal",
"Emitted when using list() to create an empty list instead of the literal []. "
"The literal is faster as it avoids an additional function call.",
),
"R1735": (
"Consider using {} instead of dict()",
"use-dict-literal",
"Emitted when using dict() to create an empty dictionary instead of the literal {}. "
"The literal is faster as it avoids an additional function call.",
),
}
options = (
(
Expand Down Expand Up @@ -964,6 +976,8 @@ def _check_consider_using_generator(self, node):
"super-with-arguments",
"consider-using-generator",
"consider-using-with",
"use-list-literal",
"use-dict-literal",
)
def visit_call(self, node):
self._check_raising_stopiteration_in_generator_next_call(node)
Expand All @@ -972,6 +986,7 @@ def visit_call(self, node):
self._check_super_with_arguments(node)
self._check_consider_using_generator(node)
self._check_consider_using_with(node)
self._check_use_list_or_dict_literal(node)

@staticmethod
def _has_exit_in_scope(scope):
Expand Down Expand Up @@ -1454,6 +1469,20 @@ def _check_consider_using_with(self, node: astroid.Call):
if could_be_used_in_with and not _will_be_released_automatically(node):
self.add_message("consider-using-with", node=node)

def _check_use_list_or_dict_literal(self, node: astroid.Call) -> None:
"""Check if empty list or dict is created by using the literal [] or {}"""
try:
for inferred in node.func.infer():
DanielNoord marked this conversation as resolved.
Show resolved Hide resolved
if inferred is astroid.Uninferable:
continue
if isinstance(inferred, astroid.ClassDef) and node.args == []:
DanielNoord marked this conversation as resolved.
Show resolved Hide resolved
if inferred.name == "list":
DanielNoord marked this conversation as resolved.
Show resolved Hide resolved
self.add_message("use-list-literal", node=node)
if inferred.name == "dict" and node.keywords == []:
self.add_message("use-dict-literal", node=node)
except astroid.InferenceError:
pass

def _check_consider_using_join(self, aug_assign):
"""
We start with the augmented assignment and work our way upwards.
Expand Down
4 changes: 2 additions & 2 deletions pylint/checkers/stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,11 +443,11 @@ def __init__(self, linter=None):
for since_vers, func_list in DEPRECATED_METHODS[sys.version_info[0]].items():
if since_vers <= sys.version_info:
self._deprecated_methods.update(func_list)
self._deprecated_attributes = dict()
self._deprecated_attributes = {}
for since_vers, func_list in DEPRECATED_ARGUMENTS.items():
if since_vers <= sys.version_info:
self._deprecated_attributes.update(func_list)
self._deprecated_classes = dict()
self._deprecated_classes = {}
for since_vers, class_list in DEPRECATED_CLASSES.items():
if since_vers <= sys.version_info:
self._deprecated_classes.update(class_list)
Expand Down
2 changes: 1 addition & 1 deletion pylint/checkers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ def parse_format_string(
IncompleteFormatString or UnsupportedFormatCharacter if a
parse error occurs."""
keys = set()
key_types = dict()
key_types = {}
pos_types = []
num_args = 0

Expand Down
2 changes: 1 addition & 1 deletion pylint/utils/pragma_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def parse_pragma(pylint_pragma: str) -> Generator[PragmaRepresenter, None, None]
if action:
yield emit_pragma_representer(action, messages)
action = value
messages = list()
messages = []
assignment_required = action in MESSAGE_KEYWORDS
elif kind in ("MESSAGE_STRING", "MESSAGE_NUMBER"):
messages.append(value)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# pylint: disable=missing-docstring, invalid-name
# pylint: disable=missing-docstring, invalid-name, use-dict-literal

numbers = [1, 2, 3, 4, 5, 6]

Expand Down
2 changes: 1 addition & 1 deletion tests/functional/c/consider/consider_using_dict_items.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Emit a message for iteration through dict keys and subscripting dict with key."""
# pylint: disable=line-too-long,missing-docstring,unsubscriptable-object,too-few-public-methods,redefined-outer-name
# pylint: disable=line-too-long,missing-docstring,unsubscriptable-object,too-few-public-methods,redefined-outer-name,use-dict-literal

def bad():
a_dict = {1: 1, 2: 2, 3: 3}
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/c/consider/consider_using_get.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# pylint: disable=missing-docstring,invalid-name,using-constant-test,invalid-sequence-index,undefined-variable
dictionary = dict()
dictionary = {}
key = 'key'

if 'key' in dictionary: # [consider-using-get]
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/d/dangerous_default_value_py30.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# pylint: disable=missing-docstring
# pylint: disable=missing-docstring, use-list-literal, use-dict-literal
import collections

HEHE = {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# pylint: disable=missing-docstring,invalid-name,line-too-long,too-few-public-methods
# pylint: disable=missing-docstring,invalid-name,line-too-long,too-few-public-methods,use-list-literal,use-dict-literal
import typing
import collections
from typing import Generic, TypeVar
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/i/invalid/e/invalid_exceptions_caught.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# pylint: disable=missing-docstring, too-few-public-methods, useless-object-inheritance
# pylint: disable=missing-docstring, too-few-public-methods, useless-object-inheritance, use-list-literal
# pylint: disable=too-many-ancestors, no-absolute-import, import-error, multiple-imports,wrong-import-position
from __future__ import print_function

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Check invalid value returned by __getnewargs_ex__ """

# pylint: disable=too-few-public-methods,missing-docstring,no-self-use,import-error, useless-object-inheritance
# pylint: disable=too-few-public-methods,missing-docstring,no-self-use,import-error, useless-object-inheritance, use-dict-literal
import six

from missing import Missing
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/i/invalid/invalid_unary_operand_type.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Detect problems with invalid operands used on invalid objects."""
# pylint: disable=missing-docstring,too-few-public-methods,invalid-name
# pylint: disable=unused-variable, useless-object-inheritance
# pylint: disable=unused-variable, useless-object-inheritance, use-dict-literal

import collections

Expand Down
2 changes: 1 addition & 1 deletion tests/functional/n/not_callable.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# pylint: disable=missing-docstring,no-self-use,too-few-public-methods,wrong-import-position, useless-object-inheritance
# pylint: disable=missing-docstring,no-self-use,too-few-public-methods,wrong-import-position,useless-object-inheritance,use-dict-literal

REVISION = None

Expand Down
2 changes: 1 addition & 1 deletion tests/functional/s/statement_without_effect.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Test for statements without effects."""
# pylint: disable=too-few-public-methods, useless-object-inheritance, unnecessary-comprehension
# pylint: disable=too-few-public-methods, useless-object-inheritance, unnecessary-comprehension, use-list-literal

# +1:[pointless-string-statement]
"""inline doc string should use a separated message"""
Expand Down
6 changes: 3 additions & 3 deletions tests/functional/u/unnecessary/unnecessary_comprehension.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
[2 * x for x in iterable] # exclude useful comprehensions
[(x, y, 1) for x, y in iterable] # exclude useful comprehensions
# Test case for issue #4499
a_dict = dict()
a_dict = {}
[(k, v) for k, v in a_dict.items()] # [unnecessary-comprehension]

# Set comprehensions
Expand All @@ -39,8 +39,8 @@
{2 * x: 3 + x for x in iterable} # exclude useful comprehensions

# Some additional tests on helptext -- when object is already a list/set/dict
my_list = list()
my_dict = dict()
my_list = []
my_dict = {}
my_set = set()

[elem for elem in my_list] # [unnecessary-comprehension]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# pylint: disable=missing-docstring, too-few-public-methods, expression-not-assigned, line-too-long

a_dict = dict()
b_dict = dict()
a_dict = {}
b_dict = {}

for k, v in a_dict.items():
print(a_dict[k]) # [unnecessary-dict-index-lookup]
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/u/unnecessary/unnecessary_lambda.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# pylint: disable=undefined-variable
# pylint: disable=undefined-variable, use-list-literal
"""test suspicious lambda expressions
"""

Expand Down
7 changes: 7 additions & 0 deletions tests/functional/u/use/use_dict_literal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# pylint: disable=missing-docstring, invalid-name

x = dict() # [use-dict-literal]
Copy link
Member

Choose a reason for hiding this comment

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

Can you rename the test files?

use_dict_literal -> use_literal_dict
use_list_literal -> use_literal_list

That will make testing them a bit easier, as you then can do pytest tests/test_functional -k use_literal to just test these two.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe -k literal is close enough ? :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I only renamed the test files. Should I have changed the message code as well? To keep it in line with document names? Or is this fine?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@cdce8p Just making sure you didn't miss this comment. No changes needed?

Copy link
Member

Choose a reason for hiding this comment

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

The comment was just about the test file names. So everything is fine with it

x = dict(a="1", b=None, c=3)
x = dict(zip(["a", "b", "c"], [1, 2, 3]))
x = {}
x = {"a": 1, "b": 2, "c": 3}
1 change: 1 addition & 0 deletions tests/functional/u/use/use_dict_literal.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use-dict-literal:3:4::"Consider using {} instead of dict()"
9 changes: 9 additions & 0 deletions tests/functional/u/use/use_list_literal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# pylint: disable=missing-docstring, invalid-name

x = list() # [use-list-literal]
x = list("string")
x = list(range(3))
x = []
x = ["string"]
x = [1, 2, 3]
x = [range(3)]
1 change: 1 addition & 0 deletions tests/functional/u/use/use_list_literal.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use-list-literal:3:4::"Consider using [] instead of list()"
2 changes: 1 addition & 1 deletion tests/functional/u/use/using_constant_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# pylint: disable=invalid-name, missing-docstring,too-few-public-methods
# pylint: disable=no-init,expression-not-assigned, useless-object-inheritance
# pylint: disable=missing-parentheses-for-call-in-test, unnecessary-comprehension, condition-evals-to-constant

# pylint: disable=use-list-literal, use-dict-literal

import collections

Expand Down