question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"Unnecessary dict call" false positives

See original GitHub issue

The dict function has 3 forms: dict(**kwarg), dict(mapping, **kwarg), and dict(iterable, **kwarg). I’m not sure if it makes sense to apply this rule to every form.

Here’s one example use case:

def foo(data):
    defaults = {'a': 1, 'b': 2}
    return dict(defaults, **data)

This example could be rewritten to use literals, but it would be less succinct and (I assume, but haven’t confirmed) less performant.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
adamchainzcommented, May 17, 2017
0reactions
adamchainzcommented, Jun 19, 2020

Looks like it is, for small dicts at least:

In [1]: def foo(data):
   ...:     defaults = {'a': 1, 'b': 2}
   ...:     return dict(defaults, **data)
   ...:

In [2]: %timeit foo({'c': 3})
397 ns ± 8.3 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [3]: def foo(data):
   ...:     return {"a": 1, "b": 2, **data}
   ...:

In [4]: %timeit foo({'c': 3})
266 ns ± 4.57 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
Read more comments on GitHub >

github_iconTop Results From Across the Web

flake8-comprehensions 3.1.1
Unnecessary (dict/list/tuple) call - rewrite as a literal. ... 3.1.1 (2019-11-16). Fix false positives in C416 rule for asynchronous comprehensions.
Read more >
False positive Definition & Meaning
noun ; A · —a test result indicative of disease that isn't actually present—can trigger a chain reaction of worry, further tests, and...
Read more >
Release highlights - Pylint 2.16.0-dev documentation - PyCQA
Fix false positive useless-type-doc on ignored argument using pylint.extensions.docparams when a function was typed using pep484 but not inside the docstring.
Read more >
Python Analyzer Updates - February & March 2021
... in the existing autofixers and fixed the false positives you reported! ... PTC-W0056: Dictionary size changed during iteration.
Read more >
Unnecessary anxiety definition and meaning
The chemicals in tobacco can cause a false positive test result, which could mean unnecessary anxiety and even an amniocentesis you don't really...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found