pygame-community / pygame-ce

🐍🎮 pygame - Community Edition is a FOSS Python library for multimedia applications (like games). Built on top of the excellent SDL library.
https://pyga.me
773 stars 120 forks source link

Minor optimizations by using comprehensions #2771

Closed cclauss closed 2 months ago

cclauss commented 3 months ago

% pipx install ruff % ruff check --select=C4,PERF --statistics

30  C408    [*] Unnecessary `dict` call (rewrite as a literal)
10  C406    [*] Unnecessary `tuple` literal (rewrite as a `dict` literal)
10  C417    [*] Unnecessary `map` usage (rewrite using a generator expression)
 9  PERF401 [ ] Use a list comprehension to create a transformed list
 2  C419    [*] Unnecessary list comprehension
 2  PERF203 [ ] `try`-`except` within a loop incurs performance overhead
 2  PERF402 [ ] Use `list` or `list.copy` to create a copy of a list

% ruff check --select=C4,PERF --fix --unsafe-fixes

Found 65 errors (52 fixed, 13 remaining).

% ruff rule C408

unnecessary-collection-call (C408)

Derived from the flake8-comprehensions linter.

Fix is always available.

What it does

Checks for unnecessary dict, list or tuple calls that can be rewritten as empty literals.

Why is this bad?

It's unnecessary to call, e.g., dict() as opposed to using an empty literal ({}). The former is slower because the name dict must be looked up in the global scope in case it has been rebound.

Examples

dict()
dict(a=1, b=2)
list()
tuple()

Use instead:

{}
{"a": 1, "b": 2}
[]
()

Fix safety

This rule's fix is marked as unsafe, as it may occasionally drop comments when rewriting the call. In most cases, though, comments will be preserved.

Options

cclauss commented 3 months ago

Are you talking about #2773 or something else?

ankith26 commented 3 months ago

I'm talking about this fail: https://github.com/pygame-community/pygame-ce/actions/runs/8425378679/job/23088593026

Starbuck5 commented 3 months ago

This PR now has merge conflicts.

cclauss commented 3 months ago

Git conflicts fixed.