grass
grass copied to clipboard
style: Fixes unnecessary-collection-call (C408) for empty collections
Only applies fixes to empty collections by ruff check --select "C408" --unsafe-fixes --fix --config "lint.flake8-comprehensions.allow-dict-calls-with-keyword-arguments = true" in order to limit the review scope.
Part of preparing the repo for Pylint 3.x for https://github.com/OSGeo/grass/issues/3921
Uses the fixes provided for ruff rule unnecessary-collection-call (C408) to fix part of Pylint's use-dict-literal / R1735 rule. I say "Part of", as for this PR I limited it to only the empty initializers.
Using literals instead of function calls is faster, as it doesn't require to load from global scope (to make sure "list" or "dict" wasn't changed to something else). Also, for the same reason it is better to import the functions to put them in scope (from xyz import thatfunc") instead of just using "import xyz" and then calling "xyz.thatfunc()" (it is slower to go fetch in xyz in global scope and then fetching thatfunc in that scope than importing it once, especially in loops).
Here, it says 3x faster (using [] instead of list() saves 1 opcode, and the [] avoids the LOAD_NAME opcode. https://ealexbarros.medium.com/why-is-faster-than-list-in-python-5bef48b530fc
Here, where it is analyzed for Python 3.12 in 2024, it goes in the same direction https://madebyme.today/blog/python-dict-vs-curly-brackets/, plus explains that {} uses a pre-allocated dict and adds the values if needed.