Skip to content

Python lint: Ruff rules for comprehensions and performance #512

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

Conversation

cclauss
Copy link
Contributor

@cclauss cclauss commented Apr 14, 2025

Fix the Python lint issues raised by:
% ruff check --select=C4,PERF --ignore=PERF203--statistics | sort -k2

4	C408    	unnecessary-collection-call
1	C416    	unnecessary-comprehension
1	PERF102 	incorrect-dict-iterator
2	PERF401 	manual-list-comprehension
Found 8 errors.
No fixes available (6 hidden fixes can be enabled with the `--unsafe-fixes` option).

Motivation and Context

These proposed changes can make code easier to reason about or faster or both.

% ruff rule PERF401

manual-list-comprehension (PERF401)

Derived from the Perflint linter.

Fix is sometimes available.

What it does

Checks for for loops that can be replaced by a list comprehension.

Why is this bad?

When creating a transformed list from an existing list using a for-loop,
prefer a list comprehension. List comprehensions are more readable and
more performant.

Using the below as an example, the list comprehension is ~10% faster on
Python 3.11, and ~25% faster on Python 3.10.

Note that, as with all perflint rules, this is only intended as a
micro-optimization, and will have a negligible impact on performance in
most cases.

Example

original = list(range(10000))
filtered = []
for i in original:
    if i % 2:
        filtered.append(i)

Use instead:

original = list(range(10000))
filtered = [x for x in original if x % 2]

If you're appending to an existing list, use the extend method instead:

original = list(range(10000))
filtered.extend(x for x in original if x % 2)

Take care that if the original for-loop uses an assignment expression
as a conditional, such as if match:=re.match("\d+","123"), then
the corresponding comprehension must wrap the assignment
expression in parentheses to avoid a syntax error.

How Has This Been Tested?

Breaking Changes

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

https://docs.astral.sh/ruff

@Kludex Kludex merged commit babb477 into modelcontextprotocol:main Apr 15, 2025
7 checks passed
@Kludex
Copy link
Member

Kludex commented Apr 15, 2025

Thanks :)

@cclauss cclauss deleted the ruff-rules-for-comprehensions-and-performance branch April 15, 2025 15:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants