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

Made C408 only apply when no arguments are passed #250

Merged
merged 1 commit into from Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions HISTORY.rst
Expand Up @@ -2,6 +2,9 @@
History
=======

* Made ``C408`` only apply when no arguments are passed to
``dict``/``list``/``tuple``.

3.2.2 (2020-01-20)
------------------

Expand Down
7 changes: 4 additions & 3 deletions README.rst
Expand Up @@ -130,9 +130,10 @@ The list of builtins that are checked for are:
C408: Unnecessary (dict/list/tuple) call - rewrite as a literal.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

It's slower to call e.g. ``dict()`` than using the empty literal, because the
name ``dict`` must be looked up in the global scope in case it has been
rebound. Same for the other two basic types here. For example:
It's slower to call these types than to construct them with literals, e.g.
``dict()`` is slower than ``{}``, because the name ``dict`` must be looked up
in the global scope in case it has been rebound, and it includes the overhead
of a function call. Same for the other two builtin types here. For example:

* Rewrite ``dict()`` as ``{}``
* Rewrite ``list()`` as ``[]``
Expand Down
11 changes: 1 addition & 10 deletions src/flake8_comprehensions.py
Expand Up @@ -187,9 +187,8 @@ def run(self):

elif (
num_positional_args == 0
and not has_star_args(node)
and not has_keyword_args(node)
and node.func.id in ("tuple", "list", "dict")
and len(node.keywords) == 0
):
yield (
node.lineno,
Expand Down Expand Up @@ -321,14 +320,6 @@ def run(self):
)


def has_star_args(call_node):
return any(isinstance(a, ast.Starred) for a in call_node.args)


def has_keyword_args(call_node):
return any(k.arg is None for k in call_node.keywords)


comp_type = {
ast.DictComp: "dict",
ast.ListComp: "list",
Expand Down
14 changes: 6 additions & 8 deletions tests/test_flake8_comprehensions.py
Expand Up @@ -618,6 +618,12 @@ def test_C408_pass_6(flake8dir):
assert result.out_lines == []


def test_C408_pass_7(flake8dir):
flake8dir.make_example_py("dict(a=1)")
result = flake8dir.run_flake8()
assert result.out_lines == []


def test_C408_fail_1(flake8dir):
flake8dir.make_example_py("tuple()")
result = flake8dir.run_flake8()
Expand All @@ -642,14 +648,6 @@ def test_C408_fail_3(flake8dir):
]


def test_C408_fail_4(flake8dir):
flake8dir.make_example_py("dict(a=1)")
result = flake8dir.run_flake8()
assert result.out_lines == [
"./example.py:1:1: C408 Unnecessary dict call - rewrite as a literal."
]


# C409


Expand Down